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
SfdcprogrammerSfdcprogrammer 

How to update case owner using apex

How to update case owner using apex???
Himanshu ParasharHimanshu Parashar
Hi,

You can write a before trigger.
 
Trigger CaseBefore on Case (before insert, before update){

//This will be your criteria
List<User> User = [select id from user limit 1];

for(Case cas : Trigger.new){
Cas.OwnerId = User[0].Id; // actual assignment will happend here.
}

or if it is not clear to you kindly elaborate your problem in detail. 

Thanks,
Himanshu
Salesforce Certified Developer | Administrator | Service Cloud Consultant

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi ,

It would be depending on your logic as well,but as per your question here is a general way.

1. Query Case using case Id or some other parameter.
2. Assgn a new value to Ownerid field.

Id caseid ='YOUR CASE ID';
ID newOwnerId ='YOUR NEW OWNER ID';
Case caseInstance = [select id,OwnerId from case where id=:caseId];
caseInstance.OwnerId = newOwnerId;
update caseInstance;

Mark this answer as BEST ANSWER if it helps you.

 
bob_buzzardbob_buzzard
Damn there's a lot of people demanding that their answer is marked as the best one.  SHOUTY CAPITALS IN BOLD no less.  

Assigning a new owner is simply a matter of updating the OwnerId field with the id of the User or Queue that you want to assign the case to.  The more difficult part is how to identify the User that you wish to assign to.  You don't want to rely on a hardcoded user id as those can change between sandbox and production. Its also not good practice to rely on a hardcoded name, as you need to update code to change this, if the user leaves for example.  I typically use a custom setting that identifies the username and then query the user record based on that. Then if the user to assign to changes, an administrator just has to update the custom setting.

You should also ensure that the user has access to the case sobject - only Salesforce licensed users do, so you'd need to make sure you didn't try to assign to a work.com or platform only user.

I don't care if you mark this as the best answer or not. I'm certainly not going to make demands or get all shouty at you.