Friday, April 12, 2013

Display a Popup when a User Login to Salesforce




Many times there is a need to broadcast a message to all the SFDC users whenever they log into the org, the message could be just a Reminder/Policy changes
or sometimes user needs to accept the terms & condn to use a particular application.
So when the user logs into the org and the first thing he/she sees is a pop up screen. He can neglect the message and continue working on his tasks but This
message needs to be displayed every time user logs in till the USer Accepts the information.

e.g. Lets say you have launched a new app in your sfdc org and you want to display the app usage policy to all the users before they can start using the
app. i.e. you want the Users to accept the policies.

In order to build this the following steps can be implemented in your SFDC org:

1) Develop a VF page which will display a popup to the end user: Something like this http://www.salesforcegeneral.com/salesforce-modal-dialog-box/

2) You will need a corresponding apex class too to support the VF page.

3) In order to check whether the User has accepted the policy or not; lets add a checkbox field on the User records... lets say Policy Accepted

4) In our controller class we can use UserInfo method to get the current user details and check whether this flag is checked or not.
If the flag is checked then we'll redirect the user to the std Home page if its unchecked we'll display the popup to the User. The pouup will have Accept
Button, if user clicks this button the Policy accepted flag on the User record will be set to true.

Final & the most important Trick :)

5) Create a VF custom TAB and overwrite this TAB with the VF page we created above.
6) Add this TAB as a default landing TAB in all of your Apps and you are done!!

Let me know if you need more explanation....I hope this post will be helpful to many salesforce developers.

- Rahul


Friday, February 8, 2013

batch & scheduler class testing

Template for batch & scheduler class test method:

@isTest 

private class Test_Nameofurclass {
    
    static testmethod void test_Nameofurclass (){
        
        DateTime currTime = DateTime.now();
        Integer min = currTime.minute();
        Integer hour = currTime.hour();
        String sch;
        
    if(min <= 58)
            sch = '0 '+ (min + 1) + ' ' + hour + ' * * ? '+ currTime.year();
        else          
            sch = '0 0 '+ (hour + 1) + ' * * ? '+ currTime.year();
        
        Test.startTest();
        
    nameofschedularclass obj = new nameofschedularclass  ();                
    String jobId = system.schedule('test', sch, obj);        
        CronTrigger ct = [SELECT id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger where id = :jobId];        
        System.assertEquals(sch, ct.CronExpression);         
                
        insert test records satisfying your query. 
        
                                                     
        database.executeBatch(new nameofurbatchclass());        
            
        Test.stopTest();
    }

}

Tuesday, January 22, 2013

Build a dynamic SOQL query string

This post is one of my promises to myself this year to build reusable components. I will try to post a few here. But whenever i write a code, Good design pattern is to make it as reusable as possible.

This Apex method basically returns the dynamic soql query string when you feed the list of field api names, object name , conditions and limit .
[Edit] This works only for soql and does not do aggregate functions or polymorphic soql or sosl

/* Return dynamic soql query string
     Inputs list of field api names
     Object name
     list of Condition strings eg 'name=\'test\'' , 'noofemployees=0'
     limit string eg '50000'*/
     */ ----------------------------------------------------
     usage
     list<string> fieldname = new list<string>();
     fieldname.add('id');
     fieldname.add('name');
     list<string> conditions = new list<string>();
     conditions.add('name=\'test\'');
   
     eg1:dynamicSoqlString(fieldname,'account',null,null)
     eg2:dynamicSoqlString(fieldname,'account',null,'2')  
     eg3:dynamicSoqlString(fieldname,'account',conditions,'2');
     */
     

     public static string dynamicSoqlString(List<string> apiNames,string objectname, list<string> conditions, string limitcount){
        if(objectname == null || apinames.size()==0){
            return null;
        }
        string soqlstring ='select ';
        for(integer counter =0;counter<apinames.size();counter++){
            if(counter!=0){
                soqlstring+=',';
            }
            soqlstring+=apinames[counter];
        }
        soqlstring += ' from '+ objectname;
        if(conditions != null && !conditions.isempty()){
            soqlstring+=' where ';
            for(integer counter =0;counter<conditions.size();counter++){
                if(counter!=0){
                    soqlstring+=' and ';
                }
                soqlstring+=conditions[counter];
            }
        }
        if(limitcount != null && limitcount !=''){
            soqlstring+=' limit '+limitcount;
        }
        return soqlstring;
    }

Saturday, October 20, 2012

Prevent inserting duplicate records without coding!!!

Hi Friends,

Today implemented this simple technique to prevent inserting  duplicate records ( records with same name)

In one of my customer's org... there was a trigger written on a campaign object to prevent inserting campaign with same name...some developer wrote it few months back... though its a simple trigger but there was a logical bug in it.... however when our system admin informed me about this issue....i thought of the following solution:-


1- Create a new text field called "custom_name__c"
2- Check "Unique(do not allow dups)" checkbox while you are creating this field
3- Create a workflow rule that updates this field with the standard name field of the object every time record is inserted or updated.....

simple right?????
 so there is no need of that trigger anymore!!!!!!!!!!!1 :)


Thursday, May 24, 2012

dashboard auto refresh


Auto refreshing dashboard:


This is the feature many sfdc admins have been asking for.... Currently it is not possible to refresh the dashboard automatically when the sfdc user logs in to salesforce.com.


use the following steps to refresh the dashboard( automatically...after 5 seconds) when the user logs in to the salesforce OR the user clicks the home tab:- 


1) Create a new custom Home Page Component( type:HTML)


2) Add the following code:-(click the show html button before pasting the code)
<script type="text/javascript"> function refreshDashboardTimeout() {var dashboardButton = document.getElementById("db_ref_btn");dashboardButton.click()}window.setTimeout("refreshDashboardTimeout();", 5*1000); </script>


3) save and Add this component to your Home Page Layout.


quiet simple....right?? :)