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
rick82000rick82000 

Case Accept button on Page Layout

This is the first time I am writing Ajax, but it seems pretty straight forward. However I am getting an error for which I cannot find an answer online.

 

I am trying to put a custom button on a Case Page Layout so when a user clicks the button then it will update the Case Owner to be user. The logic is able to query all the information correctly, but I am unable to update the record.

 

 

{!requireScript("/soap/ajax/19.0/connection.js")}

var CaseObj= new sforce.SObject("Case");

var CaseNum = '{!Case.CaseNumber}'
var UserId = '{!User.Id}';
var CaseId = '{!Case.Id}'
var ExistingOwner = '{!Case.OwnerId}'
var CaseObjs = sforce.connection.query("select id, OwnerId from Case where Id ='{!Case.Id}'");

CaseObj = CaseObjs;

if ( UserId == ExistingOwner) {
alert ('You already own the case');

}
else {

CaseObj.OwnerId = UserId;
CaseObj.Id = '{!Case.Id}';
result = sforce.connection.update([CaseObj]);
alert ('You have accepted Case Number' + CaseNum);

}

alert (CaseId + UserId + ExistingOwner + CaseObj);

 

 

 

This particular line gives me the following error when executed: "A problem with OnClick Javascript for this button was encountered. faultcode: Soapenv: Client, faultstring Missing entity type information. SObject requires a seperate 'type' field be sent"

result = sforce.connection.update([CaseObj]);

I will appreciate any help on this issue.

 

Thank You in advance.

 

Best Answer chosen by Admin (Salesforce Developers) 
rick82000rick82000

To my surprise, I don't know what is still wrong with the code above, but I am writing the same logic in a cleaner fashion and it is working for me.

 

Here is the code that I modified

 

 

{!REQUIRESCRIPT("/soap/ajax/13.0/connection.js")}

var CaseNum = '{!Case.CaseNumber}'
var UserId = '{!User.Id}';
var ExistingOwner = '{!Case.OwnerId}'

if ( UserId == ExistingOwner) {
alert ('You already own the case');
}
else {
var caseObj = new sforce.SObject("Case");
caseObj.Id = '{!Case.Id}';
caseObj.OwnerId= '{!$User.Id}';
var result = sforce.connection.update([caseObj]);
if (result[0].success=='false') {
     alert(result[0].errors.message);
} else {  
     alert('You are the new owner of the case');
      location.reload(true);
}

}