[SalesForce] Solved! Update children record using Javascript in button

I am trying to update a field in children records(Purchase_Order_Details__c) with Javascript in a button from the parent level(Purchase_Order__c). My problem is that I need to sum 10 to the field Purchase_Order_Details__c.deletethis__c. This is not working. Can anyone please help with this? I will really appreciate any help. thank you!

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

 var updateRecord = new Array();

 // get children records
 var qr = sforce.connection.query("SELECT Id FROM Purchase_Order_Details__c where Purchase_Order__c = '{!Purchase_Order__c.Id}' ");
 var records = qr.getArray("records");

 for (i=0; i < records.length ; i++)
 {
 var child = records[i];
 //here is the problem
 child.deletethis__c = child.deletethis__c + 10 ; //this field + 10

 updateRecord.push(child);

 result = sforce.connection.update(updateRecord);
 }

 //refresh the page
 window.location.reload()

WORKING VERSION

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

 var updateRecord = new Array(); 

 //get children id related to this parent 
 var result = sforce.connection.query("SELECT Id, deletethis__c FROM   Purchase_Order_Details__c where Purchase_Order__c = '{!Purchase_Order__c.Id}' "); 
 var records = result.getArray("records"); 

 for (i=0; i < records.length ; i++) 
 { 
 var child = result.records[i]; 

 var value = result.records[i].deletethis__c; 

 if (!value) value = 0; 
 child.deletethis__c = value + 10; 


 updateRecord.push(child); 

 } 

 result = sforce.connection.update(updateRecord); 


 //refresh the page 
 window.location.reload()

Best Answer

Have you tried to use Flow? This seems like a really great use case for Flow... you can easily do this with just clicks.

Create a Fast Lookup to grab all of the Child Accounts (ParentId = Id), then make your calculations via a Loop and Assignment element. Finish it off with a Fast Update :)

Related Topic