Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trigger on contact to update a account custom field #3

Open
vijayk3327 opened this issue Aug 27, 2023 · 0 comments
Open

Trigger on contact to update a account custom field #3

vijayk3327 opened this issue Aug 27, 2023 · 0 comments
Assignees
Labels
documentation Improvements or additions to documentation question Further information is requested

Comments

@vijayk3327
Copy link
Owner

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).

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)

👉 To get source code live demo link, Click Here.

Step 1:- Apex Class Trigger : contactTriggerRollSummary.apxt

` TRIGGER contactTriggerRollSummary ON Contact (BEFORE INSERT, after INSERT, BEFORE UPDATE, after UPDATE) {

List<Contact> contactList= NEW List<Contact>();
Set<Id> accountIdSet = NEW Set<Id>(); 

IF(TRIGGER.isBefore){
system.debug('trigger before event');
IF(TRIGGER.isUpdate){
contactList=TRIGGER.new;
}

}ELSE IF(TRIGGER.isAfter){        
    system.debug('trigger after event');   
    IF(TRIGGER.isDelete){
        contactList= TRIGGER.old;
        system.debug('contactListOld ' + contactList);
    }ELSE{
        contactList=TRIGGER.new; 
        system.debug('contactListNew ' + contactList); 
    }          

    FOR(Contact c1:contactList){
        IF(c1.AccountId !=NULL){
            accountIdSet.add(c1.AccountId);
        } 
        IF(TRIGGER.isUpdate){
            Contact oldContact = (Contact)TRIGGER.oldMap.get(c1.Id);                 
            IF(oldContact.AccountId != c1.AccountId){
                accountIdSet.add(oldContact.AccountId);
            }                                

            List<Account> accObjList = NEW List<Account>();
               FOR(Account parentSetObj: [SELECT Id, Name, contactSummary__c, (SELECT Id, FirstName, LastName FROM Contacts) FROM Account WHERE Id IN:accountIdSet])
               {
                   List<Contact> childObj = parentSetObj.Contacts;
                   String conStr = '';
                   FOR(Contact con:childObj){
                       conStr = conStr + con.FirstName + ' ' + con.LastName + '\n';
                   }
                    parentSetObj.contactSummary__c = conStr;
                    accObjList.add(parentSetObj);
                }
                UPDATE accObjList;               

        }             
    }

}
}`

👉 To get source code live demo link, Click Here.

@vijayk3327 vijayk3327 added documentation Improvements or additions to documentation question Further information is requested labels Aug 27, 2023
@vijayk3327 vijayk3327 self-assigned this Aug 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation question Further information is requested
Projects
Status: No status
Development

No branches or pull requests

1 participant