1

I am trying to update Account custom field Account_Case_Email__c from Case object Trigger whenever a new Case is being created , I want to concatenate Case custom field value to the account custom field.

trigger CaseEmailTrigger on Case (after insert,  after delete) {

    if(Trigger.isAfter && Trigger.isInsert){
        CaseHandler.handleAfterInsert(Trigger.New, Trigger.newMap);
    }

    if(Trigger.isAfter && Trigger.isDelete){
        
    }

}

Handler:

public with sharing class CaseHandler {

    public static void handleAfterInsert(List<Case> newCase, Map<Id, Case> caseMap) {

        Set<Id> caseId = Trigger.newMap.keySet();
        List<Case> allCase = new List<Case>();
        
        for (Case eachCase:[Select AccountId,Account.Account_Case_Email__c, Case_Email_Custom__c FROM Case Where Id IN : caseId]){
            eachCase.Account.Account_Case_Email__c = eachCase.Account.Account_Case_Email__c+', '+eachCase.Case_Email_Custom__c;
            System.debug(eachCase.Account.Account_Case_Email__c);
            allCase.add(eachCase);
        }
        update allCase;
    }
2
  • Is there any error you are facing? and If there is multiple cases on a given Account... every time you create a Case that field on Account will be overriden by this trigger. And if its one Case per Account, you can also create a Formula field instead of a trigger or you can update using a workflow/flow/process builder Mar 23, 2022 at 13:03
  • NO I am not facing any error. In Log I am getting value from Account Email and Case Email both, but its not getting concat and not updating over Account record. I am considering a situation for multiple cases on account updating and all value from cases shoould be concatenated and updated to Account custom field Account_Case_Email__c.
    – yRohit
    Mar 23, 2022 at 13:12

1 Answer 1

2

Setting the issue of continually appending to a string aside...

When you insert/update an object, it is only the data on that object that is inserted or updated. Any data on related objects is ignored.

So when you update allCases;, the update is only being applied to the data on the Case records themselves. The change you're making to the related accounts are discarded.

The quick fix for this one is to change List<Case> allCase = new List<Case>(); to instead be List<Account> relatedAccounts = new List<Account>();. In your loop, you would then need to add items to this list (similar to how you're adding cases now). relatedAccounts.add(eachCase.Account);

eachCase.Account is a fully-fledged instance of the Account SObject (and custom relationships like My_Custom_Lookup__r are also fully-fledged instances of their respective SObjects).

That said

There are other improvements you could make to this trigger handler (you could get rid of the query entirely if you were to add a formula field to pull in that data from the related Account, as you only need a record Id to update a record).

But really, the use case I'm imagining for what you're trying to accomplish is probably better served by simply including this Case_Email_Custom__c field in the "Case" related list (which you can alter by editing the Account page layout(s)).

1
  • Thanks @Derek for clarification. :-)
    – yRohit
    Mar 23, 2022 at 13:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .