Part 5: Salesforce Interview Questions (Apex)

41. What is Recursion and when does this issue occur?

Recursion is the process of executing the same piece of code infinite times. Eventually, this will result in hitting Governor limits in Salesforce. This issue occurs when we have an after Update Trigger event along the Process builder/Workflow field update acting on the Same field.

Example:If we have an After update trigger on Opportunity that is trying to Update a stage field and we also have a process builder/workflow on Stage field this will result in Recursion as the trigger update will fire the Process builder/workflow and process builder/workflow is again going to invoke trigger. This will keep on executing infinite times and eventually, the process results in hitting governor Limit.

42. How to avoid Recursion in Salesforce?

We can avoid recursion in Salesforce by using a static helper method. Read below for complete details of Recursion

Dev Tip2: How to Avoid Recursion in trigger/Salesforce

43.  What is DML in salesforce?

DML(Data Manipulation Language) in Salesforce provides the ability to manage records using insert, Update, Delete and Restore statements

44. What are different DML Statements in Salesforce?

insert,update,upsert,delete,undelete,merge

45. What is the difference between update and upsert?

update statement helps in updating the record based on recordId. Upsert DML statement creates the record if there is no existing record and if a record exists then it will update the record.

46. Will any records be deleted during merge process?

The merge statement merges up to three records of the same sObject type into one of the records, deleting the others, and re-parenting any related records.

47. What are different Database methods in Salesforce?

  • Database.insert()
  • Database.update()
  • Database.upsert()
  • Database.delete()
  • Database.undelete()
  • Database.merge()

48. What is difference between DML Statements and Database methods?

Unlike DML statements, Database methods have an optional allOrNone parameter that allows you to specify whether the operation should partially succeed. When this parameter is set to false, if errors occur on a partial set of records, the successful records will be committed and errors will be returned for the failed records.

Note: No exceptions are thrown with the partial success option.

49. Can i insert partial records in a Transaction?

Yes we can insert partial records in a transaction using Database method with allorNone parameter set to false.

Ex:  Database.SaveResult[] results = Database.insert(recordList, false);

50. Please provide a sample code that process partial records in salesforce?

Execute the below piece of code in Execute Anonymous window of Developer Console

// Create a list of contacts
List<Contact> conList = new List<Contact> {
        new Contact(FirstName='Joe',LastName='Smith',Department='Finance'),
        new Contact(FirstName='Kathy',LastName='Smith',Department='Technology'),
        new Contact(FirstName='Caroline',LastName='Roth',Department='Finance'),
        new Contact()};
            
// Bulk insert all contacts with one DML call
Database.SaveResult[] srList = Database.insert(conList, false);
// Iterate through each returned result
for (Database.SaveResult sr : srList) {
    if (sr.isSuccess()) {
        // Operation was successful, so get the ID of the record that was processed
        System.debug('Successfully inserted contact. Contact ID: ' + sr.getId());
    } else {
        // Operation failed, so get all errors
        for(Database.Error err : sr.getErrors()) {
            System.debug('The following error has occurred.');
            System.debug(err.getStatusCode() + ': ' + err.getMessage());
            System.debug('Contact fields that affected this error: ' + err.getFields());
	 }
    }
}

sfdclightning

Related post