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
tsalbtsalb 

JS Onclick Button: Query to update other child records? (Detail Page Button)

Is it possible to update multiple records from a detail page button with the help of a query? I know it's possible to do this with a list, but here's the scenario:

 

User recommends child record - changes a status and a boolean. Other child records also need to have their boolean(s) toggled to true. However, the button is on the detail page - NOT a list. 

 

The following works great:

 

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

var newRecords = [];

var thisP = new sforce.SObject("Proposal__c");
thisP.id = "{!Proposal__c.Id}";
thisP.Status__c = "Awarded";
thisP.test__c = true;
newRecords.push(thisP);

result = sforce.connection.update(newRecords);

location.reload(true);

 

 

However, I'm trying to introduce this, where Ideally i'd like to update other child records (with the same parent (Order__c) lookup)

 

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

var newRecords = [];

var otherP = sforce.connection.query("Select Id from Proposal__c where Order__c = '{!Proposal__c.OrderId__c}'  ");
otherP.test__c = true;
newRecords.push(otherP);  //Add all other records to array?

var thisP = new sforce.SObject("Proposal__c");
thisP.id = "{!Proposal__c.Id}";
thisP.Status__c = "Awarded";
thisP.test__c = true;
newRecords.push(thisP);   //Add current detail page record to array

result = sforce.connection.update(newRecords);

location.reload(true);

 

Best Answer chosen by Admin (Salesforce Developers) 
b-Forceb-Force

here to go

 

Thanks,

Bala

{!REQUIRESCRIPT("/soap/ajax/23.0/connection.js")}
try
{
var strQuery="Select Id from Proposal__c where Order__c = '" +'{!Proposal__c.OrderId__c}'+"'";

var newRecords = [];

var otherP = sforce.connection.query(strQuery);
alert(otherP);
var records = otherP.getArray("records");
for(var i=0; i < records.length ; i ++)
{
	var ob=new sforce.SObject("Proposal__c");
    ob.Id=records[i].Id;
	ob.Status__c = "Awarded";
	ob.test__c = true;
	newRecords.push(ob);
}
var result=sforce.connection.update(newRecords);

alert(result);

}catch(er)
{
	alert(er);
}

 

All Answers

b-Forceb-Force

here to go

 

Thanks,

Bala

{!REQUIRESCRIPT("/soap/ajax/23.0/connection.js")}
try
{
var strQuery="Select Id from Proposal__c where Order__c = '" +'{!Proposal__c.OrderId__c}'+"'";

var newRecords = [];

var otherP = sforce.connection.query(strQuery);
alert(otherP);
var records = otherP.getArray("records");
for(var i=0; i < records.length ; i ++)
{
	var ob=new sforce.SObject("Proposal__c");
    ob.Id=records[i].Id;
	ob.Status__c = "Awarded";
	ob.test__c = true;
	newRecords.push(ob);
}
var result=sforce.connection.update(newRecords);

alert(result);

}catch(er)
{
	alert(er);
}

 

This was selected as the best answer
tsalbtsalb

Almost. I'm goign to try to tweak it a little bit but,

 

I'm not trying to change all related records status to "Awarded". Just the single one (from where the button click was initiated).

 

However, I need to change ALL (including one from where button click was initiated) to test__c = true;

 

I think this might be of some help though, i'll definitely poke around. 

tsalbtsalb

Working

 

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

try
{
var strQuery="Select Id from Proposal__c where Order__c = '" +'{!Proposal__c.OrderId__c}'+"'";

var newRecords = [];

var otherP = sforce.connection.query(strQuery);
var records = otherP.getArray("records");
for(var i=0; i < records.length ; i ++)
{
	var ob = new sforce.SObject("Proposal__c");
        ob.Id = records[i].Id;
	ob.test__c = true;
	newRecords.push(ob);
}
var thisOb = new sforce.SObject("Proposal__c");
thisOb.Id = "{!Proposal__c.Id}";
thisOb.Status__c = "Awarded";

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

alert(result);
location.reload(true);

}catch(er)
{
	alert(er);
}

 

b-Forceb-Force

Its nice to heard that it works for you

 

Thanks,

bForce

tsalbtsalb

Thanks again bForce. I have another issue now - it's no fault of the button - but I can't seem to use it for a named portal user.

 

http://boards.developerforce.com/t5/AJAX-Toolkit-S-Controls/Named-Customer-Portal-JS-onclick-remote-invocation-failed/td-p/430607

 

Basically, we have authenticated portal users logging in, and this is a button for them. I've tested it thoroughly with an internal user (myself) and it works fine. however...when a portal user (with API enabled) tries to hit the button.... I get a weird error.

 

Remote invocation failed, due to: (Lots of html markup,seems like my generic error page)

then...

Page Not Found: /portal/services/Soap/u/24.0

 

And I know that site users can't use the API - can named portal users not as well?

tsalbtsalb

Figured part of this out...

 

{!REQUIRESCRIPT("/soap/ajax/24.0/connection.js")}
sforce.connection.serverUrl = '{!$Site.Prefix}/services/Soap/u/24.0';

 Needed to add the second line. 

 

However, it looks like named customer portal users do not have access to the SOQL in the button? I'm able to make edits on the page the button is clicked from...however the boolean isn't toggled on (any) of the records.

tsalbtsalb

ughhhhh. I'm retarded.

 

Didn't enable write permission to that field for the portal user. All is well.