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
ChopaForceChopaForce 

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.

Best Answer chosen by Admin (Salesforce Developers) 
Avidev9Avidev9

Seems Like  the issue lies in the comparison you are doing, you cant compare a Id with a String.

 

Replace the Line 

 if(c.Account.Portal_Parent_Account__c !='' && c.Account.Portal_Parent_Account__c != null)

With Just

If(c.Account.Portal_Parent_Account__c != null)

All Answers

Avidev9Avidev9

Seems Like  the issue lies in the comparison you are doing, you cant compare a Id with a String.

 

Replace the Line 

 if(c.Account.Portal_Parent_Account__c !='' && c.Account.Portal_Parent_Account__c != null)

With Just

If(c.Account.Portal_Parent_Account__c != null)
This was selected as the best answer
ChopaForceChopaForce

Thanks a lot, it's working now. 

sfdcfoxsfdcfox
It should be noted that an ID field can never have an empty string value. The only valid ID values are 15 or 18 characters. Of course, if there is no value, it will still be null.