131. What will happen if you try to update record in After Trigger Context?
Ans : You will get an error saying “record is Read only”.
132. Let’s say we have to update the same record in After Trigger context. Is there any way or workaround?
Ans : If we create a new instance of an SObject in the Apex Trigger in memory using the Id of the newly created record as provided in the After Trigger context, we can perform an Update DML statement and not get a read only error. This is because in Apex, the SObject is seen as a new reference (even though the records have the same SFDC ID) and therefore is eligible for DML operations. The below snippet of code illustrated this working and not working.
List<Contact> originals = new List<Contact>(); if(mirrorResultMap.values().size() > 0) { for(Contact origContact : contactRecs.values()) { Contact mirrorContact = mirrorResultMap.get(origContact.Id); //origContact.Linked_Contact__c = mirrorContact.Id; //Link the Original Record tot he Mirror Record WILL FAIL Contact origContactUpdate = new Contact(Id=origContact.Id, Linked_Contact__c = mirrorContact.Id); //This will WORK originals.add(origContactUpdate); } //update contactRecs.values(); //Update the Records -> THIS WILL FAIL AS ITS ORIGINAL RECORDS IN MEMORY update originals; }
Credit goes to Cory Cowgill for this Blog Entry.
133 . When loading data into date fields such as Opportunity Close Date using the Data Loader, the date displayed in the application is sometimes one day earlier than the date in the file. What may be the reason and solution ?
Ans :
The reason for this is that fields such as Close Date are actually date/time fields. When a date is loaded without specifying the time, the time is defaulted to 00:00 – midnight. When another user is in a time zone which is behind the current user’s time zone, the date will show on the previous day. For example:
20 August 2008 00:00 in Paris is 19 August 2008 23:00 in London
Similar issues can arise when daylight savings time begins or ends.
Two simple solutions to this are:
1) Specify a time as well as a date when loading dates using the Data Loader.
or
2) Switch your PC’s time zone to Hawaiian time before starting up the Data Loader. Continue reading “Salesforce Interview Question – Part 14”