Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

An as example, say I have a trigger (list object) with accounts in it, and I want to compare the "Status" attribute of an account to a String. This is how I do it right now:

if (trigger.oldMap.get(account.id).Status__c != null && trigger.oldMap.get(account.id).Status__c.contains('active')) {}

My true purpose is simply to execute the second expression, but in the case that the current account did not have a Status, I would be returned a NPE because of the compareTo call. So I add in a not null check first which seems like a terribly inefficient way of doing things.

How can this be done better?

share|improve this question
2  
Terribly inefficient way of doing things? Certainly not in terms of performance. In terms of code length you could clean this up by storing Trigger.oldMap.get(account.Id) in a variable. –  Phil Rymek Jan 29 '13 at 22:19
    
It doesn't gain any efficiency, but you could check out the apex-lang commons, specifically the StringUtils. If you find yourself implementing the same types of private utility functions for null check and then do x, something like this (or your own) that unifies/centralizes functionality might help you not repeat yourself...of course it is another dependency... –  Peter Knolle Jan 30 '13 at 0:18
    
Nothing wrong with what you have, but my personal preference would be to use a ternary (or nested-ternary if necessary): if (trigger.oldMap.get(account.id).Status__c == null ? false : trigger.oldMap.get(account.id).Status__c.contains('active')) {} Its actually slightly longer in this case, but I think can be cleaner for more complex evaluations. Agree with @phil-r - you are more likely to be using a variable, in which case you might have: if (oldItem.Status__c == null ? false : oldItem.Status__c.contains('active')) {} –  Stephen Willcock Jan 30 '13 at 13:08

1 Answer 1

You could use isNotBlank or isNotEmpty instead (though I guess it amounts to the same as a null check)

if ( String.isNotBlank(trigger.oldMap.get(account.id).Status__c) &&  trigger.oldMap.get(account.id).Status__c.contains('active') ) {}

According to the docs, isNotBlank Returns true if the specified String is not whitespace, not empty (''), and not null; otherwise, returns false.

To code defensively you will have to do some kind of check, whether it be null checking or isBlank/isEmpty etc. if there is a chance that Status__c could be null.

What you could do is write your own static method to check and return '' if null? e.g.

static String makeNullString( String in ) {
  return in == null ? '' : in;
}

Then you could do this instead:

if (makeNullString(trigger.oldMap.get(account.id).Status__c).contains('active')) {}
share|improve this answer
    
compareTo was just an example, I have to use .contains() in most cases; using contains in an example would require more context, so I just used a generic string method. –  user1031 Jan 29 '13 at 20:07
    
Ok, that makes more sense I couldn't work out what you were trying to do. I guess there are variations on a theme of how to do it, but I don't think you will get away from having to do something. Updated answer slightly. –  Phil Hawthorn Jan 29 '13 at 20:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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