+ Start a Discussion
Eric PohlabelEric Pohlabel 

Update Case Owner to a Value in an Account field

Decent admin - complete newbie when it comes to coding. I am attempting to solve my very first real live org problem using a trigger. This is going to be the first “non-tutorial” trigger I have ever written and it might be a bit more than I bargained for. Here is the scenario:
I have a lookup to User on the Account object “SRM__c”. When  SRM__c  is populated – I need to update the Case Owner to the User populated in SRM__c for every OPEN case that is related to the Account.
trigger updateRelatedSRMCaseOwner on Account (after insert, after update){
      for (Case c : Trigger new){
         if (c.isClosed = FALSE && NOT(ISBLANK(Account.SRM))){
             c.OwnerID = Account.SRM;
              update c;
         }
     }
}
I don’t think I can run the trigger on Account object…then have the FOR loop looking for CASES can I?
Then I think I need the UserID of the User that is populated in the Account.SRM field but I have no idea what the syntax for that should be. Im also not sure that I can use both the Case and Account objects in the same IF statement. HELP!!!!!
 
Best Answer chosen by Eric Pohlabel
UC InnovationUC Innovation
Hi Eric,

This seems like something process builder should be able to take care of. I would look into that before trying to write a trigger.

Having said that, the trigger should look something like the following:
trigger updateRelatedSRMCaseOwner on Account (after insert, after update){
    List<Case> caseListToUpdate = new List<Case>();
    
    List<Account> accountsWithCaseChildren = [SELECT SRM__c, (SELECT OwnerID, isClosed FROM Case__r) FROM Account WHERE id in: trigger.new];
    
    for (Account a : accountsWithCaseChildren){
        if (a.SRM__c != null)
        {
            for (Case c : a.Case__r)
            {
                if (c.isClosed = FALSE && NOT(ISBLANK(Account.SRM))){
                     c.OwnerID = a.SRM__c;
                     caseListToUpdate.add(c);
                 }
            }
        }
    }
    
    update caseListToUpdate;
}

 

All Answers

UC InnovationUC Innovation
Hi Eric,

This seems like something process builder should be able to take care of. I would look into that before trying to write a trigger.

Having said that, the trigger should look something like the following:
trigger updateRelatedSRMCaseOwner on Account (after insert, after update){
    List<Case> caseListToUpdate = new List<Case>();
    
    List<Account> accountsWithCaseChildren = [SELECT SRM__c, (SELECT OwnerID, isClosed FROM Case__r) FROM Account WHERE id in: trigger.new];
    
    for (Account a : accountsWithCaseChildren){
        if (a.SRM__c != null)
        {
            for (Case c : a.Case__r)
            {
                if (c.isClosed = FALSE && NOT(ISBLANK(Account.SRM))){
                     c.OwnerID = a.SRM__c;
                     caseListToUpdate.add(c);
                 }
            }
        }
    }
    
    update caseListToUpdate;
}

 
This was selected as the best answer
Eric PohlabelEric Pohlabel
I tried to use Process Builder but couldn't find a way to dynamically set the case owner based on the value of SMS__c.  THANK YOU FOR YOUR REPLY!
UC InnovationUC Innovation
You're very welcome Eric! If you don't mind, please choose the best answer so you can mark this thread as solved!
Eric PohlabelEric Pohlabel
Thanks - your trigger put me on track...but a thrid try with the process builder found a declaritive way to do it as well.  Thanks again for your help!