+ Start a Discussion
ArrowArrow 

JS Onclick Button update child records

I would like a button on opportunities which can be clicked to update child opportunities

 

I have tried to adapt code I found on this forum but the below code updates the parent rather than the children. I don't have much javascript knowledge.

 

Child opportunities linked to parent opportunities via Parent_Opportunity__c lookup. Child Relationship Name is Child_Opportunities.

 

 

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

try
{
var strQuery="Select Id from Opportunity where Parent_Opportunity__c = '" +'{!Opportunity.Id}'+"'";

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("opportunity");
        ob.Id = records[i].Id;
	newRecords.push(ob);
}
var thisOb = new sforce.SObject("opportunity");
thisOb.Id = "{!Opportunity.Id}";
thisOb.StageName= "{!Opportunity.StageName}";
thisOb.Status__c= "{!Opportunity.Status__c}";

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

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

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

 

Best Answer chosen by Admin (Salesforce Developers) 
SoleesSolees

Try something like this:

 

{!REQUIRESCRIPT("/soap/ajax/23.0/connection.js")}
var __sfdcSessionId = '{!GETSESSIONID()}';
try {
var strQuery="Select Id from Opportunity where Parent_Opportunity__c = '" +'{!Opportunity.Id}'+"'";
var newRecords = [];
var otherP = sforce.connection.query(strQuery);
var records = otherP.getArray("records");
if (records.length >= 1) {
for(var i=0; i < records.length ; i ++) {
var ob = new sforce.SObject("Opportunity");
ob.Id = records[i].Id;
ob.StageName= "{!Opportunity.StageName}";
ob.Status__c= "{!Opportunity.Status__c}";
var result = sforce.connection.update([ob]);
}
var thisOb = new sforce.SObject("opportunity");
thisOb.Id = "{!Opportunity.Id}";
thisOb.StageName= "{!Opportunity.StageName}";
thisOb.Status__c= "{!Opportunity.Status__c}";
var result = sforce.connection.update([thisOb]);

alert(result);
location.reload(true);
}
}catch(er) {
alert(er);
}

All Answers

SoleesSolees

Try something like this:

 

{!REQUIRESCRIPT("/soap/ajax/23.0/connection.js")}
var __sfdcSessionId = '{!GETSESSIONID()}';
try {
var strQuery="Select Id from Opportunity where Parent_Opportunity__c = '" +'{!Opportunity.Id}'+"'";
var newRecords = [];
var otherP = sforce.connection.query(strQuery);
var records = otherP.getArray("records");
if (records.length >= 1) {
for(var i=0; i < records.length ; i ++) {
var ob = new sforce.SObject("Opportunity");
ob.Id = records[i].Id;
ob.StageName= "{!Opportunity.StageName}";
ob.Status__c= "{!Opportunity.Status__c}";
var result = sforce.connection.update([ob]);
}
var thisOb = new sforce.SObject("opportunity");
thisOb.Id = "{!Opportunity.Id}";
thisOb.StageName= "{!Opportunity.StageName}";
thisOb.Status__c= "{!Opportunity.Status__c}";
var result = sforce.connection.update([thisOb]);

alert(result);
location.reload(true);
}
}catch(er) {
alert(er);
}

This was selected as the best answer
ArrowArrow

Thanks Solees, your solution works perfectly