Category: Salesforce

  • Salesforce: Adding specific business days to a date using BusinessHours class

    Businesshours is a system class provided by Salesforce for performing various operations on the DateTime values. It provides methods for checking if a date falls during business hours, whats the diff between two dates in business.

    The in-built method does allow adding the specific days to the DateTime value but there is a catch, it needs to be in the Long format. More information could be found here.

    add(businessHoursId, startDate, intervalMilliseconds)

    What exactly this method does?

    The method has been specifically designed to add the number of hours in the millisecond format. And addition is done with respect to business hours. Let’s consider you want to add 1 day to the Date December 18th, 10 AM. And business hours are set for 8 AM to 8 PM for all days.

    Long interval = 24 * 60 * 60 * 1000;
                     h   m    s    ms
    Datetime dt = DateTime.newInstance(2017, 12, 18, 10,0,0);
    System.debug('Date :' + dt.format());
    Long interval = (24 * 60 * 60 * 1000);
    Datetime newdt = BusinessHours.add(bh.Id, dt, interval);
    System.debug('After adding 1 day :' + newdt.format());
    
    //OUTPUT
    22:12:52:003 USER_DEBUG [3]|DEBUG|Date :12/18/2017 10:00 AM
    22:12:52:004 USER_DEBUG [6]|DEBUG|After adding 1 day :12/20/2017 10:00 AM
    

    This calculation added complete 2 days as business hours are set 12 hours a day.

    Alternative for avoiding such hour based calculation could be going in an iterative way.

    public static Datetime addBusinessDays(Datetime startDate, Integer numberOfDays, id busniesshourId)
    {
    	Integer count = 0;
    	while (count < days) {
    		startDate = startDate.addDays(1);
    		if (BusinessHours.isWithin(businesshourId, startDate))
    			count++;
    	}
    	return startDate;
    }

    This will also ensure that the initial time is preserved.

  • Salesforce: Trigger on Attachment – Restricting user from attaching files with specific extensions

    By default, Salesforce doesn’t allow admin to configure or restrict a user from adding a specific type of files. Admin may need that user should not be able to attach files of type exe, dll which can have the virus in them. Also, there is no virus check is done when a file is getting uploaded to the Salesforce.

    But Salesforce does allow writing trigger on the Attachment Object by which you can implement such restrictions. Following is the snippet of the code which will restrict the user from adding the files with extension mentioned in the set list. You can also edit the code and do the reverse by checking if extension exists in the set then only allow attaching the file.

    https://gist.github.com/prasannadeshpande/7ad6f5e49c83ab5a84e628e1096c24f8

    Once trigger code is up and running, whenever Salesforce tries to attach any file with extension exe or dll he will come across following error message.

    AttachmentException

  • Creating multiple tabs for same Salesforce Object

    Many time I have seen Salesforce Admins wants multiple tabs for same object depending upon the RecordTypes or some other criteria like separate tabs for Premium Account, Gold Accounts and Silver. Indeed they can create multiple views and use the picklist and use “Go” button. But it has disadvantage like multiple clicks, not that user friendly.

    In short there is no direct way of creating multiple tabs for same object in Salesforce. Then I thought of creating an Apex component which will take some inputs like ObjectName, Fields to display and criteria/Filter. This appraoch was needing too much of coding and generic code writing for supporting it for all objects. And it was limited to the query filter one can have.

    Creating multiple views is the feature inbuilt provided by the Salesforce and is very powerful. So I thought to use the apex:enhancedList control which takes the viewId as a parameter. You can find the documentation for the EnhancedList here. The most important thing is apex:enhancedlist only requires the viewid.

    I created a simple VisualForce Page like below.

    <apex:page >
    <apex:enhancedList height=”600″ rowsPerPage=”25″ id=”AccountList” listid=”{!$CurrentPage.Parameters.viewId}” customizable=”false”/>
    </apex:page>

    I wanted to reuse this page so I didn’t added any standard controller to the Page itself.

    Then now comes the step of creating the Tabs. Sample purpose I am taking Account’s views.

    Salesforce support following three types of tab creation:

    1. Custom Object Tabs: – Not suitable to above approach.
    2. VisualForce Tabs: – This will work but do not allow passing the parameters from the URL, so this will not allow reuse of the same page.
    3. Web Tabs: – This allows any URL integration into the tab and suits requirement.
    Complex task is to find the viewId. I didnt found it straight forward.
    Creating the Tab now:
    Now add this tab to your SelectedTabs.
    AS ITS A WEB TAB I WAS NOT ABLE TO SET SELECTED TAB. ANY WORK AROUND?