1

Hopefully this is a simple one, but I'm pretty stumped. Basically, I'm editing a text field of a custom object. However, it seems I get a Invalid Foreign Key Relationship error when I try and check whether the field is null:

Here's the code that throws the exception:

for(Account a: listOfAccounts{
    for(object__c o: listOfObjects){
        if(!o.Name.isBlank()){
             o.Name.replace(a.textField__c, '');
        }
    }
}

Not sure why, since this works:

for(Account a: listOfAccounts{
    for(object__c o: listOfObjects){     
        o.Name.replace(a.textField__c, '');
    }
}

However, this code throws a null pointer exception if o.Name is null

  • Well, I narrowed it down to using the .isBlank() method... Still now sure why it's throwing an error :/ – Doodereeno66 Apr 1 '15 at 18:48
1

isBlank is a static method of the String class. You want:

if(!String.isBlank(o.Name)){
         o.Name.replace(a.textField__c, '');
}
0

It's the way you have the 'Is Not Blank' set up

!o.Name.isBlank()

Should be

NOT(ISBLANK(o.Name))
  • 1
    Interesting... The documentation shows otherwise: salesforce.com/us/developer/docs/apexcode/Content/…. I think you might be thinking setting up formulas in SFDC? – Doodereeno66 Apr 1 '15 at 18:56
  • @Danomyte66 Yeah sorry about that, I've been deep in with workflows and they validate based on the formula's. Speaking of which, you could probably accomplish this same thing with a workflow rule. – Cody Beaner Apr 1 '15 at 19:35
0

Looks like the answer is !String.isEmpty(o.Name) (as it states in the documentation :/)

  • isEmpty is not the same as isBlank. isBlank evaluates if all characters in the string are whitespace. – Brian Mansfield Apr 1 '15 at 19:13

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.