Learn Salesforce Trigger in a Simple way -coderinme

We all are aware of the triggers that we have studied in MySQL which are used to control or trigger any action, same as these we have Salesforce triggers which are used for the various purpose and for various events and we are going to Learn Salesforce Trigger in a Simple way.

Learn Salesforce Trigger in a Simple way

Before going to the detailed use of it lets have the basic knowledge of it that what is Salesforce Triggers, how they can help us,  how to operate them and many more things.

So Basically, Salesforce Triggers is a bunch of code which is executed on the basis of various events and condition, results in the updation, insertion or deletion of record or records.

These triggers are worked upon various events like:

  • Insert: This will run on Before & After condition.
  • Update: This will run on Before & After condition.
  • Delete: This will run on Before condition only.
  • Upsert: This will run on Before & After only.
  • Undelete: This will run on After only.

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.

 

Syntax:

trigger trigger_name on object_name (events){

——-trigger body——-

}

 

These are the details of the triggers, now understand the trigger with the help of an example:

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');
       }

}

 

Explanation:

  • In the first line, the trigger is working on Account object of Salesforce and event as before insert, before update.
  • Then a for loop is running which is fetching all the new records from trigger.new in Account variable ‘ac’.
  • After that, it checks the if condition i.e. if account type is ” Prospect” then this account will become active otherwise it is Inactive.
  • Then in the next if condition it will check if the account type is Active but Active field is not “Yes” then it will throw an Error otherwise the else will work.

 

This is all about the basics of the trigger with a simple Trigger example and its explanation.We’ll discuss the triggers in a more complex way in the next post.

 

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

 

To watch the video online please check this video:

 

 

 

 

 

A Salesforce Developer at AlmaMate Info Tech PVT LTD. An Aligarian and also your query solver in database field.This site mark the difference as Black or White to make you choose the best for you. Don't feel pressurized, feel confident..!!

Leave a reply:

Your email address will not be published.