Skip to main content

Posts

How to format Date and Date time in Apex class ?

Hi All, We might need to display date in some other format as per requirement. For example there might be an requirement to display created date as MONTH/DAY or time as 11: 00 PM or 9 :00 AM.   How will we do it ?  For Time format System.debug(Datetime.now(). format('hh:mm a') ); For Date format   System.debug('Today--'+Date.Today());  String daysMonth = String.valueOf(Date.Today().month()) + '/' + String.valueof(Date.Today().day());  System.debug('daysMonth --'+daysMonth ); 22:54:03.3 (3392679)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex 22:54:03.3 (3811485)|SYSTEM_METHOD_ENTRY|[1]|com.salesforce.api.interop.apex.bcl.DateMethods.today() 22:54:03.3 (3885055)|SYSTEM_METHOD_EXIT|[1]|com.salesforce.api.interop.apex.bcl.DateMethods.today() 22:54:03.3 (3920613)|SYSTEM_METHOD_ENTRY|[1]|String.valueOf(Object) 22:54:03.3 (3939253)|SYSTEM_METHOD_EXIT|[1]|String.valueOf(Object) 22:54:03.3 (4125004)|SYSTEM_METHOD_ENTRY|[1]|System.debug(AN...

How to calculate time spent or no of days between two day time value ?

Hi All, This post explains how to  calculate time spent and no of days between two date time value and issue faced while doing same and how to resolve those. First Try  Account acc = [select id,createddate,lastmodifieddate from Account limit 1]; datetime diff = acc.lastmodifieddate - acc.createddate; System.debug('diff --'+diff ); Error -  COMPILE ERROR: Date/time expressions must use Integer or Double or Decimal LINE: 3 COLUMN: 14  Second Try  Account acc = [select id,createddate,lastmodifieddate from Account limit 1]; Integer diff = Integer.valueOf(acc.lastmodifieddate) - Integer.valueOf(acc.createddate); System.debug('diff --'+diff ); Error EXCEPTION: System.TypeException: Invalid integer: java.util.GregorianCalendar[time=1455789326000,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="GMT",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=2,mi...

How to Lock/Unlock record thorough Apex code ?

Hi All, We all have faced many business scenario where we have to lock/unlock records. We had traditional approach to create separate record type and page layout to achieve it, but salesforce has given API to do thorough apex code. Please make sure below settings is enabled. Locking Record  1. Create a standard Approval process which will take care of locking records. http://salesforceworld4u.blogspot.com/2017/07/how-to-supress-standard-approval-email.html 2. Invoke Approval process   a. Using Process builder (Submit for Approval)   b. Use Apex code to invok from trigger or custom button.  /*     * This method will invoke the standard approval process     */     public static void submitForApproval(Case caseRecord)     {         // Create an approval...

How to resolve the java script error "Cannot read property execute of undefined"

Hi All, Debugging java script error is very tedious, if there is some still mistake then whole functionality will not work and it will not even tell line number of code where exactly that error is coming. Anyways let us say you have custom detail page button (Source code = java script) , you want to call apex class from that button code, you faced this error " Cannot read property execute of undefined " This is because you have missed to add  {!requireScript("/soap/ajax/26.0/apex.js")} in the java script code. {!REQUIRESCRIPT("/soap/ajax/23.0/connection.js")}  {!requireScript("/soap/ajax/26.0/apex.js")}  sforce.apex.execute("CaseApprovalController","unlockCaserecord",{caseid:'{!Case.Id}'});

How to display button on apex page message ?

Hi All, We might need to display button while showing page message to get user feed back. Based on user feedback necessary action need to be taken care. For example Below is the code snippet.  <apex:pageMessages escape="false"/>  <apex:outputPanel rendered="{!isYesVisible}">         <apex:pageMessage severity="info" strength="3" summary="{!applicantMessage}">                           <apex:commandButton value="Yes" action="{!cloneOpportunity}"/>                 <apex:commandButton value="No" action="{!stayinSamePage}"/>                   </apex:pageMessage>             </apex:outputPanel>

How to supress standard approval email in salesforce ?

Hi All, Sometimes we might need to suppress approval email from being sent out without affecting approval process. There are two way we can achieve it. 1. Creating Queue. Create empty queue record and use empty queue in approval process as approver. 2. Update user record Select Migration admin user as approver in the approval process. Change approver setting for migration admin user.

How to export detail record in word document ?

Hi All, We can do this using conga composer , in my earlier post I have explained this . http://salesforceworld4u.blogspot.com/2013/08/generating-pdf-using-conga-composer.html We will do in simple way/easier way. here we go 1. Create visaulforce page <apex:page standardController="Case" contentType="application/msWord#msword.doc" >     <apex:detail />   </apex:page> 2. Create custom button. Content source might be execute java script or link. /apex/GeneratingVFasWordDoc?id={!Case.Id}