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
Saurabh Bisht 19Saurabh Bisht 19 

Concatenate two fields using trigger design field

object:opportunity
Opportunity Name field concatenate with Account Name field
ex:
Opportunity name :jbl
Account Name: Saurabh
Result should be: jbl-Saurabh
Any one code it
AnkaiahAnkaiah (Salesforce Developers) 
Hi Saurabh,

try with below code.
trigger oopnameUpdate on Opportunity (before insert,before update) {
    
    set<id> accids = new set<id>();
    for(opportunity opp:trigger.new){
        
        if(opp.accountid != null){
            accids.add(opp.accountid);
        }
    }
    map<id,string> accnamemap = new map<id,string>();
    List<Account> acclist = [select id,name from Account where id =:accids];
    for(account acc: acclist){
       accnamemap.put(acc.id,acc.name);
    }
    
   for(opportunity opp:trigger.new){
        
        if(accnamemap.containsKey(opp.AccountId)){
        
        opp.Name = opp.Name + '-'+accnamemap.get(opp.AccountId);
        }
   }
}

If this helps, Please mark it as best answer.

Thanks!!