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
mlundwallmlundwall 

Auto-fill values from Account to Case.

Hi,

 

On our Accounts we have a unique ID field that works as a unique identifier in other systems. When customers call the Customer Care and they create Cases this is what they supply as part of the identification process. This unique ID needs to be entered in the Case which should then link the case to the account and populate fields on the case with information from the account.

 

Any ideas on how to go about?

Best Answer chosen by Admin (Salesforce Developers) 
mlundwallmlundwall

This has been solved.

 

One problem was that using an after insert on the case you cannot change the Case via Trigger.new.

 

I had to retrieve the case with an SOQL and then update with the update statement.

All Answers

Jake GmerekJake Gmerek

A trigger should do the trick for you.

 

Something like this:

Trigger updateFields on case (before insert, before update){
list<id> ids = new list<id>(); //store the case ids
for (case c : trigger.new){ 
   //Check to make sure that the case is being linked to an account
   if (c.accountName != NULL){
      ids.add(c.id);
   }

list<case> theCases = new list<case>([select id, fieldtoupdate1,..., accountName.fieldOneValue, ... from case where id in: ids]);

for (case curCase: theCases){
    curCase.fieldtoUpdate1 = curCase.fieldOneValue;
    ...
}
}

 

mlundwallmlundwall

Thanks for reply,

 

When trying to set the account on the case I cannot get the account to be saved. The field on the Case that I thought stored the account is a lookup(Account) how do I get the case to be related to the account my code looks like:

 

trigger updateFields on Case (before insert, before update) {
    list<String> eids = new list<String>(); //store the case eids
    for (case c : trigger.new){ 
       //Check to make sure that the case is being linked to an account
       if (c.EID__c != NULL){
          eids.add(c.EID__c);
          system.debug('account:' + c.Account);
       }
     }
    for (String sid: eids){
    	system.debug('eid:' +sid);
    }
     
    system.debug('eids: '+eids.size());
    list<Account> accs = new list<Account>([select 
                              Name,
                              Store_ID__c
                        from 
                              Account
                        where Store_ID__c IN: eids
                         ]);
                                  
    system.debug('accs: '+accs.size());
    for (Account curAcc: accs){
        system.debug('storeid: '+curAcc.Store_ID__c);                          
        for (Case curCase: trigger.new){
            curCase.Account = (Account) curAcc;
        }
    }
}

 

mlundwallmlundwall

This has been solved.

 

One problem was that using an after insert on the case you cannot change the Case via Trigger.new.

 

I had to retrieve the case with an SOQL and then update with the update statement.

This was selected as the best answer