Following are the 2 lines of my controller code,

system.debug('id is ' + FSInputRecord.Client__c);
if(FSInputRecord.Client__c != null && FSInputRecord.Client__c != '')

Where FSInputRecord.Client__c is a lookup to Contact which gets its value from the VF page.

Now the first line gives me a valid ID e.g. 00355000009YTSrAAO But the second line throws exception - System.StringException: Invalid id:

Why so? what is the problem with the ID here?

Also when I leave the FSInputRecord.Client__c input lookup field empty then the second line doesn't throw any error.


Because Client__c is of the Id type, the empty string ('') is being coerced to an Id, which is not a valid Id. You never need to compare an Id to an empty string, because that is not a valid state for an Id to be in. The error doesn't occur when Client__c is null, because the && operator is a short-circuit operator (if the left side is false, the right side is never evaluated).

  • 1
    I believe I'm reading it incorrectly, did you mean "if the left side is false , the right side is never evaluated"? Just confused... – Mahmood Feb 24 '17 at 20:59
  • @Mahmood Oops. You're right. That's what I meant. Thanks for pointing that out. – sfdcfox Feb 24 '17 at 21:04

I think the problem is because of this FSInputRecord.Client__c != ''.Can you try removing this and see if this solves your problem.Let us know if the issue is still there.


Check this post from Deepak Anand, explains why Empty Strings throw an error for ID.

EMPTY STRING - eventhought it doesn't have any value but an EMPTY STRING https://success.salesforce.com/answers?id=90630000000hTOsAAM


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.