You need to sign in to do that
Don't have an account?
pdo
Custom List Button Update for a Related-Related Object
Hi Team,
I am trying to create a Custom List Button where I would like to update a Custom Object (Asset__c) from the Related List of another Custom Object (Inventory_Request__c) that are joined by a Junction Object (Asset_Request_Junction__c). In a nutshell, I am on the Inventory Request Screen, I select my Asset Request Junction records, and want to update the Assets associated to theose Junction records. The error I am getting is saying that the Ids are not defined...can anyone help me with this one?
{!REQUIRESCRIPT("/soap/ajax/16.0/connection.js")} var assetItems = {!GETRECORDIDS($ObjectType.Asset_Request_Junction__c)}; var SelectedIds=''; for(var i=0;i < assetItems.length; i++) { SelectedIds+="'"+assetItems[i]+"',"; } SelectedIds=SelectedIds.substring(0,SelectedIds.length - 1); var updatedAssets = []; if(assetItems.length<1) { alert("Please choose at least one Asset to update."); } else { var r = confirm("Click ''OK'' to update Assets."); if (r == true) { try { var aiQuery = "select Asset__r.Id from Asset_Request_Junction__c where Id in ("+SelectedIds+")"; var result = sforce.connection.query(aiQuery); var records = result.getArray("records"); for (var n=0; n<records.length; n++) { if (records[n].get("Asset_Status__c") != "Good Firefox") { var a = new sforce.SObject("Asset__c"); a.id = records[n].id; a.Asset_Status__c = "Good Firefox"; updatedAssets.push(a); } } result = sforce.connection.update(updatedAssets); window.location.reload(); if (result[0].getBoolean("success")) { alert("Assets Updated"); } else { alert("Failed to update Assets" + result[0]); } } catch (e) { alert(e); } } }
Thanks hitz! Got it working!
All Answers
Please try below code.
{!REQUIRESCRIPT("/soap/ajax/10.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")}
var assetItems = {!GETRECORDIDS($ObjectType.Asset_Request_Junction__c)};
var SelectedIds='';
for(var i=0;i < assetItems.length; i++) {
SelectedIds= SelectedIds + ","+assetItems[i];
}
var updatedAssets = [];
if(assetItems.length<1) {
alert("Please choose at least one Asset to update.");
} else {
var r = confirm("Click ''OK'' to update Assets.");
if (r == true) {
try {
var aiQuery = "select Asset__r.Id from Asset_Request_Junction__c where Id in ("+SelectedIds+")";
var result = sforce.connection.query(aiQuery);
var records = result.getArray("records");
for (var n=0; n<records.length; n++) {
if (records[n].get("Asset_Status__c") != "Good Firefox") {
var a = new sforce.SObject("Asset__c");
a.id = records[n].id;
a.Asset_Status__c = "Good Firefox";
updatedAssets.push(a);
}
}
result = sforce.connection.update(updatedAssets);
window.location.reload();
if (result[0].getBoolean("success")) {
alert("Assets Updated");
} else {
alert("Failed to update Assets" + result[0]);
}
} catch (e) {
alert(e);
}
}
}
Hey hitz,
Thanks for the quick response...I am now getting this error. Not sure that I am formatting my query very well here. Below is the error. Any advice?
MALFORMED_QUERY
Asset_Request_Junction__c where Id in (,<<ID>>,<<ID>>)
ERROR at Row;:1:Column:62
unexpected token: '('',
Hi,
Replace code with below.
//var aiQuery = "select Asset__r.Id from Asset_Request_Junction__c where Id in ("+SelectedIds+")";
var result = sforce.connection.query("select Asset__r.Id from Asset_Request_Junction__c where Id in ("+SelectedIds+")");
var records = result.getArray("records");
Thanks hitz! Got it working!