You need to sign in to do that
Don't have an account?

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.
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.
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]); }
If you wanted to set them all to false you should do something more like this:
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
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.
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?