function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ruby KandahRuby Kandah 

Using Flow or Apex Trigger to Update Account Field from Case Field

Hey Everyone! 

I want fields (picklist and text fields) in my Case Object to be linked to similar fields in the Account Object. I am trying to use Flow Designer + Process Builder to accomplish this, but I can't seem to get it to trigger. Can anyone help me out with this?

Thanks!
Best Answer chosen by Ruby Kandah
sailee handesailee hande
Hi Ruby,

trigger UpdateField on Case (before insert,before update) 
{
       List<Case> cons = new List<Case>();
    System.debug('trigger.newMap.keySet()'+trigger.newMap.keySet());
    Set<Id> accountIdSet = new Set<Id>();
    for(Case c : Trigger.New)
        accountIdSet.add(c.AccountId);
        
    //List<Account> acc    =    [Select id,DescriptionNew__c,City__c from Account];
    Map<id,Account> idToAccountMap    =    new Map<id,Account>([Select id,DescriptionNew__c,City__c from Account 
                                                             where id in : accountIdSet]);
    System.debug('idToAccountMap'+idToAccountMap);
       //Case cons    =    new Case();
    For(Case c: trigger.new)
    {
            c.DescriptionNew__c    =    idToAccountMap.get(c.AccountID).DescriptionNew__c;
          c.City__c            =    idToAccountMap.get(c.AccountID).City__c;
            
    }     
     
    //update cons; 
}

Instead of DescriptionNew__c,City__c you can put your own fields.If this helps then please let me know.

Regards,
Sailee

All Answers

sailee handesailee hande
Hi Ruby,

trigger UpdateField on Case (before insert,before update) 
{
       List<Case> cons = new List<Case>();
    System.debug('trigger.newMap.keySet()'+trigger.newMap.keySet());
    Set<Id> accountIdSet = new Set<Id>();
    for(Case c : Trigger.New)
        accountIdSet.add(c.AccountId);
        
    //List<Account> acc    =    [Select id,DescriptionNew__c,City__c from Account];
    Map<id,Account> idToAccountMap    =    new Map<id,Account>([Select id,DescriptionNew__c,City__c from Account 
                                                             where id in : accountIdSet]);
    System.debug('idToAccountMap'+idToAccountMap);
       //Case cons    =    new Case();
    For(Case c: trigger.new)
    {
            c.DescriptionNew__c    =    idToAccountMap.get(c.AccountID).DescriptionNew__c;
          c.City__c            =    idToAccountMap.get(c.AccountID).City__c;
            
    }     
     
    //update cons; 
}

Instead of DescriptionNew__c,City__c you can put your own fields.If this helps then please let me know.

Regards,
Sailee
This was selected as the best answer
Ruby KandahRuby Kandah
Hi Sailee, 

Thanks so much for your answer! I actually wanted to update fields on the account when a case field was updated, so should I just switch over the case and the account call objects in your code?

Let me know!

Thanks!


Ruby
sailee handesailee hande
Hi Ruby,

Yes you can switch it.If it works then let me know.

Regards,
Sailee
Keith Stephens 18Keith Stephens 18
Hello, how would you do this in reverse?  If I select a broker from my case page then save it I want the brokers email to show in the brokers email text box on the case page, but the broker email is in the account.Primary_Email__r.
Thanks,