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;
    }