Salesforce Interview Question – Part 14

Author posted by Jitendra on Posted on under category Categories Apex, Salesforce and tagged as Tags with 1 Comment on Salesforce Interview Question – Part 14

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”

Salesforce Interview Question – Part 13


121 : Consider we have overall 90% code coverage however there is one class which have 0% code coverage. Can we still able to deploy that class on production?
Ans : Yes. Minimum 1% required for every trigger and there is no such restriction for Apex class.


122 : How to get selected records ID from List View using Javascript / Ajax Toolkit, when custom button is added on List View page?
Ans : Create a new Button on Lead of type List Button. Add the button on Lead List View Layout and write below Javascript code:

{!RequireScript("/js/functions.js")}

var recordsSelected = {!GetRecordIds($ObjectType.Lead)}
for(var i=0; i < recordsSelected .length ; i++) {
     alert('Selected ID '+recordsSelected[i]);
}

123 : In Ajax toolkit for custom Javascript button, you have to explicitly login to API because global Session variable is not available. In that case it is security vulnerable because anybody logged in can see the javascript code and your username and password. So is there any way to avoid this?
Ans: We can create a visualforce page with output type as JavaScript. Global session variable is available in VF page. Initialize the global javascript variable in that VF page. include VF page as a javascript file and we are done!


124 : In Custom Component How we can return value to Custom Controller or Controller Extension?
Ans: In Apex, Objects are passed by reference (read this article to understand Pass by Value and Pass by reference in Salesforce and also read this Salesforce blog article). So supply an argument of wrapper class (object) type to custom component. If its value is changed in Custom component we will get updated value in controller also. Continue reading “Salesforce Interview Question – Part 13”

Salesforce Interview Questions – Part 11

101. How to force lead assignment rule via Apex while updating or adding the Lead?
Ans : To enforce Assignment Rules in Apex you will need to perform following steps:

  1. Instantiate the “Database.DMLOptions” class.
  2. Set the “useDefaultRule” property of “assignmentRuleHeader” to True.
  3. Finally call a native method on your Lead called “setOptions”, with the Database.DMLOptions instance as the argument.

Example:

// to turn ON the Assignment Rules in Apex
Database.DMLOptions dmlOptn = new Database.DMLOptions();
dmlOptn.assignmentRuleHeader.useDefaultRule = true;
leadObj.setOptions(dmlOptn);

102. How to implement the pagination in SOQL ?

Ans:
In spring 12, Salesforce has come up with ability of SOQL to get records from position “X” instead of position “1” every time to help creating pagination feature.

Pagination in SOQL using keyword Offset
Pagination in SOQL using keyword Offset

Example:

Select Id, Name from Lead LIMIT 5 OFFSET 2

Above query will return 5 Lead records starting from record number 10 (5×2).

Read more here about SOQL pagination Continue reading “Salesforce Interview Questions – Part 11”