Hey guys, today in this post we are going to learn about How to Write a Apex trigger to Add the Contact First Name and Last Name to Account Custom Field Whenever the Contact inserted or Updated in Salesforce.
➡ Real time scenarios:- Write a trigger on Contact and Add First Name and Last Name of Contact to Associated Account Custom Field Whenever Contact record is Inserted or Updated.
Files we used in this post example
ContactAccountTrigger.apxt | Apex Class Trigger | It will be fired whenever Contact Record is inserted Or updated |
Parent Object:- Account |
Parent Object (Account) | Trigger on Contact and updae the Associted Account Custom Field (updateContact__c) |
Child Object:- Contact |
Child Object (Contact) | It is child object and associated with Account Object. |
Final Output
You can download file directly from github by Click Here.
Other related post that would you like to learn in LWC
Create Apex Class Trigger Controller
Step 1:- Create Apex Class : ContactAccountTrigger.apxt
From Developer Console >> File >> New >> Apex Class
ContactAccountTrigger.apxt [Apex Class Controller]
TRIGGER ContactAccountTrigger ON Contact (BEFORE INSERT, BEFORE UPDATE, after INSERT, after UPDATE) {
List<Contact> conList =NEW List<Contact>();
Set<Id> setid = NEW Set<Id>();
IF(TRIGGER.isBefore){
system.debug('trigger before event');
conList = TRIGGER.new;
}ELSE IF(TRIGGER.isAfter){
conList=TRIGGER.new;
FOR(Contact con:conList){
setid.add(con.AccountId);
}
system.debug('setid ' + setid);
List<Account> accList = [SELECT Id, Name, updateContact__c FROM Account WHERE Id=:setid];
IF(TRIGGER.isInsert){
FOR(Contact c1:TRIGGER.new){
FOR(Account a1:accList){
a1.updateContact__c= c1.FirstName + ' ' + c1.LastName;
UPDATE a1;
}
}
}ELSE IF(TRIGGER.isUpdate){
FOR(Contact c2:TRIGGER.new){
FOR(Account a2:accList){
a2.updateContact__c= c2.FirstName + ' ' + c2.LastName;
UPDATE a2;
}
}
}
}
}
Further post that would you like to learn
What are triggers in Apex?
Apex triggers enable you to perform custom actions before or after changes to Salesforce records, such as insertions, updates, or deletions. A trigger is Apex code that executes before or after the following types of operations: insert. update. delete.
Can we use trigger new in before insert?
Before insert: When using this event, the code block is executed before a new record is inserted. Before update: When you use this event, the code gets executed before a new record is updated in the object. Before delete: When you're using this event, the record gets deleted before the execution of the code block.
What are bulk triggers?
All triggers are bulk triggers by default, and can process multiple records at a time. You should always plan on processing more than one record at a time. An Event object that is defined as recurring is not processed in bulk for insert , delete , or update triggers.
Related Topics | You May Also Like
Our Free Courses →
👉 Get Free Course →
📌 Salesforce Administrators 📌 Salesforce Lightning Flow Builder 📌 Salesforce Record Trigger Flow Builder |
👉 Get Free Course →
📌 Aura Lightning Framework 📌 Lightning Web Component (LWC) 📌 Rest APIs Integration |