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
FGNYFGNY 

How to update changed saleforce objects at once?

Is there a possibility to update changed Saleforce objects at once? I wrote the following code which updates a field in a number of accounts, however the process takes much time, since each account is individually updated to salesforce after the change. Is there a possibility to change the objects first and then update them to salesforce at once.
Code:
var accounts = sforce.connection.query("SELECT Id, Name, Prospecting_Opps__c FROM Account WHERE Prospecting_Opps__c=true");
var rec = accounts.getArray("records");
alert(accs.size);
var j =0;
while (j<accs.size)
{
checkbox(rec[j].Id);
j++;
}

function checkbox(Accid)
{
var Acc = new sforce.SObject("Account");
Acc ["Id"] = Accid;
Acc ["Prospecting_Opps__c"] = "true";
sforce.connection.update([Acc]);
}

 
new.sforce.sObject returns an array and sforce.connection.update updates it to salesforce. Therefore those arrays need to be wrapped up in a structure like an "array of arrays". C++ for example knows two dimensional arrays caled matrix, but  as far I know Javascript don't know such element.
DevAngelDevAngel
First of all you are selecting accounts where Prospecting_Opps__c = true and then setting each one of those to true again.  :smileyindifferent:

If you wanted to set them all to false you should do something more like this:

Code:
var accounts = sforce.connection.query("SELECT Id, Name, Prospecting_Opps__c " +
"FROM Account WHERE Prospecting_Opps__c=true"); var rec = accounts.getArray("records"); for (var i = 0;i<rec.length;i++) { rec[i].Prospecting_Opps__c = "false"; } sforce.connection.update(rec);

Notice that I am using the length property of the accounts.getArray("records") array.  The size property of the queryResult is the full size of all available records, not the size of the array that was returned.  You could have a size of 50,000 but a single query will only return at most 2,000. 

Also, notice that I don't need to create new objects for each account returned in the query, they already exist in my query results.  We just need to modify the field that we are concerned with and since the id was part of our SOQL statement, it is already set as well.

Finally, we do a single update call passing in our modified array of objects.


Cheers
 

FGNYFGNY

Thanks, it works but It caused an other problem. My previous code was this one, it worked but extremely slow due to single record update to SF.

Code:
var opps = sforce.connection.query("SELECT Id, Account.Id, Account.Parent.Id, Account.RecordTypeId, 
Account.Prospecting_Opps__c FROM Opportunity WHERE Typeofopportunity__c IN ('SIS', 'WIS', 'SIS/WIS-REVAMPING') 
AND New_Forecast_Category__c IN ('Probable', 'Commit') AND CloseDate IN(" + document.Formular.feld.value + ")");

var rec = opps.getArray("records");

var i =0;
while (i<rec.length)
{
if (rec[i].Account.RecordTypeId == "012200000000ELxAAM")
{checkbox(rec[i].Account.Parent.Id.slice(0, 15));}
if (rec[i].Account.RecordTypeId == "012200000000ELsAAM")
{checkbox(rec[i].Account.Id.slice(0, 15));}
i++;

function checkbox(Accid)
{
var Acc = new sforce.SObject("Account");
Acc ["Id"] = Accid;
Acc ["Prospecting_Opps__c"] = "true";
sforce.connection.update([Acc]);
}

Now with this code which updates the whole array at once It reports an error with the rec[i].Account.Parent.Prospecting_Opps__c field (rec[i].Account.Prospecting_Opps__c workds fine). Does it mean the opportunity can't access the fields of parent account of the opportunitie's account and I still have to do it through creating new SForce object for this account?

Code:
var opps = sforce.connection.query("SELECT Id, Account.Id, Account.Parent.Id, Account.RecordTypeId, 
Account.Prospecting_Opps__c FROM Opportunity WHERE Typeofopportunity__c IN ('SIS', 'WIS', 'SIS/WIS-REVAMPING') 
AND New_Forecast_Category__c IN ('Probable', 'Commit') AND CloseDate IN(" + document.Formular.feld.value + ")");
var rec = opps.getArray("records");

var i =0;
while (i<rec.length)
{
if (rec[i].Account.RecordTypeId == "012200000000ELxAAM")
{rec[i].Account.Prospecting_Opps__c = "true";}
if (rec[i].Account.RecordTypeId == "012200000000ELsAAM")
{rec[i].Account.Parent.Prospecting_Opps__c = "true";
i++;

sforce.connection.update(rec);