Part 4: Salesforce Inteview Questions( Trigger)

31. What is the difference between trigger.old and trigger.new?

Trigger.old contains lists of old records(old field values) and Trigger.new contains list of new records(new field values). Ex: If we have written a trigger on Account, trigger.new contains list of new Account records and trigger.old contains list of old Account records.

32. What is the difference between trigger.oldmap and trigger.newmap?

Trigger.oldmap contains map of oldSobjectrecords with key as Id’s and Trigger.newmap contains map of  Id’s of sobjectrecords.

Note: Trigger.oldmap is only available in update and delete Triggers. Trigger.newmap is only available in before updateafter insertafter update, and after undelete triggers.

33. What are the different types of events in the trigger?

A trigger can be invoked with 7 events

Before insert, Before Update, After insert, After update, Before delete,After delete,After undelete

34. Explain best practices in Trigger?

We should write one trigger per object by defining all events. We should call a class from trigger to handle processing logic so that we can control the execution. Optionally we could also write event-based triggers per object but maintaining this would be difficult. Since we have 7 events we can have 7 triggers per object.

35. What is the default size of records that a trigger can process?

By default, the processing size of trigger is 200. But there are exceptions to this rule. For example: If we are loading records through data loader and you have changed the record size to 500 then the trigger will fire for 500 records at a time.

36. What are the operations that don’t invoke triggers?

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_ignoring_operations.htm

37. What is bulkification?

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_bulk_idioms.htm

38. Can I perform DML Operations on trigger.new and trigger.old context variable?

No, you cannot perform DML Operations on trigger.new and trigger.old context Variable.

39. Explain Sample trigger code with different trigger events?

trigger myAccountTrigger on Account(before delete, before insert, before update, 
                                    after delete, after insert, after update) {
if (Trigger.isBefore) {
    if (Trigger.isDelete) {

        // In a before delete trigger, the trigger accesses the records that will be
        // deleted with the Trigger.old list.
        for (Account a : Trigger.old) {
            if (a.name != 'okToDelete') {
                a.addError('You can\'t delete this record!');
            } 
        }
    } else {

    // In before insert or before update triggers, the trigger accesses the new records
    // with the Trigger.new list.
        for (Account a : Trigger.new) {
            if (a.name == 'bad') {
                a.name.addError('Bad name');
            }
    }
    if (Trigger.isInsert) {
        for (Account a : Trigger.new) {
            System.assertEquals('xxx', a.accountNumber); 
            System.assertEquals('industry', a.industry); 
            System.assertEquals(100, a.numberofemployees);
            System.assertEquals(100.0, a.annualrevenue);
            a.accountNumber = 'yyy';
        }

// If the trigger is not a before trigger, it must be an after trigger.
} else {
    if (Trigger.isInsert) {
        List<Contact> contacts = new List<Contact>();
        for (Account a : Trigger.new) {        
            if(a.Name == 'makeContact') {
                contacts.add(new Contact (LastName = a.Name,
                                          AccountId = a.Id));
            }
        } 
      insert contacts;
    }
  }
}}}

 

sfdclightning

Related post