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
AswiniAswini 

update the Contact owner and Account owner with the user who created the Case.How to do this ??

Update the Contact owner and Account owner with the user who created the Case.How to do this ??
Best Answer chosen by Aswini
CharuDuttCharuDutt
Hii Ashwini
Try Below trigger
trigger caseTrigger on Case (after insert) {
    map<Id,String>Newmap = new map<Id,String>();
    for(Case c : trigger.new){
        if(c.AccountId != null ){
            Newmap.put(c.AccountId, c.OwnerId);
        }
        if(c.ContactId != null){
            Newmap.put(c.ContactId, c.OwnerId);
        }
    }
    list<Account> lstAcc = [Select Id,OwnerId from Account where Id In :Newmap.keySet()];
    list<Contact> lstCon = [Select Id,OwnerId from Contact where Id In :Newmap.keySet()];
    For(Account Acc : lstAcc){
        if(Newmap.containsKey(Acc.Id)){
            Acc.OwnerId = Newmap.get(Acc.Id);
        }
    }
    For(Contact Con : lstCon){
        if(Newmap.containsKey(Con.Id)){
            Con.OwnerId = Newmap.get(Con.Id);
        }
    }
    
    if(lstAcc.size()>0){
        update lstAcc;
    }
	if(lstCon.size()>0){
        update lstCon;
    }
}
Please Mark It As Best Answer If It Helps
Thank You! 

All Answers

CharuDuttCharuDutt
Hii Ashwini
Try Below trigger
trigger caseTrigger on Case (after insert) {
    map<Id,String>Newmap = new map<Id,String>();
    for(Case c : trigger.new){
        if(c.AccountId != null ){
            Newmap.put(c.AccountId, c.OwnerId);
        }
        if(c.ContactId != null){
            Newmap.put(c.ContactId, c.OwnerId);
        }
    }
    list<Account> lstAcc = [Select Id,OwnerId from Account where Id In :Newmap.keySet()];
    list<Contact> lstCon = [Select Id,OwnerId from Contact where Id In :Newmap.keySet()];
    For(Account Acc : lstAcc){
        if(Newmap.containsKey(Acc.Id)){
            Acc.OwnerId = Newmap.get(Acc.Id);
        }
    }
    For(Contact Con : lstCon){
        if(Newmap.containsKey(Con.Id)){
            Con.OwnerId = Newmap.get(Con.Id);
        }
    }
    
    if(lstAcc.size()>0){
        update lstAcc;
    }
	if(lstCon.size()>0){
        update lstCon;
    }
}
Please Mark It As Best Answer If It Helps
Thank You! 
This was selected as the best answer
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Aswini,

As I answered in other question please use the below code.
 
trigger CaseTrigger on Case (after insert,after update) {
    set<id> accountid  = new set<id>();
    set<id> Contactid= new set<id>();
    Map<id,id> mapAccount= new Map<id,id>();
        Map<id,id> mapContact= new Map<id,id>();
    List<Contact> conlist= new list<Contact>();
    List<Account> acclist1= new List<Account>();
    for (Case c: Trigger.new){
        if(c.AccountId!=null){
            accountid.add(c.AccountId);
            mapAccount.put(c.AccountId,c.OwnerId);
        }
        if(c.ContactId!=null){
            Contactid.add(c.ContactId);
            mapContact.put(c.ContactId, c.OwnerId);
        }
        
    }
    List<Contact> con= [select id,ownerid from contact where id=:Contactid];
    List<Account> acc= [Select id,ownerid from Account where id=:Accountid];
    
    for(Account acclist:acc){
        if(mapAccount.containsKey(acclist.id))
            acclist.OwnerId=mapAccount.get(acclist.id);
        acclist1.add(acclist);
        
    }
    for(Contact clist:con){
         if(mapContact.containsKey(clist.id))
            clist.OwnerId=mapContact.get(clist.id);
        conlist.add(clist);
    }
    update acclist1;
    update conlist;

}

If this solution helps, Please mark it as best answer.

Thanks,