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
gopal neelurugopal neeluru 

How to update Custom look up field to user in Case with Account ownerId which is lookup to user using Apex Trigger

trigger Example123 on Case (before insert, before update) {

    public List <Account> lstAcc =  new List<Account>();
    public Id accountOwner;
   /* For (Case c : Trigger.new){
        lst.add(c.id);
    }*/
    //system.debug('******' +lst);
     list<case> cc= [select id,AccountId, case.Account.OwnerId, AdditionalCon__c from case where id =: Trigger.new];
     system.debug('******' +cc);
     
      for(Case cas : cc){
      lstAcc = [Select Id, Ownerid from Account where Id  =: cas.AccountId]; 
      }
     for (Case c: cc){ 
       // system.debug('c.account.Ownerid**********'+c.account.Ownerid);     
         if((Trigger.isInsert || Trigger.isUpdate ) && Trigger.isBefore ){  //Before Insert       
            //system.debug('c.account.Ownerid**********'+c.account.Ownerid);  
             if(c.AdditionalCon__c ==null ){

                for(Account a : lstAcc ){
                    if(c.AccountId == a.Id){
                    accountOwner = a.OwnerId;
                    }   
                } 
                 
               system.debug('AccountOwner**********'+accountOwner);  
               c.AdditionalCon__c = accountOwner; 
               system.debug('AdditionalCon :' +c.AdditionalCon__c);
           }
       } 
          //lst.add(c);
    }
    
        //update lst;
           }
Best Answer chosen by gopal neeluru
PratikPratik (Salesforce Developers) 
Hi Gopal,

Try this code:

trigger caseupdate on case(before update,before insert) {

    list<case> caselist=new list<case>();
    
    map<id,string> casemap=new map<id,string> ();
    set<id> caseids= new set<id> ();
    caseids.addall(trigger.newmap.keyset());
    for(case c :[select id, account.ownerid from case where id in : caseids]) {
    
    casemap.put(c.id,c.account.ownerid);

    }
    
    for(case cs: trigger.new) {
    
    cs.user__c=casemap.get(cs.id);
    }
    
    
}


Here user__c is the custom lookup field on case.

Thanks,
Pratik

P.S. If this answers you question, please mark it as "Best Answer" so it will help other community members too.

All Answers

PratikPratik (Salesforce Developers) 
Hi Gopal,

Try this code:

trigger caseupdate on case(before update,before insert) {

    list<case> caselist=new list<case>();
    
    map<id,string> casemap=new map<id,string> ();
    set<id> caseids= new set<id> ();
    caseids.addall(trigger.newmap.keyset());
    for(case c :[select id, account.ownerid from case where id in : caseids]) {
    
    casemap.put(c.id,c.account.ownerid);

    }
    
    for(case cs: trigger.new) {
    
    cs.user__c=casemap.get(cs.id);
    }
    
    
}


Here user__c is the custom lookup field on case.

Thanks,
Pratik

P.S. If this answers you question, please mark it as "Best Answer" so it will help other community members too.
This was selected as the best answer
Madhavi B TMadhavi B T
@Pratik
Can You Please Mention the same in Apex Handler.
Test Class.
Public Static Void UpdateUser(List<Case> casemap)
But Trigger.newmap how to change