In Salesforce, Triggers are known as Apex Triggers. The triggers are unique and are accessible, particularly for general and anticipated actions such as lead conversions. Trigger in the Salesforce is the Apex code that we use to perform the operation before and after we report the record. In this article, we will study different kinds of Trigger scenarios in Salesforce.
If you would like to become a Salesforce certified professional, then visit Mindmajix - A Global online training platform: “ Salesforce Certification Training” Course. This course will help you to achieve excellence in this domain.
1. As per the execution order, we have two types of triggers: 1) before trigger 2) after the trigger.
While using this event, first, we will execute the code block, and after that, we insert the record.
2.We use the Before trigger to perform the logic on the same object.
3.We use the “before event” trigger if we have to update the same record in the trigger.
4.In the Before trigger, the records are not committed to the database. Therefore, we skip the DML.
5.When the record is committed to the database, i.e. record Id is created, and our trigger records in read-only mode. We cannot update the values.
[Related Article: "How to create Sample Apex Triggers in Salesforce"]
Keywords of Trigger Syntax:
Trigger name: It is the name that we have to give to our trigger.
Object name: It is the object over which we have to perform the action.
Trigger_events: They are the events list separated with commas.
Create “Sales Rep” with data type on Account object. While we create account records, account owners will be systematically inserted into the sales representative field. While we update the record account owner, the sales representative will be systematically updated.
In the upper trigger scenario, we have to start the trigger prior to the account creation, so we use the “before insert” trigger over the Account object. Likewise, according to the requirement, for the update event, therefore, we update the “before update” trigger over the Account object.
We use “set” for storing the “Ids” because we don’t require duplicate Ids of the account owner. So, we create an instance for that like the “SetAccowner”.
After that, we use the “for” loop to all the new accounts; therefore, we use “trigger. new” in the loop. In the “for” loop, we add the account “OwnerId” in the set.
After that, we create the instance of the map “user map” that stores “value = user” and “Key = Id” along with the query. SELECT Name FROM user WHERE Id IN: setAccowner
After that, in the following “for” loop, we create the instance “usr” for the user, which is equivalent to the Account “owner” that we get from “map”.
In the end, the sales representative “sales_rep_c” is equivalent to “usr. Name”
Create the field with the name “Contact Relationship” checkbox over the contact object and generate an object known as “Contact Relationship” that is associated with contacts. Develop a logic while we create a connection by verifying the contact relationship checkbox; after that contact, the relationship is established systematically for the contact. Trigger:
According to the requirement, we perform the operation over the trigger when a user submits a record which means we have to use the “after insert” trigger. After that, when we have to check about an event, therefore we use “trigger. insert” and “trigger.isAfter” to dismiss trigger on these specific events.
After that, we create an instance of the apex class “ContactMasterHandler” as “ConIns”. After that, we call a “callContactRelationshipByContact” method from the apex class.
Let us see an apex method, “createContactRelationshipByContact”, in which we pass contact list in the IstCon instance.
After that, we create a instance of the List of Contact_Relationship_c as “conlist” that stores “Contact_Relationship_c” records.
Then, we create the “for” loop for all the recently created contacts in the salesforce.
After that, “newcon.Contact_Relationship_c” is a field from the created contact.
After that, we create the instance for the “Contact_Relationship_c” object like “CR”.
After creating the record for the “CR”, we give the name the same as the “LastName” of the newly established contact. Contact is equivalent to the id of the newly established contact.
After that, we add “CR” in the “ConList”, and by “Inserting”, we insert the “Contact_Relationship_c” record in the salesforce.
When we modify contact relationship owner, then owner name is systematically inhabited in contact relationship; after that owner, the name is systematically inhabited in the Name field of contact relationship.
According to the requirement, before carrying out the update task, we will do some modifications to the “Contact_Relationship_c” object. Therefore, we create the trigger for the “before update”.
After that, we check for a specific operation to launch the trigger. Therefore, we check whether the event is “trigger.isUpdate” or “trigger.isBefore”. After that, we simply call the apex class method by generating the instance of the class “conReload”.
In the method, we create the “Map” with the “map_id_name” instance that comprises value =String together with the “Set” with the “IdSet” Instance, which includes the set of the Ids.
After that, in the first “for” loop, we fix “ListRecs.ownerId” i.e. ownerId of the contact relationship record.
After that, we retrieve users from the query and save their “Name” and “Id” on the Map.
After that, we will have the null check whether a user exists in the list and go inside the “for” loop. If the ownerId of “Contact_Relationship_c” is not null, then the contact relationship name is the user’s name.
In Salesforce, we use triggers for performing some actions or tasks. Triggers act as the apex code used to carry out a task. By using the trigger scenarios, we can systematically add the opportunity or account owner to the sales and account team. I hope this article provides you with the required information about trigger scenarios in salesforce.
|Name||Viswanath V S|
Viswanath is a passionate content writer of Mindmajix. He has expertise in Trending Domains like Data Science, Artificial Intelligence, Machine Learning, Blockchain, etc. His articles help the learners to get insights about the Domain. You can reach him on Linkedin