You need to sign in to do that
Don't have an account?
APEX - String Exception: Invalid ID
Hello,
I am trying to assign an Id to a string variable (parentId).
The logic is the following: if the Account of the Contact has a parent account then use the parent Account id otherwise read the Account Id.
The issue raises when I am assigning the Parent Account ID (c.Account.Portal_Parent_Account__c) to my variable.
I ran the query within the SOQL editor ( eclipse schema ) and it returned me with an id for the Portal_Parent_Account.
here's my code:
for(Contact c : [Select Account.Portal_Parent_Account__c,Account.Id from Contact where id =: contactid limit 1]) { if(c.Account.Portal_Parent_Account__c !='' && c.Account.Portal_Parent_Account__c != null) { parentid = c.Account.Portal_Parent_Account__c; system.debug('******************Parent ID IF:' + parentid); } else { parentid = c.Account.Id; system.debug('******************Parent ID ELSE:' + parentid); } }
Please note that I am not doing any insert or update before this code. I'm suspecting that maybe something is wrong when using 2 levels of hierarchy (parent of parent) ? Perhaps a lazy loading scenario ?
Thank you for reading my post.
Seems Like the issue lies in the comparison you are doing, you cant compare a Id with a String.
Replace the Line
With Just
All Answers
Seems Like the issue lies in the comparison you are doing, you cant compare a Id with a String.
Replace the Line
With Just
Thanks a lot, it's working now.