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
vandervander 

AJAX - update AssetId on Case problem

Hi -

I'm working on an S-Control using the AJAX toolkit to update the AssetId on a Case. My query is returning the correct Case Id and I have the correct AssetId to update with but my update is not working. My javascript function is below. This is my first time working with the AJAX toolkit and Case object so my syntax is probably incorrect. Any suggestions are appreciated:

function IsChecked(form, element_name, value)
{
//query sforce for current Case and current AssetId
var queryResult2 = sforceClient.Query("Select Id, CaseNumber, AssetId from Case Where CaseNumber = '" + caseId + "'");
//new array to hold results of updates
var updateObjects = new Array();
for (i = 0; i < form.elements.length; i++) {
if ((form.elements[i].name == element_name) && (form.elements[i].checked)) {
var aid = form.elements[i].value;
var _case = queryResult2.records[0];
//set case AssetId to checked value
_case.set("AssetId", aid);
//push updated AssetId to update array
updateObjects.push(_case);
}
}

if (updateObjects.length > 0) {
var x = window.confirm("Are you sure you want to update "+ updateObjects.length +" record(s) ?");
if (x) {
var sr = sforceClient.Update(updateObjects);
//window.parent.parent.location.href = retUrl;
window.close();
}
} else {
alert("No Records will be updated");
//window.parent.parent.location.href = retUrl;
window.close();
}
}
DevAngelDevAngel

Hi vander,

Let's try this:

function IsChecked(form, element_name, value) 
{ 
	//query sforce for current Case and current AssetId
	var queryResult2 = sforceClient.Query("Select Id, CaseNumber, AssetId from Case Where CaseNumber = '" + caseId + "'"); 
	//new array to hold results of updates
	var updateObjects = new Array();
	for (i = 0; i < form.elements.length; i++) { 
		if ((form.elements[i].name == element_name) && (form.elements[i].checked)) { 
			var aid = form.elements[i].value;
			var _case = queryResult2.records[i];
			//set case AssetId to checked value
			_case.set("AssetId", aid);
			//push updated AssetId to update array
			updateObjects.push(_case);
		} 
	} 

	if (updateObjects.length > 0) { 
		var x = window.confirm("Are you sure you want to update "+ updateObjects.length +" record(s) ?"); 
		if (x) { 
			var sr = sforceClient.Update(updateObjects);
			for (var i=0;i<sr.length;i++) {
				if (sr[i].success == false) {
					alert("There was an error updating case " + 
						updateObjects[i].get("CaseNumber") + "\nError: " + 
						sr[i].errors[0].message);
				}
			}
			//window.parent.parent.location.href = retUrl; 
			window.close();
		} 
	} else { 
		alert("No Records will be updated"); 
		//window.parent.parent.location.href = retUrl; 
		window.close();
	} 
} 

Message Edited by DevAngel on 08-10-2005 09:18 AM

vandervander
thanks!

your code to show return errors helped me figure it out.
the update was erroring out because i was selecting CaseNumber in my query.
because this was in my update object, it was trying to update the CaseNumber which obviously is not allowed.
once i removed that from the select statement everything worked great!
DevAngelDevAngel

Yup, it's a little extra code, but the time spent to write the extra code is a lot less than staring till your cross eyed at your lines of code.