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
Tejashwini S 1Tejashwini S 1 

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 Tejashwini S 1
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Aswini,

The trigger should be modified slightly as below.
 
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;

}

This will work for update scenerio also.

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

Thanks,
 

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Tejashwini,

please try using the below code.
 
trigger CaseTrigger on Case (after insert) {
    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,
 
ravi soniravi soni
hi ,
Try below trigger. I believe it will help you.
trigger CaseTrigger on Case (after insert) {
        Map<id,id> mapAccount= new Map<id,id>();
        Map<id,id> mapContact= new Map<id,id>();
    List<Contact> conlist= new list<Contact>();
    List<Account> acclist =  new List<Account>();
	
    for (Case oCase: Trigger.new){
        if(oCase.AccountId!=null){
            mapAccount.put(oCase.AccountId,oCase.OwnerId);
        }
        if(oCase.ContactId!=null){
           
            mapContact.put(oCase.ContactId, oCase.OwnerId);
        }
        
    }
   
    
    for(Account acc:[Select Id,ownerid from Account where Id In : mapAccount.keySet()]){
        if(mapAccount.containsKey(acc.Id))
            acc.OwnerId=mapAccount.get(acc.Id);
        acclist.add(acc);
        
    }
    for(Contact con:[select id,ownerid from contact where id In :mapContact.keySet()]){
         if(mapContact.containsKey(con.id))
            con.OwnerId=mapContact.get(con.id);
        conlist.add(con);
    }
	if(acclist.size() > 0){
	update acclist;
	}
	if(conlist.size() > 0){
	update conlist;
	}
    
    

}

don't forget to mark it as best answer.
Thank you​​​​​​​
AswiniAswini
Hi Sai Praveen  ,
This trigger is not fulfilling the conditions.
Once I change the owner the account and contact the owner remains the same.
​​​​​​​Could you plz help me to resolve this?
mukesh guptamukesh gupta
Hi Aswini,

You need to write before update trigger on Account and Contact object. and check with Account Owner and Contact Owner with Case Owner.

if Owner is same then user can't change new owner.

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh

 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Aswini,

As per the question you mentioned the owner should be changed only while creation. So written the code for insert scenerio. I will update the code for update scenerio also and will share here.

Thanks,
 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Aswini,

The trigger should be modified slightly as below.
 
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;

}

This will work for update scenerio also.

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

Thanks,
 
This was selected as the best answer
AswiniAswini
Hi Sai Praveen ,
Thank you so muchhh...
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Aswini,

If the solution helped you , Please mark it as best answer.

Thanks,