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
karol.freebergkarol.freeberg 

System.StringException: Invalid Id error in For loop.

I am getting the following error and I am at a loss on what is wrong. Can anyone please help? The line of code with the error is underlined below.

11:00:01.063 (63040986)|USER_DEBUG|[23]|DEBUG|---------------------------------rows 3
11:00:01.063 (63047571)|SYSTEM_METHOD_EXIT|[23]|System.debug(ANY)
11:00:01.063 (63058551)|SYSTEM_METHOD_ENTRY|[26]|LIST<User>.iterator()
11:00:01.063 (63228660)|SYSTEM_METHOD_EXIT|[26]|LIST<User>.iterator()
11:00:01.063 (63259893)|SYSTEM_METHOD_ENTRY|[26]|system.ListIterator.hasNext()
11:00:01.063 (63295436)|SYSTEM_METHOD_EXIT|[26]|system.ListIterator.hasNext()
11:00:01.063 (63545352)|FATAL_ERROR|System.StringException: Invalid id:

List<String> userAcctNames = new List <String>();
       
        // Get all users modified in the last 3 days  
        User[] upusers = [Select ID, Department, Account_Name__c, contact_created__c,
                    LastModifiedDate From User Where LastModifiedDate >= :ckdt ];

        system.debug('---------------------------------rows ' + upusers.size());
       
       
        for (User theuser : upusers) {
             
            // Collect the IDs                                                                                 
            if (theuser.id != '') {
                userids.add(theuser.id);
                userAcctNames.add(theuser.Account_Name__c);
                system.debug('--------------------------------added ' + theuser.id + theuser.account_name__c);            
            }  //End if for collection of ids
         }  // end for loop
Best Answer chosen by karol.freeberg
karol.freebergkarol.freeberg
I changed the code as follows and got rid of the error. I guess the for loop just did not like the way the list was created. List upusers = [Select ID, Department, Account_Name__c, contact_created__c, LastModifiedDate From User Where LastModifiedDate >= :ckdt ]; for (User theuser:upusers) { ….

All Answers

sam sammsam samm
Can you print the ids after the loops

And where are you adding this?
userids.add(theuser.id);
karol.freebergkarol.freeberg
Since the code errors on the for (User theuser:upusers) line, I never get to the debug to display the IDs. The adds for adding to lists are valid, just did not show the initialization of those lists since again that is not where the error is occurring. In fact I commented out those lines just to try and debuf it further. This should be just a simple SOQL query for users and then using a for loop to itinerate the list. I am new at this but I have coded those same lines with an opportunity and it worked fine. I just don’t understand what is wrong with this one.
karol.freebergkarol.freeberg
I changed the code as follows and got rid of the error. I guess the for loop just did not like the way the list was created. List upusers = [Select ID, Department, Account_Name__c, contact_created__c, LastModifiedDate From User Where LastModifiedDate >= :ckdt ]; for (User theuser:upusers) { ….
This was selected as the best answer
TC DeveloperTC Developer
waht is the data type of userids list change it to List<sting> userids it will work