Update case and opportunity owner to login user using custom button:

I am having a case record 'x' and i want to create a custom button and when i click the button it will update case owner and opportunity owner to login user. How can i achieve this??

  • Are you here referring the custom detail page or list button or button in VF page? Apr 17 '15 at 10:58
  • I tried in calling a apex class using onclick javascript button but i am new to it i am not able to call apex class Apr 17 '15 at 11:01
  • @ase I think this is what you are looking for. just update the relevant fields accordingly. success.salesforce.com/answers?id=90630000000ggpwAAA Apr 17 '15 at 11:06

You don't need to create any apex class for accomplishing this. You can make use of connection.js file. Place following code on click of javascript

var objCase= new sforce.SObject("Case"); 
objCase.Id = '{!Case.Id}';
objCase.OwnerId = '{!$User.Id}';
var result = sforce.connection.update([objCase]); 

Repeat same for Opportunity object and replace Case with Opportunity.

  • Thanks for the quick response. It is working fine. how can i update case related opportunitites and i am having few custom objects owner to update??? Apr 17 '15 at 11:34
  • Are you referring here that Opportunity object has a lookup with Case i.e. Opportunity is the child object of Case? Apr 17 '15 at 11:37
  • For other custom objects, you can simply replace the Case with Custom object API name in the code shared. {!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")} {!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")} var objCase= new sforce.SObject(custom object API name); objCase.Id = '{!customobject.Id}'; objCase.OwnerId = '{!$User.Id}'; var result = sforce.connection.update([objCase]); Apr 17 '15 at 11:38
  • Yes @ pankaj . In case i am having lookup to opportunity. If i click on the button it must update case owner, opportunity owner, an custom object owner with one button click Apr 17 '15 at 11:41

Hi Pankaj i have written the code for opportunity

var objCase= new sforce.SObject(opportunity); objCase.Id = '{!opportunityId}'; objCase.OwnerId = '{!$User.Id}'; var result = sforce.connection.update([objCase]);

var result = sforce.connection.update([objCase]);

But i am getting error for this please help me out it is a lookup field

  • {!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")} {!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")} var objCase= new sforce.SObject("Opportunity"); objCase.Id = '{!Opportunity.Id}'; objCase.OwnerId = '{!$User.Id}'; var result = sforce.connection.update([objCase]); Apr 17 '15 at 12:02
  • Case is working fine but for oppportunity case owner remains same. Button is on case object may be it is not calling related opportunity?? Apr 17 '15 at 12:06
  • Or can u please help me out how to call apex class from javascript also helps me. Please guide me Apr 17 '15 at 12:10
  • Please check my below response. Apr 17 '15 at 12:25



    var result = sforce.apex.execute("ClassName","methodName",


global class ClassName

    webservice static String methodName(Id sId, String sName)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.