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
iSfdciSfdc 

Update outside a for loop

Hi,
I am trying to update the Opportunity Line Items with two Custom field values. When one Opportunity has multiple Products, I need to update all the Products with the two Custom fields. I am updating inside a for loop , it works fine. But I am not able to update all the line Items in a Single go outside the for loop, so that one time update would happen.
 
Snippet:

for(var i =0;i<records2.length;i++)

var c= new sforce.SObject("OpportunityLineItem");
c.Id=records2[i].Id;
c.Amount_On_First_Year__c=amountyear;
c.Quantity_on__c=quantyear;
result2= sforce.connection.update([c]);
 
if (result2[0].getBoolean("success")) {
count++;
}
}
 
Is there anyway to Update all the Records outside the for loop, any pointers would help.
 
 
Gani
Greg HGreg H

I think this is what you are looking to do:

Code:
var SingleUpdateArray = new Array();
for (var i = 0; i<records2.length; i++) {
 var c = new sforce.SObject("OpportunityLineItem");
 c.Id = records2[i].Id;
 c.Amount_On_First_Year__c = amountyear;
 c.Quantity_on__c = quantyear;
 SingleUpdateArray.push(c);
}
var result2 = sforce.connection.update(SingleUpdateArray);

It should at least get you pointed in the right direction,

-greg

iSfdciSfdc
Thank you !! Its much useful.