Skip to main content

Posts

System.FinalException: Sobject rows does not allow errors:

Sometimes we need to show error message from trigger using add error method. This post is all about what are the challenge i faced to display error message. My requirement is showing error message if an opportunity which is not linked to contact are updated.As we know opportunitycontactrole is junction object which will relate both opportunity and contact.  So It is so simple, I went for Opportunity beofore update. trigger OpportunityBeforeUpdate on Opportunity (before update) {               OpportynitySequesncecontroller.beforeUpdate(); } public class OpportynitySequesncecontroller {       public void static beofeUpdate(){                     // others logic are there cos many operation might be there on before update               OpportynityManagement .validateOppty(trigger.newmap)       } } pub...

How to add list of records to a map?

We all know map is very much required in SFDC, As a developer we should be familiar with map uses. This post is all about to add list of records to a map. Map<Id,Account> account_map =  new Map<Id,Account>(); public static void addinglisttoMap(List<Account> accList){ for(Account acc:accList){ account_map.put(acc.id,acc); } }  Normally we used to do like above, where processing time will be more(CPU execution time will be more) because we are iterating over a for loop to construct a map.  I will tell you the best way to construct the map where we can avoid iteration.  public static void addinglisttoMap(List<Account> accList){ Map<Id,Account> account_map =  new Map<Id,Account>(accList); } Advantages-  Processing time will be very less No of character in apex class is also less.  Hope it will be helpful.  Keep coding and exploring :)  

Order Of Execution In Salesforce

How to convert any standard time to PST ?

As we already know based on user local and company info date time will be displayed by default. However we need to convert date time from one format to another. Below is some code snippet for this . DateTime CurentTime = system.now(); String TimeZones = '(GMT-07:00) Pacific Daylight Time (America/Los_Angeles)'; List<String> lststrsplit = TimeZones.substring(12,TimeZones.length()).split('\\(',2); System.debug('Printing--lststrsplit-'+lststrsplit); string strTimeZone = lststrsplit[1].substring(0,lststrsplit[1].length()-1); System.debug('Printing--strTimeZone-'+strTimeZone); system.debug(CurentTime+'abc#'); string strCurrentTime = CurentTime.format('YYYY-MM-dd HH:mm:ss', strTimeZone); Datetime dateCurrentTime = Datetime.valueof(strCurrentTime); System.debug('Printing--dateCurrentTime-'+dateCurrentTime);

How to Parse date in SFDC?

Date format used to vary from system to system. In SFDC it is like YYYY-MM-DD -2016-03-10 00:00:00 Lets say date format is DD/MM/YYYY in other system, how will we store such date foramt ? String s = '15/08/2014'; date dt = Date.Parse(s); System.debug('Printing--today date -'+system.today()); System.debug('Printing---'+dt); Out put will be 11:25:10.11 (12471791)|USER_DEBUG|[3]|DEBUG|Printing--today date -2016-03-10 00:00:00 11:25:10.11 (12560905)|USER_DEBUG|[5]|DEBUG|Printing---2014-08-15 00:00:00

How to avoid this error while running test class "FIELD_CUSTOM_VALIDATION_EXCEPTION, Attempt to de-reference a null object: []"

This post is all about how efficiently custom settings can be used in the class so that we can avoid null pointer exception while running test class with seealldata is false. As per the best practice concern, we should not use SeeAlldata is true in all the test class.If we wont use that attribute then we need to create all the test data(custom setting record and object record ) in the test class. We might not aware of all the custom setting which will be required to be inserted because they will be refereed in the trigger of some other object which is not really required to be executed. Normally we use custom setting like below    Boolean ischecked = customsettingname__c.getInstance('RecordName').IsChecked__c ; If we wont insert record name as "RecordName" in the custom setting customsettingname__c in test class , then we will come across this issue   F IELD_CUSTOM_VALIDATION_EXCEPTION, Attempt to de-reference a null object: []" To avoid such issue we ...