1

I have to run manual one time script on produciton for mass updating accounts. I would need help for best way so that below scripts would not hit apex limits. Is there any other way by which i would just need to run the script just once and it take care of apex limits

for(Account acc: [select id,name,newname__c from account limit 10000])
{
acc.newname__C=name;

 }
update acc;
1

sfdcfox's answer works well when you have a situation where a field is null, and you want to populate that field.

If you don't have a field, or already updated some records and you realized that you made a mistake somewhere, there is still hope.

You can add a filter to your query that checks who modified a record last (lastModifiedBy), and when it was last modified (lastModifiedDate).

// For LastModifiedDate, it's easiest if you include your own timezone offset.
// The format is 'YYYY-MM-DD' + 'T' + 'HH:MM:SS' + '+|-' + 'HH:MM'
// Retain leading zeros
// I'm in UTC-5 land right now, adjust as appropriate
List<Account> records = [SELECT Name FROM Account WHERE LastModifiedBy.Alias = 'DerekF' AND LastModifiedDate <= 2018-04-01T12:00:00-05:00 limit 10000];
for(Account record: records) {
  record.newname__c = record.Name;
}
update records;
System.assertNotEquals(0, records.size(), 'No more records to process');

For this to work, you do need a constant, unchanging point in time to use. The idea is that when you update the record again, its LastModifiedDate will be updated to a point after that cutoff time. This keeps you from updating the same records over and over.

  • Thanks Derek. You suggestion is great, i would keep it with me. Also sfdcfox's answers will also work great as i have field whose value is null. – David Mycka Apr 1 '18 at 18:40
4

Unfortunately, you can't do it that easily, unless you've prepared ahead of time. You'll need to execute the code over and over again until you're done. This is how I'd do it:

Account[] records = [select name from account where newname__c = null limit 10000];
for(Account record: records) {
  record.newname__c = record.Name;
}
update records;
System.assertNotEquals(0, records.size(), 'No more records to process');

Just press the Ctrl-Alt-E hotkey over and over and over again until you get a failed assertion. Note that if you have any triggers or process builder, etc, you'll need to adjust the 10,000 row limit accordingly.

0

Probably easiest and best way for you is to use Data Loader.

Firstly you'll need export accounts that you need to csv file (so you'll have id of them) and then edit them in a way that you want and import again.

Or if you have some external id just create csv file with updated fields

  • Thanks User1974566, but we are not allowed to connect dataloader to production. – David Mycka Apr 1 '18 at 17:04
  • The Data Import Wizard supports Accounts. You could use Workbench or Reports to export a CSV of Account ID and name, and then re-import it in update mode pointing the name column to the new field. You can do up to 50K records at a time. – Charles T Apr 2 '18 at 12:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.