Write a Apex trigger to Add the Contact First Name and Last Name to Associated Account Custom Field Whenever the Contact inserted or Updated in Salesforce | Trigger to update Associated Account Custom Field

6,289 views

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

Add the Contact First Name and Last Name to Account Custom Field -- w3web.net

 

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]

  1.  TRIGGER ContactAccountTrigger ON Contact (BEFORE INSERT, BEFORE UPDATE, after INSERT, after UPDATE) {  
  2.     List<Contact> conList =NEW List<Contact>();
  3.     Set<Id> setid = NEW  Set<Id>();
  4.  
  5.     IF(TRIGGER.isBefore){
  6.         system.debug('trigger before event');
  7.         conList = TRIGGER.new;
  8.  
  9.     }ELSE IF(TRIGGER.isAfter){
  10.          conList=TRIGGER.new;
  11.         FOR(Contact con:conList){
  12.            setid.add(con.AccountId); 
  13.         }
  14.         system.debug('setid ' + setid);
  15.  
  16.         List<Account> accList = [SELECT Id, Name, updateContact__c FROM Account  WHERE Id=:setid];       
  17.  
  18.         IF(TRIGGER.isInsert){                     
  19.  
  20.              FOR(Contact c1:TRIGGER.new){
  21.                  FOR(Account a1:accList){
  22.                      a1.updateContact__c= c1.FirstName + ' ' + c1.LastName;
  23.                      UPDATE a1;
  24.                  }   
  25.               }
  26.  
  27.  
  28.         }ELSE IF(TRIGGER.isUpdate){
  29.             FOR(Contact c2:TRIGGER.new){
  30.                 FOR(Account a2:accList){
  31.                     a2.updateContact__c= c2.FirstName + ' ' + c2.LastName;
  32.                     UPDATE a2;
  33.                 }   
  34.             }             
  35.         }        
  36.  
  37.     }
  38. }

  

Further post that would you like to learn

 

 

FAQ (Frequently Asked Questions)

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 β†’
 
 


Hi, This is Vijay Kumar behind the admin and founder of w3web.net. I am a senior software developer and working in MNC company from more than 8 years. I am great fan of technology, configuration, customization & development. Apart of this, I love to write about Blogging in spare time, Working on Mobile & Web application development, Salesforce lightning, Salesforce LWC and Salesforce Integration development in full time. [Read full bio] | | The Sitemap where you can find all published post on w3web.net

Leave a Comment