1

I got this exception while running following script in developer console.I need to update account records by using this script.

YearExt yr = new YearExt();
 List<Id> accountIds = new List<Id>();
 List<Account> accounts = new List<Account>();
 list<Custom_object__c> conlist=[select Contact__c,Contact__r.Account.ID from Custom_object__c    where Date__c>= :yr.startDate AND
 Date__c<= :yr.endDate];
  for(Custom_object__c c :conlist){
accountIds.add(c.Contact__r.Account.ID);
}
if(accountIds.size()>0){
List<Account> acclist=[SELECT Id, Field1__c, Field2__c
FROM Account where Id in :accountIds];
for(Account acct : acclist)
 {
        acct.Field1__c = true;
       acct.Field2__c =true;

        accounts.add(acct);
   }
   }    
    update accounts;

However,I have put LIMIT 100 in the below line and the script executed without any exception.But I need to update all the account records. How can I do this?Is there any alternate way of updating the records without these scripts.

 list<Custom_object__c> conlist=[select Contact__c,Contact__r.Account.ID from Custom_object__c    where Date__c>= :yr.startDate AND
 Date__c<= :yr.endDate LIMIT 100];
1

One way to just accomplish this using the limit is to exclude from the query records that have already been processed. You can then run the script in the developer console many times to work through all the records chunk by chunk:

list<Custom_object__c> conlist=[
    select Contact__c, Contact__r.Account.ID
    from Custom_object__c
    where Date__c>= :yr.startDate
    and Date__c<= :yr.endDate
    and (Contact__r.Account.Field1__c != true or Contact__r.Account.Field2__c != true)
    limit 100
    ];
  • Then how can I assure if all the accounts are updated? – user12202 Oct 8 '14 at 8:15
  • @user12202 Each time the query is run it will find the next 100 Contacts that have un-updated Accounts and update the related Accounts. You'll know you are finished when zero Contacts are returned meaning that all the Accounts are updated. (Hopefully you can actually use a much bigger limit value - experiment.) – Keith C Oct 8 '14 at 8:16
  • @ Keith C Thanks it is working.But after some time the record is not getting updated though it still has some records that has acct.Field1__c !=true and acct.Field2__c !=true.Please help. – user12202 Oct 9 '14 at 10:47
  • @user12202 Running the script from the developer console will update records there that match at that time, but records added or edited later won't be handled. For that you will need a trigger or scheduled solution. Is that your situation or are you saying that the script isn't working? – Keith C Oct 9 '14 at 11:00
  • there are existing records.No records are added or updated at this point of time.While start executing the script it updated upto 5000 records.After that there still some 6000 records those need to be updated.No matter how many times I run the script there is no change in the unupdated records.Its strange .I dont find any reason for this. – user12202 Oct 9 '14 at 12:04
1

If you need to update all the accounts, then you should move this code into an apex batch job

refer here for details on how to implement a batch with samples

basically you need to move your soql query into the start method and move the update part into the execute method of the batch..

this will ensure not to update all accounts in one go.. but to do it in multiple batches that salesforce can manage..

you can then either schedule the batch or execute it from your developer console

0

CPU time will be calculated for a transaction. So after you update the accounts all the before and after triggers on account will be in the same transaction. So some where in that code you may have lots of loops and some library method calls which may not be the optimized code... To see how much amount of time taken for a library method call, for loop and logic execution time, set the filters of the debug logs to finest for System/Apex and rest of logs to none. So that you can see the execution time for each of the mehod , custom logic, library call... Based on this time of execution i.e in milliseconds you can fix the code to complete the block in less time.

Still you can optimized your code like this.. But it may not be working solution in your case

YearExt yr = new YearExt();
 Set<Id> accountIds = new Set<Id>();
 List<Account> accounts = new List<Account>();
  for(Custom_object__c c :[select Contact__c,Contact__r.Account.ID from Custom_object__c    where Date__c>= :yr.startDate AND
 Date__c<= :yr.endDate]){
accountIds.add(c.Contact__r.Account.ID);
}

for(Account acct : [SELECT Id, Field1__c, Field2__c
FROM Account where Id in :accountIds])
 {
        acct.Field1__c = true;
       acct.Field2__c =true;

        accounts.add(acct);
   }

    update accounts;

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.