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
Keri-An Richards 9Keri-An Richards 9 

Copy a field from Account to Case

Hi all,
Hoping I can get some help please.
I have a picklist field on Account that I need to copy into a field of the same name (which is also a picklist) on a Case.
So, when a case is created, I need to check the Production_Platform__c field on the Account record that the Case is for and populate the Production_Platform__c field on the case with the same value.
I know I could easily do it with either a formula field or a workflow rule if I could use text in the field, but it needs to be a picklist on the Case to drive a dependent picklist that's relying on it.
So, I think what I need is a trigger - but I don't know Apex and therefore my cry for help!
Thanks,
Keri-An
Best Answer chosen by Keri-An Richards 9
ShashankShashank (Salesforce Developers) 
This should work:
 
trigger updateProductionPlatformFromAccount on Case (before insert) {
    set<Id> accIds = new set<Id>();
    for(case c:trigger.new){
        accIds.add(c.accountId);
    }
    map<Id,account> accMap = new map<Id,account>([select Production_Platform__c from account where Id in :accIds]);
    for(case ca:trigger.new){
        if(accMap.get(ca.accountId).Production_Platform__c!=null){
            ca.Production_Platform__c = accMap.get(ca.accountId).Production_Platform__c;
        }
    }
}

 

All Answers

ShashankShashank (Salesforce Developers) 
This should work:
 
trigger updateProductionPlatformFromAccount on Case (before insert) {
    set<Id> accIds = new set<Id>();
    for(case c:trigger.new){
        accIds.add(c.accountId);
    }
    map<Id,account> accMap = new map<Id,account>([select Production_Platform__c from account where Id in :accIds]);
    for(case ca:trigger.new){
        if(accMap.get(ca.accountId).Production_Platform__c!=null){
            ca.Production_Platform__c = accMap.get(ca.accountId).Production_Platform__c;
        }
    }
}

 
This was selected as the best answer
Keri-An Richards 9Keri-An Richards 9
Thanks Shashank - that's perfect