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
Leonie ChristianiLeonie Christiani 

update fields based on a custom field match between objects

Hello,

I am looking to do the following:
When a new case is created with a custom field (Unique ID) populated i want SFDC to find the matching unique ID in an account record and then autopopulate the Account name on the case object.

I appreciate any help i can get!

Thank you,
Leonie
Deepak GerianiDeepak Geriani

Hello,

On case trigger after insert write Select query on account and get the name of account and update or set the field on case object.

case trigger (after insert ){

for(case c : trigger.new)[
Account account = [select id, name from Account where id= :  c.uniquefieldApiName ];
c.Accountfieldname = account.name;
update c;
}

}


this is just sample please use try catch block because you will get errors when you are not able to find the id or submit unrelvant data.

please mark it solved if your issue has been solved 

Jitendra Singh ShahiJitendra Singh Shahi
Hi,

In the above answer, you will have a problem with the governer limit as you are making a DML query inside for loop. It is best practice to always create bulkify triggers and avoid making DML query in for loop. See the below link for the governer limit in apex and trigger best practice.

https://developer.salesforce.com/docs/atlas.en-us.salesforce_app_limits_cheatsheet.meta/salesforce_app_limits_cheatsheet/salesforce_app_limits_platform_apexgov.htm

https://developer.salesforce.com/page/Apex_Code_Best_Practices

You can make your trigger bulkify using this: ( Maybe you have to modify this code for names of your field )
 
trigger yourTriggerName on Case(after insert ){
    Map<String, Case> uniqueFieldMap = new Map<String, Case>(); 
    List<Case> caseToUpdate = new List<Case>();
    for(Case case : trigger.new){
        uniqueFieldMap.put(case.uniquefieldApiName, case);
    }

    for(Accont acc: [select id, name from Account where id in: uniqueFieldMap.keyset()]){
        if(uniqueFieldMap.containsKey(acc.id)){
            Case case = uniqueFieldMap.get(acc.id);
            case.Accountfieldname = account.Name;
            caseToUpdate.add(case);
        }
    }

    if(caseToUpdate.size()>0){
        update caseToUpdate;
    }
}

I hope this will help. let me know if you have any queries. if it helps please mark it as the best answer,  so others can take help from here.