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
lodoss118lodoss118 

does the ajax toolkit handle batch updates of mass records?

can it for example update more than 5k accounts etc in batches
werewolfwerewolf
Yes, you can update them in batches of...well, I've forgotten the limit, but I believe it's 100.  It's in the API developer's guide whatever that limit is.
lodoss118lodoss118
but how do i update them in batches? sorry for being a noob :(
werewolfwerewolf
Just make an array of SObjects with the desired changes, ensure that array has less than the maximum number (which I hypothesize is 100), and pass that array to the update call.
lodoss118lodoss118
but isn't that the probllem i want to update more than 200 at one time?
werewolfwerewolf
Well you have to group them into batches of 200 to update them.  That's how it works.


Message Edited by werewolf on 06-27-2008 02:08 PM
sfdcfoxsfdcfox
I actually did that with a custom Delete List Button. Here's the code as an example:

Code:
{!RequireScript("/soap/ajax/10.0/connection.js")}
var IdsToDelete, successes, errors, successS, errorS, results, msg, subIdsToDelete, subIndex
IdsToDelete = {!GetRecordIDs($ObjectType.Lead)}
if(IdsToDelete.length == 0)
alert('Error:\n\nYou must choose at least one record to delete.')
delRecords = false
if(IdsToDelete.length > 0)
delRecords = confirm('Warning:\n\nYou are about to delete '+IdsToDelete.length+' record'+
(IdsToDelete.length==1?'':'s')+'.\n\nAre you certain you wish to continue?')
results = null
successes = errors = subIndex = 0
if(delRecords)
{ while(subIndex <= IdsToDelete.length)
{ subIdsToDelete = IdsToDelete.slice(subIndex, Math.min(subIndex+200,IdsToDelete.length))
results = sforce.connection.deleteIds(subIdsToDelete)
while(results.length)
if(results.pop().getBoolean('success'))
successes++
else
errors++
subIndex += 200
}
msg = ''
successS = successes==1?' was':'s were'
errorS = errors==1?' was':'s were'
if(successes > 0)
msg += successes+' record'+successS+' deleted'
if(errors > 0)
{ if(msg != '')
msg += ', and '
msg += errors+' record'+errorS+' NOT deleted'
}
msg += '.'
alert(msg)
window.top.location.href = window.top.location.href
}

 



Message Edited by sfdcfox on 06-28-2008 07:22 PM
lodoss118lodoss118
thanks guy i really appreciate the help also thanks for the source example, i was also wondering is this possible to do in apex yet?
werewolfwerewolf
Yes, you can do this in Apex, although there are batch limits in Apex also and overall governor limits which may foil you.
lodoss118lodoss118
are there any example on how i would do this in apex, for example if i query

Account[] accList = [SELECT Id, Name FROM Account];

if there are more than 200 how i do batch them for update

update accList; ??


Message Edited by lodoss118 on 06-30-2008 08:27 AM
sfdcfoxsfdcfox

 To handle a large number of records, use the for-list language construct-- this allows you to iterate using "queryMore" (internally) to efficiently handle a large number of records (governor limits apply). For example:

Code:
for(Account[] accs:[select id,industry from account])
{   for(Account acc:accs)
    {   acc.Industry='';
    }
    update accs;
}

 

This queries 200 records at a time, updates all 200 in-memory records, and then writes the updates back to Salesforce (don't do this unless you really want to blank all your industry values). Note that there would be an upper cap of how many records you could update (Limits.getLimitDMLRows() records or Limits.getLimitDMLStatements()*200, whichever is lower, but can't exceed 20,000).

lodoss118lodoss118
ok how would i update says if the query return 20k records then wouldn;t i need to batch group them?
sfdcfoxsfdcfox
If your query goes above the governor limits, you'll simply get an error and all the changes will roll back. If you need to upate more than what's allowed, you would need to then change the function to a web services call and use the AJAX toolkit in that case... or use the @future annotation so you can run the call asynchroniously, which has a much higher governor limit. If you need more than what an async call can do, then you definately should consider using the AJAX toolkit. Generally, if you're needing to use a large number of records at once, you're probably designing whatever it is you're trying to do wrong.