Deep Diving in Salesforce Triggers – Coderinme

Introduction to Salesforce Trigger

Before discussing Salesforce Trigger, it is necessary to understand what the trigger is? The meaning of the word trigger is to pull.In terms of database, Trigger is a set of statements that a system executes when there is a change or modification in Record.Similarly, Salesforce Trigger is piece of code that executes before and after any specific DML events occur to salesforce records like insertion,updation & deletion.

Salesforce Trigger

Let’s have a look on Syntax

TRIGGER <trigger_name> ON <Object_name> (TRIGGER events) {
// Implement the Logic Here
}

Here,TRIGGER specifies the standard keyword

  • <trigger_name> specifies the name of the the trigger(It’s good to follow camel case in this
  • <Object_name> specifies the name of the Sobject upon which you want to write trigger.
  • TRIGGER events specifies the event like before insert, after insert etc. that we will discuss in our next section/post

Prerequisites (Things you should know before)

  1. Basic knowledge about Salesforce Standard & Custom Objects & Fields
  2. Configurational Functionalities that salesforce provide like validation,workflow & Process rules
  3. Basic Knowledge About Apex
  4. Knowledge about SOQL used in Salesforce

When to use , When not to use salesforce trigger

  • Please remember if something is possible using workflow or process builder then we should not use trigger.
  • Trigger enable us to perform custom actions that are not possible through point and click functionalities of Salesforce.
    Please check this link (Less Coding More Freedom)
  • When the limits of workflow rules exceed like there is more than 50 workflow rules on a single object,trigger is feasible in this case.
  • Based on order of execution we will decide trigger is better to use or other customization for example we need a value of a field in a trigger which is being updated by a workflow.
    now workflow works after the execution of before and after triggers and because of that we won’t find that value of that field and our logic will not execute properly  For that purpose we will use trigger. for more info please refer following link (order of execution)

As we know Salesforce is based on cloud which means we have a great power to use , but with great power comes with great responsibility which are  Governer Limits which salesforce implies on each customization and configuration.

We have to take care of  limits in Trigger for example:

1. Limit on DML operations per transaction is 150
if a trigger contains more than 150 DML per transaction then governer limit will exceed and trigger will fail to execute.

2. Limit on SOQL per transaction
if a trigger contain more than 100 SOQL queries then in this case also, governer limit will exceed and trigger will fail to execute.

3. Limit on number of records which can be processed within a DML operation can be performed is 10,000

There are some context variables for the triggers :

Variable Usage
isExecuting Returns true if the current context for the Apex code is a trigger, not a Visualforce page, a Web service, or an executeanonymous() API call.
isInsert Returns true if this trigger was fired due to an insert operation, from the Salesforce user interface, Apex, or the API.
isUpdate Returns true if this trigger was fired due to an update operation, from the Salesforce user interface, Apex, or the API.
isDelete Returns true if this trigger was fired due to a delete operation, from the Salesforce user interface, Apex, or the API.
isBefore Returns true if this trigger was fired before any record was saved.
isAfter Returns true if this trigger was fired after all records were saved.
isUndelete Returns true if this trigger was fired after a record is recovered from the Recycle Bin (that is, after an undelete operation from the Salesforce user interface, Apex, or the API.)
new Returns a list of the new versions of the sObject records.

This sObject list is only available in insertupdate, and undelete triggers, and the records can only be modified in before triggers.

newMap A map of IDs to the new versions of the sObject records.

This map is only available in before updateafter insertafter update, and after undelete triggers.

old Returns a list of the old versions of the sObject records.

This sObject list is only available in update and delete triggers.

oldMap A map of IDs to the old versions of the sObject records.

This map is only available in update and delete triggers.

size The total number of records in a trigger invocation, both old and new.

Here is the look of trigger code.

trigger updateType on Account (before insert,before update) {
         for(Account ac:Trigger.new){
                if(ac.Type=='Prospect')
                    ac.Type__c='Active';
         else
                ac.Type__c='Inactive';
         if(ac.Type__c=='Active' && ac.Active__c!='Yes')
              ac.Active__c.addError('Active field should be yes');
         else if(ac.Type__c=='Inactive' && ac.Active__c!='No')
              ac.Active__c.addError('Active field should be No');
       }

}

In the next part of salesforce Trigger we will discuss the code, we will understand about the events , trigger design and it’s working , field update, validation, related record updation.

All rights reserved. No part of this Post may be copied, distributed, or transmitted in any form or by any means, without the prior written permission of the website admin, except in the case of brief quotations embodied in critical reviews and certain other noncommercial uses permitted by copyright law. For permission requests, write to the owner, addressed “Attention: Permissions Coordinator,” to the admin @coderinme

Leave a reply:

Your email address will not be published.