1

I am getting error saying'Loop must iterate over a collection type: User'.I am not able to find what actually error wants to say.

List<User> listUserDeactivate = new List<User>();
Set<ID> userDeactSet = new Set<ID>();
for(user u : listUserDeactivate){
    try{
        System.debug(':::::::inside try block ::::::::'+u);
        if(u!=null && !u.isEmpty()){

            //List<Database.SaveResult> results= Database.update(listUserDeactivate);
            for (user deactivateuser : u) {
                deactivateuser.IsActive = false;
            }
            update u;        
            System.debug(':::::::inside try block 2 ::::::::'+u);          
        }
        //System.debug(':::::::inside try block 2 ::::::::'+results);

    }catch(exception e) {

        System.debug('value of ids of deactivation users ::::: '+u);
        Set<Id> resultIds = (new Map<Id,User>(u)).keySet();
        System.debug('value of ids of exception users ::::: '+resultIds);
        userDeactSet.add(resultIds);          
    }                
}
3
  • Your list has no data in it when your loop begins. So, there is nothing to loop through.
    – smckitrick
    Jan 26, 2016 at 14:53
  • This is just example.I have data in list.Just for reference i kept like above.
    – Jack vardy
    Jan 26, 2016 at 15:02
  • You should move try/catch block and dml all out of the for loop. Take a look at some of the excellent questions on our exchange.
    – Adrian Larson
    Jan 26, 2016 at 15:45

1 Answer 1

2

Error in this line

for (user deactivateuser : u) {
       deactivateuser.IsActive = false;
 }

Here u is single record and you are tring to iterate ..

You can iterate list in for loop


Just do that u.IsActive = false.. Also you need to perform update DML outside of for loop

Create a list, once you make u.IsActive = false add record to list and outside of for loop perform DML

List<User> listUserToUpdate = new List<User>();
for(user u : listUserDeactivate){
    u.IsActive  = false;
    listUserToUpdate.add(u);
}

try{
    update listUserToUpdate;

}catch(exception e) {

             //other codes here
}                

instead try catch use Database.upsert for DML and store result in Database.SaveResult and get all the falut records

List<Database.SaveResult> updateResults;
updateResults = Database.upsert(listUserToUpdate, false);
for(Integer i=0;i<updateResults.size();i++){

   if (!updateResults.get(i).isSuccess()){
                // DML operation failed
                userDeactSet.add(listUserToUpdate.get(i).Id); 
                system.debug('Failed ID'+listUserToUpdate.get(i).Id);

    }
 }
system.debug('--------check---userDeactSet--'+userDeactSet);
8
  • Got it for one record issue but how can i update dml outside of for loop as i need to update it.I am using try - catch block and i need to update inside try block.Any alternative.
    – Jack vardy
    Jan 26, 2016 at 15:04
  • @RobertBarnett check my updated ans'
    – Ratan Paul
    Jan 26, 2016 at 15:08
  • @Ratan-I am using try catch statement inside for loop.So how can i keep dml statement outside of outer for loop (outside try-catch)
    – Jack vardy
    Jan 26, 2016 at 15:15
  • @RobertBarnett check my updated ans
    – Ratan Paul
    Jan 26, 2016 at 15:18
  • 1
    I am able to get id of failed records which solved my problem easily and made my code shorter.
    – Jack vardy
    Jan 26, 2016 at 16:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .