Trigger on contact to update a account custom field | Write a trigger on contact and update the account custom field uses of apex trigger in Salesforce

1,859 views


Hey guys, today in this post we are going to learn about How to update the FirstName & LastName to the account on custom field contactSummary (Long Text Area) using trigger in Salesforce.

Account Custom Fields & Relationships

Create a custom field on Account name as contactSummary__c (Long Text Area).

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
   

Real time scenarios:- Write a trigger on contact and update the FirstName & LastName to the account on custom field contactSummary (Long Text Area) whenever contact record is only updated.

Trigger fired whenever contact record is only updated.

Standard object:- Account >> New custom field:- contactSummary__c (Long Text Area)

Standard object:- Contact >> Standard fields:- FirstName, LastName

Files we used in this post example:-

contactTriggerRollSummary.apxt Apex Class Trigger It will be fired whenever contact record is updated

Parent Object:- Account

Custom Field :- contactSummary__c

Parent Object with Custom Field Trigger on contact and update the FirstName and Lastname to the account on custom field contactSummary (Long Text Area).

Child Object:- Contact

Standard Field:- FirstName, LastName

Child Object with Field

Contact FirstName and LastName will be updated to account whenever record is edit and save.

You can download file directly from github by click here

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
   

Final Output

w3web.net

 

You can download file directly from github by Click Here.

 
 

Other related post that would you like to learn in Salesforce

Step 1:- Apex Class Trigger : contactTriggerRollSummary.apxt

From Developer Console >> File >> New >> Apex Trigger

contactTriggerRollSummary.apxt [Apex Class Trigger]

  1.   TRIGGER contactTriggerRollSummary ON Contact (BEFORE INSERT, after INSERT, BEFORE UPDATE, after UPDATE) {
  2.  
  3.     List<Contact> contactList= NEW List<Contact>();
  4.     Set<Id> accountIdSet = NEW Set<Id>(); 
  5.  
  6.    IF(TRIGGER.isBefore){         
  7.         system.debug('trigger before event');
  8.         IF(TRIGGER.isUpdate){ 
  9.             contactList=TRIGGER.new;            
  10.         }
  11.  
  12.  
  13.     }ELSE IF(TRIGGER.isAfter){        
  14.         system.debug('trigger after event');   
  15.         IF(TRIGGER.isDelete){
  16.             contactList= TRIGGER.old;
  17.             system.debug('contactListOld ' + contactList);
  18.         }ELSE{
  19.             contactList=TRIGGER.new; 
  20.             system.debug('contactListNew ' + contactList); 
  21.         }          
  22.  
  23.         FOR(Contact c1:contactList){
  24.             IF(c1.AccountId !=NULL){
  25.                 accountIdSet.add(c1.AccountId);
  26.             } 
  27.             IF(TRIGGER.isUpdate){
  28.                 Contact oldContact = (Contact)TRIGGER.oldMap.get(c1.Id);                 
  29.                 IF(oldContact.AccountId != c1.AccountId){
  30.                     accountIdSet.add(oldContact.AccountId);
  31.                 }                                
  32.  
  33.                 List<Account> accObjList = NEW List<Account>();
  34.                    FOR(Account parentSetObj: [SELECT Id, Name, contactSummary__c, (SELECT Id, FirstName, LastName FROM Contacts) FROM Account WHERE Id IN:accountIdSet])
  35.                    {
  36.                        List<Contact> childObj = parentSetObj.Contacts;
  37.                        String conStr = '';
  38.                        FOR(Contact con:childObj){
  39.                            conStr = conStr + con.FirstName + ' ' + con.LastName + '\n';
  40.                        }
  41.                         parentSetObj.contactSummary__c = conStr;
  42.                         accObjList.add(parentSetObj);
  43.                     }
  44.                     UPDATE accObjList;               
  45.  
  46.             }             
  47.         }
  48.   }
  49. }

Further post that would you like to learn in Salesforce

 


 

FAQ (Frequently Asked Questions)

What is before trigger and after trigger in Salesforce?

Before triggers are used to update or validate record values before they're saved to the database. After triggers are used to access field values that are set by the system (such as a record's Id or LastModifiedDate field), and to affect changes in other records.

What is before insert trigger in Salesforce?

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.

Can I update the same record in after trigger context?

So yes, you can update records in an after trigger - but you need to give it some thought and make sure it's the right thing to do.

Related Topics | You May Also Like

 
Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 
  

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



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