Saturday, May 2, 2015

List of items that cannot be deployed in salesforce

Recently when I was deploying configuration for an org which has not gone live, into UAT I made note of manual tasks as i need to reuse that for deploying from UAT to production. I thought i will add few more items on the list to the list i already had. These items had to be manually configured and cannot be deployed.

  •  Renaming Standard fields labels
  • Deletion of standard Picklist values
  • Configuring Search Layouts for all standard objects
  • Escalation rules and Assignment rules
  • Quote Templates
Always as a golden rule Deploy anything with profiles as all the necessary permissions will be carried over. Please feel free to add anything in the comments

Friday, May 9, 2014

Calculate Nth Business Day using Apex in Salesforce.com

It's been a year since I've posted anything on my blog. I've been busy with a lot of custom development on force.com. :)

You might run into a situation where you have to calculate the Nth working days from a specified date.
You will find this post very useful to calculate Nth business day from a given date.

My client has a Service Level Agreement (SLA) requirements in which they need to act on customer complaints within N business days depending upon the severity.

So I came up with the following apex code to determine the Nth business day from a given date. (This APEX CODE Utilize the Holiday object.)
It's pretty straight forward, and it might come in handy for you.

To setup holidays Go to : Administrative setup > Company Profile > Holidays> add few entries e.g. 26th May 2014 >Memorial Day

Apex code is as below:

 //to calculate Nth business day 
    public static Date calculateNthWorkingDay(Date fromDate, Integer N){

         //Query hoildays from Org  
                List<Holiday> holidays=[Select StartTimeInMinutes, Name, ActivityDate From Holiday ];

                Date NthDay = fromDate; 
                
                for(Integer k=0;k<N ;k++ ){
                    if(checkifWorkDay(NthDay.addDays(1),holidays)){                        
                        NthDay = NthDay.addDays(1);
                    } 
                    else
                    {
                      NthDay = NthDay.addDays(1);
                      K--;
                    }
                }

                return NthDay;//send Nth Business day

      }

    //To check if sent date is a working day by comparing with org holidays
  public static boolean checkifWorkDay(Date sentDate,List<Holiday> holidays){
    
                Date weekStart  = sentDate.toStartofWeek();
                
                for(Holiday hday:holidays){
                        if(sentDate.daysBetween(hday.ActivityDate) == 0)                        
                        return false;                        
                }
                
                if(weekStart.daysBetween(sentDate) ==0 || weekStart.daysBetween(sentDate) == 6)
                {
                       return false;
                } else 
                       return true;
    }

Add these methods in your apex class. lets say name of the apex class > Your_APEX_Class_Name

In order to get 10th business day from today call this method as follows:

date nday = Your_APEX_Class_Name.calculateNthWorkingDay(system.today(),10 );
system.debug('nday is**'+nday);

Hope you will find this code useful.

Thanks,
Rahul G

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