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: " Online Salesforce Course ". These courses will help you to achieve excellence in this Domain |
1. As per the execution order, we have two types of triggers: 1) before the trigger and 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.
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 by commas.
Related Article: How to create Sample Apex Triggers in Salesforce |
Create “Sales Rep” with data type on the 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.
Step 2
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”.
Step 3
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.
Step 4
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
Step 5
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”.
Step 6
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:
Related Article: Salesforce Interview Questions |
Step 1
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 the trigger on these specific events.
Step 2
After that, we create an instance of the apex class “ContactMasterHandler” as “ConIns”. After that, we call a “callContactRelationshipByContact” method from the apex class.
Step 3
Let us see an apex method, “createContactRelationshipByContact”, in which we pass the contact list in the IstCon instance.
Step 4
After that, we create an instance of the List of Contact_Relationship_c as “conlist” that stores “Contact_Relationship_c” records.
Step 5
Then, we create the “for” loop for all the recently created contacts in the salesforce.
Step 6
After that, “newcon.Contact_Relationship_c” is a field from the created contact.
Step 7
After that, we create the instance for the “Contact_Relationship_c” object like “CR”.
Step 8
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.
Related Article: Salesforce Marketing Cloud Training |
Step 9
After that, we add “CR” in the “ConList”, and by “Inserting”, we insert the “Contact_Relationship_c” record in the salesforce.
When we modify the contact relationship owner, then the owner name is systematically inhabited in the contact relationship; after that owner, the name is systematically inhabited in the Name field of the contact relationship.
Trigger
Step 1
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”.
Step 2
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”.
Step 3
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.
Step 4
After that, in the first “for” loop, we fix “ListRecs.ownerId” i.e. ownerId of the contact relationship record.
Step 5
After that, we retrieve users from the query and save their “Name” and “Id” on the Map.
Step 6
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.
Our work-support plans provide precise options as per your project tasks. Whether you are a newbie or an experienced professional seeking assistance in completing project tasks, we are here with the following plans to meet your custom needs:
Name | Dates | |
---|---|---|
Salesforce Training | Dec 24 to Jan 08 | View Details |
Salesforce Training | Dec 28 to Jan 12 | View Details |
Salesforce Training | Dec 31 to Jan 15 | View Details |
Salesforce Training | Jan 04 to Jan 19 | View Details |
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