Hey guys, today in this post we are going to learn about How to Write a trigger to update parent account phone number when ever the contact phone number is updated using trigger handler and helper class in Salesforce
Real time scenarios:- Write a trigger on Contact to update the parent Account Phone number when ever the Contact Phone is updated through trigger handler and helper class in Salesforce.
- Don’t forget to check out:- Trigger to check duplicate name to custom object in Salesforce Click Here For More Information
Files we used in this post example
contactTrigger.apxt | Apex Class Trigger | It will be fired when ever contact phone number is updated. |
contactTriggerHandler.apxc | Apex Class Trigger Handler | Apex handler trigger to update account phone number with contact phone number when ever contact is updated. |
Parent Object:- Account Fields:- Phone Child Object:- Contact Fields:- Phone, AccountId (Lookup) |
Trigger to updated parent Account from Contact based on lookup relation AccountId. |
Final Output
Other related post that would you like to learn in Salesforce
- Find the below steps for this post.
Create Apex Class Trigger
Step 1:- Create Apex Class : contactTrigger.apxt
From Developer Console ➡ File ➡ New ➡ Apex Class
contactTrigger.apxt [Apex Class Controller]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
trigger contactTrigger on Contact (before insert, before update, before delete, after insert, after update, after delete, after undelete) { if(trigger.isBefore ){ system.debug('I am before trigger '); } else if(trigger.isAfter){ system.debug('I am after trigger '); if(trigger.isUpdate){ contactTriggerHandler.afterUpdateHelper(trigger.new); } } } |
Create Apex Trigger Handler and Helper Class
Step 2:- Create Apex Class : contactTriggerHandler.apxc
From Developer Console ➡ File ➡ New ➡ Apex Class
contactTriggerHandler.apxc [Apex Class Controller]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
public class contactTriggerHandler { public static void afterUpdateHelper(List<Contact> conList){ Set<Id> setId = new Set<Id>(); for(Contact con:conList){ setId.add(con.AccountId); } system.debug('setId ' + setId); List<Account> accItemList = [Select Id, Name, Phone, (Select Id, FirstName, LastName, Phone, AccountId From Contacts) From Account Where Id IN:setId]; for(Account a1:accItemList){ for(Contact c1:a1.Contacts){ if(c1.Phone !=null){ a1.Phone = c1.Phone; update accItemList; } } } } } |
Further post that would you like to learn in Salesforce
Check Out Youtube:- Earn Unlimited Everyday With This Trick:- Click Here
What is trigger in Salesforce?
A trigger is an Apex script that executes before or after data manipulation language (DML) events occur.
Why do we use triggers in Salesforce?
Typically, we use triggers to perform operations based on specific conditions, to modify related records or restrict certain operations from happening.
What are different events in trigger?
The events that fire a trigger include the following: DML statements that modify data in a table ( INSERT , UPDATE , or DELETE ) DDL statements.
Related Topics | You May Also Like
- Your reaction of the article ▾
Nice post, really helpful for me
nice post…
it’s Such a great talk. now want to share me and my mom story::When I was 13 I used to ask myself, ‘What am I going to do with my life? To find the answer to that question, I wrote the first dream list of my life. Fortunately, the first on the list is to live in all continents and the second is to travel to the wonderful places with my mom. I was young then, and perhaps this is the second time I wrote about it because my mother thought of it as the best thing in the world. Now that I gain a lot of experience after traveling, it just shows that my mother is my role model.
Nice Post
Thank a lot for sharing this awesome info. I personally like the last point and follow me in my blog. I love to share on social media. keep it up!!!