0

I am having 3.5k records but while executing the code i get

System.LimitException: Apex CPU time limit exceeded

Although i need to update only 360 records whose duplicate__c is true.

List<Account> act = new List<Account>();
for(Account accou: [select name,duplicate__c from account where duplicate__c=true])
{
    accou.duplicate__c=false;
    act.add(accou);
        update act;

}
  • You are executing inside a for look, each DML may cause additional triggers, why not use dataloader for this operation? – Raul Jul 4 at 11:32
  • 2
    360 record updates per transaction could fail with CPU exception if you have downstream triggers, wfr, process builders, flows -- you can gain some insight into where the CPU time goes by using the Developer Console profiling feature or just looking at the debug log. If this is a 1 time anon apex script, run it multiple times with a Limit x as part of the SOQL – cropredy Jul 4 at 18:42
1

You need to update records in bulk.

You cannot perform more than 150 DML operations in a single transaction. as you have written update stmt in a for loop, you ran into an issue. Just move it outside of for loop.

List<Account> act = new List<Account>();
for(Account accou: [select Id,name,duplicate__c from account where duplicate__c=true])
{
    accou.duplicate__c=false;
    act.add(accou);
}
update act;

Again you can update max 10000 records in a single transaction. In your case, there are 3.5k records only, so it should work.

Also, this will be a very helpful article to know how to bulkify your code.

Apex Governor Limits

  • i try your approach and move soql outside for loop still the error remain same. List<Account> act = new List<Account>([select name,duplicate__c from account where duplicate__c=true]); for(Account accou: act) {....... – Palash Jul 4 at 11:54
  • can you paste error in question? – rahul gawale Jul 4 at 11:56
  • Line: 95, Column: 1 System.LimitException: Apex CPU time limit exceeded – Palash Jul 4 at 12:02
  • 1
    You do not need the Id in you query as it is automatically returned. The only time you need Id is if you are editing from the query table within the dev console tool palette – Eric Jul 8 at 7:00
0

Since i cannot update all query at once so i need to use limit

List<Account> accouUpd= new List<Account>();
List<Account> act = new List<Account>([select name,duplicate__c from account where duplicate__c=true limit 80] );
for(Account accou: act)
{
    accou.duplicate__c=false;
    accouUpd.add(accou);
        update accouUpd;

}

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.