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

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

3 Answers 3

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))
2
  • 1
    Interesting... The documentation shows otherwise: salesforce.com/us/developer/docs/apexcode/Content/…. I think you might be thinking setting up formulas in SFDC? Apr 1, 2015 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. Apr 1, 2015 at 19:35
0

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

1
  • isEmpty is not the same as isBlank. isBlank evaluates if all characters in the string are whitespace. Apr 1, 2015 at 19:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .