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
DTAPADMINDTAPADMIN 

Get Unexpected element error when tyring to update case status with S-Control

I am trying to create an S-Control that updates the status of a Case to the value of the status of a bug which is a child custom object.  I get the error: Unexpected element {urn:partner.soap.sforce.com}done during simple type deserialization' when I click on the link to execute the S-Control. 
 
Here is my code:
 

Code:

<html>
<head>
<script src="/soap/ajax/9.0/connection.js"></script>
<script src="/js/dojo/0.4.1/dojo.js"></script>
<script> 
 dojo.addOnLoad(init);
function init() { 
 var callback = { 
 onSuccess : displayResult, 
 onFailure : displayError 
 };  
 var cnum = sforce.connection.query("SELECT Case__r.Id FROM SFDC_Bug__c WHERE Name = '{!SFDC_Bug__c.Name}'");    
 var bstatus = sforce.connection.query("SELECT Status__c FROM SFDC_Bug__c WHERE Name = '{!SFDC_Bug__c.Name}'");    
 var myObj = new sforce.SObject("Case");   
 myObj.Id = cnum;  
 myObj.Status = bstatus;   
 sforce.connection.update([myObj], callback);
} 

function displayResult(result) { 
 var it = new sforce.QueryResultIterator(result); 
 var html = []; 
 while(it.hasNext()) { 
 var record = it.next(); 
 if (record.Case__r) { 
 html.push("Case__r.Id = " + record.Case__r.Id + "Has Been Updated!" + "<br>"); 
 } 
 html.push("<hr>"); 
 html.push("<br>"); 
 } 
 document.getElementById("output-div").innerHTML = html.join("");
}
function displayError(error) { 
 document.getElementById("output-div").innerHTML = 
 "oops something went wrong ... " + error;
}

</script> 

</head>
<body> 
<div id="output-div"></div>
</body>
</html>


 
Any help would be greatly appreciated.

Ron HessRon Hess
you can get both of these in one query
try :

"SELECT Case__r.Id, Status__c FROM SFDC_Bug__c WHERE Name = '{!SFDC_Bug__c.Name}'"


but, note the query call returns a struct.
cnum and bstatus are structures, not id's or strings to use in the properties of the new myObj

you should try something like this

var records = cnum.getArray('records')
records[0].Case_r.Id;  // access the ID

to get the id, then the status, would be
records[0].Status__c // get the status



DTAPADMINDTAPADMIN

Here is my updated code and now I get [object Error]

Code:

<html>
<head>
<script src="/soap/ajax/9.0/connection.js"></script>
<script src="/js/dojo/0.4.1/dojo.js"></script>
<script> 
 dojo.addOnLoad(init);
function init() { 
 var callback = { 
 onSuccess : displayResult, 
 onFailure : displayError 
 };  

 var queryResult = sforce.connection.query("SELECT Case__r.Id, Status__c FROM SFDC_Bug__c WHERE Name = '{!SFDC_Bug__c.Name}'");   

 var records = queryResult.getArray('records');  
 
 var myObj = new sforce.SObject("Case");  
 myObj.Id = records[0].Case__r.ID;  
 myObj.Status = records[0].Status__c;  
 sforce.connection.update([myObj], callback);
} 

function displayResult(result) { 
 var it = new sforce.QueryResultIterator(result); 
 var html = []; 
 while(it.hasNext()) { 
 var record = it.next(); 
 if (record.Case__r) { 
 html.push("Case__r.Id = " + record.Case__r.Id + "Has Been Updated!" + "<br>"); 
 } 
 html.push("<hr>"); 
 html.push("<br>"); 
 } 
 document.getElementById("output-div").innerHTML = html.join("");
}

function displayError(error) { 
 document.getElementById("output-div").innerHTML = 
 "oops something went wrong ... " + error;
}
</script> 

</head>
<body> 
 <div id="output-div"></div>
</body>
</html>


 

Ron HessRon Hess
i will have to look at your schema, i think you need to use Case__c rather than __r

can you email me directly rhess at salesforce.com
thx
DTAPADMINDTAPADMIN
I have sent you an email in regards to this post.
DTAPADMINDTAPADMIN

Ron worked with me on this code and we got it to work.  So if you are looking for a way to update the status of one object based on the status of another.  This code will do just that.  FYI the display results code is for troubleshooting you will probably want to replace this with code to return them to the view they were in.

Code:

<html> 
<head> 
<script src="/soap/ajax/9.0/connection.js"></script> 
<script src="/js/dojo/0.4.1/dojo.js"></script> 
<script> 
dojo.addOnLoad(init); 
function init() { 
var callback = { 
onSuccess : displayResult, 
onFailure : displayError 
}; 

var queryResult = sforce.connection.query("SELECT Case__r.Id, Status__c FROM SFDC_Bug__c WHERE Name = '{!SFDC_Bug__c.Name}'"); 

var records = queryResult.getArray('records'); 

var myObj = new sforce.SObject("Case"); 
myObj.Id = records[0].Case__r.Id; 
myObj.Status = records[0].Status__c; 
sforce.connection.update([myObj], callback); 
} 

function displayResult(result) { 
var html = []; 
html.push(result.toString()); 

html.push("<hr>"); 
html.push("<br>"); 

document.getElementById("output-div").innerHTML = html.join(""); 

} 

function displayError(error) { 
document.getElementById("output-div").innerHTML = 
"oops something went wrong ... " + error; 
} 
</script> 

</head> 
<body> 
<div id="output-div"></div> 
</body> 
</html>


 
Thank You Ron for all your help.  I really appreciate it!

lpadminlpadmin

Question: We have field in opportunities that we want to auto update with the value from corresponding field in single related, custom object.

I it feasable to have this done automatically as the value changes in the custom object?

DTAPADMINDTAPADMIN
You would have to have the above code ethier override the save button or ran as an inline s-control on the detail page.