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
Jonathan Osgood 3Jonathan Osgood 3 

Invalid Id Error calling map

Hi All,

I'm getting an invalid id error when I call userAccounts.get(p.Ownerid).AccountId; I think I may be populating my userAccounts map incorrectly?
 
list<ID> pubIds = new list<ID>();
        
        for(Publication__c p: pubList){ 
            pubIds.add(p.id);                      
        }
        
        list<ID> UserIds = new list<ID>();
        
        for(Publication__c p: pubList){ 
            UserIds.add(p.ownerid);                      
        }
        map<id,user> userAccounts = new map <id, user>([SELECT Id, Name, AccountId
                                                                                           FROM User 
                                                                                           WHERE Id IN :UserIds 
                                                                                           AND IsActive = true]);
        
        
        list<Publication__c> pubsToUpdate = [SELECT ID, ownerId
                               					     FROM Publication__c 
                               					      WHERE ID in: pubIds];
        
        for(Publication__c p: pubsToUpdate){
            
            p.Next_Object__c      = stepOneMap.get(p.Id).id;
            
 	          	if(userAccounts.get(p.OwnerId).AccountId !=''){
              
             	  	 p.Account__c = userAccounts.get(p.Ownerid).AccountId;
           			
            }
                             
                              }
        Update pubsToUpdate;

 
Best Answer chosen by Jonathan Osgood 3
Jonathan Osgood 3Jonathan Osgood 3
Thanks Amit,

I beleive the problem was due to my null check. I had used AccountId !='' when I should have used AccountId !=null.

This solved the "invalid ID" error.

Thanks for your help!
 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Add containsKey in your map.

pLease update your code like below
list<ID> pubIds = new list<ID>();
        
        for(Publication__c p: pubList){ 
            pubIds.add(p.id);                      
        }
        
        list<ID> UserIds = new list<ID>();
        
        for(Publication__c p: pubList){ 
            UserIds.add(p.ownerid);                      
        }
        map<id,user> userAccounts = new map <id, user>([SELECT Id, Name, AccountId
                                                                                           FROM User 
                                                                                           WHERE Id IN :UserIds 
                                                                                           AND IsActive = true]);
        
        
        list<Publication__c> pubsToUpdate = [SELECT ID, ownerId
                               					     FROM Publication__c 
                               					      WHERE ID in: pubIds];
        
        for(Publication__c p: pubsToUpdate)
		{
            
            p.Next_Object__c      = stepOneMap.get(p.Id).id;
            
 	          	if(userAccounts.containsKey(p.OwnerId) && userAccounts.get(p.OwnerId).AccountId !='')
				{
              
             	  	 p.Account__c = userAccounts.get(p.Ownerid).AccountId;
           			
				}
                             
        }
        Update pubsToUpdate;

Let us know if this will resolve your issue
Jonathan Osgood 3Jonathan Osgood 3
Thanks Amit,

This helps with my null check, but I'm still getting an invalid ID error:
 
17:26:16.0 (933895996)|STATEMENT_EXECUTE|[96]
17:26:16.0 (933898163)|STATEMENT_EXECUTE|[97]
17:26:16.0 (933968604)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4
17:26:16.0 (933994462)|HEAP_ALLOCATE|[96]|Bytes:5
17:26:16.0 (934016442)|VARIABLE_ASSIGNMENT|[96]|p|null|
17:26:16.0 (934022894)|STATEMENT_EXECUTE|[99]
17:26:16.0 (934105170)|HEAP_ALLOCATE|[99]|Bytes:4
17:26:16.0 (934115931)|HEAP_ALLOCATE|[99]|Bytes:79
17:26:16.0 (934127302)|HEAP_ALLOCATE|[99]|Bytes:4
17:26:16.0 (934143079)|HEAP_ALLOCATE|[99]|Bytes:7
17:26:16.0 (934655924)|SOQL_EXECUTE_BEGIN|[99]|Aggregations:0|SELECT Id, Name, AccountId FROM User WHERE (Id IN :tmpVar1 AND IsActive = TRUE)
17:26:16.0 (940122956)|SOQL_EXECUTE_END|[99]|Rows:1
17:26:16.0 (940149037)|HEAP_ALLOCATE|[99]|Bytes:8
17:26:16.0 (940169599)|HEAP_ALLOCATE|[99]|Bytes:83
17:26:16.0 (940230469)|HEAP_ALLOCATE|[99]|Bytes:8
17:26:16.0 (940317046)|HEAP_ALLOCATE|[99]|Bytes:16
17:26:16.0 (940327727)|HEAP_ALLOCATE|[99]|Bytes:4
17:26:16.0 (940339923)|VARIABLE_SCOPE_BEGIN|[99]|userAccounts|Map<Id,User>|true|false
17:26:16.0 (940386051)|VARIABLE_ASSIGNMENT|[99]|userAccounts|{"s":1,"v":{"005R00000028vYKIAY":{"s":2,"v":{"Id":"005R00000028vYKIAY","Name":"Jonathan Osgood","AccountId":"001R00000140tt1IAA"}}}}|0x76370694
17:26:16.0 (940391837)|STATEMENT_EXECUTE|[105]
17:26:16.0 (940396512)|HEAP_ALLOCATE|[105]|Bytes:95
17:26:16.0 (940406362)|HEAP_ALLOCATE|[105]|Bytes:4
17:26:16.0 (940695004)|SOQL_EXECUTE_BEGIN|[105]|Aggregations:0|SELECT ID, ownerId, Next_Object__c, Previous_Object__c FROM Publication__c WHERE ID IN :tmpVar1
17:26:16.0 (943193641)|SOQL_EXECUTE_END|[105]|Rows:1
17:26:16.0 (943216620)|HEAP_ALLOCATE|[105]|Bytes:8
17:26:16.0 (943233970)|HEAP_ALLOCATE|[105]|Bytes:58
17:26:16.0 (943255622)|HEAP_ALLOCATE|[105]|Bytes:8
17:26:16.0 (943268678)|VARIABLE_SCOPE_BEGIN|[105]|pubsToUpdate|List<Publication__c>|true|false
17:26:16.0 (943303957)|VARIABLE_ASSIGNMENT|[105]|pubsToUpdate|{"s":1,"v":[{"s":2,"v":{"Id":"a0dR0000004aGknIAE","OwnerId":"005R00000028vYKIAY"}}]}|0xbc10747
17:26:16.0 (943382763)|HEAP_ALLOCATE|[109]|Bytes:5
17:26:16.0 (943432827)|HEAP_ALLOCATE|[109]|Bytes:12
17:26:16.0 (943450138)|VARIABLE_SCOPE_BEGIN|[109]|p|Publication__c|true|false
17:26:16.0 (943472176)|VARIABLE_ASSIGNMENT|[109]|p|{"s":1,"v":{"Id":"a0dR0000004aGknIAE","OwnerId":"005R00000028vYKIAY"}}|0x1d4d3abe
17:26:16.0 (943479197)|STATEMENT_EXECUTE|[109]
17:26:16.0 (943481901)|STATEMENT_EXECUTE|[111]
17:26:16.0 (943638774)|HEAP_ALLOCATE|[111]|Bytes:-4
17:26:16.0 (943656069)|VARIABLE_ASSIGNMENT|[111]|this.Next_Object__c|"a0fR00000041d4BIAQ"|0x1d4d3abe
17:26:16.0 (943946097)|HEAP_ALLOCATE|[113]|Bytes:16
17:26:16.0 (943966058)|METHOD_EXIT|[4]|01pR0000000M6fY|publicationTriggerHelper.createStepObjects(List<Publication__c>)
17:26:16.0 (944124639)|FATAL_ERROR|System.StringException: Invalid id: 

Class.publicationTriggerHelper.createStepObjects: line 113, column 1
Trigger.createPublicationSteps: line 4, column 1
17:26:16.0 (944150465)|FATAL_ERROR|System.StringException: Invalid id: 

Class.publicationTriggerHelper.createStepObjects: line 113, column 1
Trigger.createPublicationSteps: line 4, column 1
17:26:16.944 (944224022)|CUMULATIVE_LIMIT_USAGE
17:26:16.944 (944224022)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 2 out of 100
  Number of query rows: 2 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 10 out of 150
  Number of DML rows: 10 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

17:26:16.944 (944224022)|CUMULATIVE_LIMIT_USAGE_END

17:26:16.0 (946117379)|CODE_UNIT_FINISHED|createPublicationSteps on Publication trigger event AfterInsert for [a0dR0000004aGkn]
17:26:16.0 (947400636)|EXECUTION_FINISHED

 
Amit Chaudhary 8Amit Chaudhary 8
You are using right object Id in below line ? . Please check stepOneMap Value and Next_Object__c field data type
            p.Next_Object__c      = stepOneMap.get(p.Id).id;

I hope account__c is account lookup only ?
                        p.Account__c = userAccounts.get(p.Ownerid).AccountId;



 
Jonathan Osgood 3Jonathan Osgood 3
Thanks Amit,

I beleive the problem was due to my null check. I had used AccountId !='' when I should have used AccountId !=null.

This solved the "invalid ID" error.

Thanks for your help!
 
This was selected as the best answer