0

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()
4
  • It seems to me that this logic would be better to put in a page with standardController="Purchase_Order__c" so you can write the logic in Apex and test it.
    – Adrian Larson
    May 28, 2015 at 23:03
  • Hi Adrian, the thing is that if I just overwrite the field it works just fine, the problem is when I try to sum 10 to whatever value is already there.
    – Carlos
    May 28, 2015 at 23:14
  • That's a pretty big difference between what you were asking and the solution. You switched from incrementing to multiplying. If you start with null or 0 and do that many times in a row, the result will still be 0, which is very different behavior than your initial ask.
    – Adrian Larson
    May 29, 2015 at 17:12
  • Hi Adrian, sorry! my bad I updated the script, it should be sum. this made the magic result.records[i].deletethis__c. Thank you for your help! Have a great weekend!
    – Carlos
    May 29, 2015 at 17:19

3 Answers 3

3

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 :)

1

You never query for the field deletethis__c and so you cannot increment it. Add it to your query and your code should work. You might want to also convert falsy values to 0.

var value = child.deletethis__c;
if (!value) value = 0;
child.deletethis__c = value + 10;
3
  • Adrian, I added deletethis__c to the query and I modified the code as above but the value is not incrementing.. any ideas why? thank you in advance! please see the updated script above.
    – Carlos
    May 28, 2015 at 23:31
  • Are you getting any errors? Maybe throw in alert(result)?
    – Adrian Larson
    May 28, 2015 at 23:34
  • the alert for result says id='whatever', success=true, but I checked the values and nothing changed.
    – Carlos
    May 28, 2015 at 23:37
1

1) you dont query the field you are updating

2) just remember that javascript is case sensitive and salesforce is case insensitive

3) move the update ajax call outside of the for loop

4) the update call returns an error message if it fails, you can use that to help you figure out what isnt working

1
  • user2610036, thank you for your response, please see the updated script above.
    – Carlos
    May 28, 2015 at 23:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .