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
SFDC developer999SFDC developer999 

CPU time limit exceeded

I have the following code that get 'Apex CPU time limit exceeded'

 List<USER>  userList = new List<USER>();
  
   for (USER  aUser : [SELECT u.city, u.id FROM User  u where u.state = 'NEW YORK']  )  { 
        aUser.city = 'New York';      
        userList.add(aUser);
  }
 update  userList;
 
Per recommendations  below:

// Use this format for efficiency if you are executing DML statements // within the for loop. Be careful not to exceed the 150 DML statement limit. Account[] accts = new Account[];
for (List<Account> acct : [SELECT id, name FROM account WHERE name LIKE 'Acme']) {
  // Your logic here
accts.add(acct);
}
update accts;

, I revised the query as follows

   List<USER> userList = new List<USER>();

for (List<USER> user : [SELECT u.id FROM User u where u.state = 'NEW YORK']) {
  ...
    userList.add(user);
}
 
but get this error: 

 
Method does not exist or incorrect signature: void add(List<User>) from the type List<User>

Please tell me why. thanks a lot
Soumyodeep Guha 8Soumyodeep Guha 8
try this
for (USER> u : [SELECT id FROM User where state = 'NEW YORK'])
v varaprasadv varaprasad
Hi ,

Please check once following code : 
 
for (USER user : [SELECT u.id FROM User u where u.state = 'NEW YORK']) {
  ...
    userList.add(user);
}

Hope this helps you!

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com
 
Shamsi 110Shamsi 110
Change you code a bit.
  
List<USER> userList = new List<USER>();

for (USER user : [SELECT u.id FROM User u where u.state = 'NEW YORK']) {
  ...
    userList.add(user);
}

Kindly mark my answer if it helps you.
Thanks,
Hasan Shamsi