+ Start a Discussion
InternalServerErrorInternalServerError 

Best Practice on Inserting/updating Null values

Hi all,

 

I recently created a trigger that inserts/updates some values taken from one record into another record on another object and this is working fine. However, some of the fields in the "source" object are not required, therefore very often some of these fields are blank. This causes the fields on the target record to be inserted with a value saying "null" which doesn't look good.

 

What is the best way of avoiding the 'null' text to appear when the source field has no value?

 

I know I could create many IF statements to insert certain fields only when they are not null but if I have many field this would get very messy....

 

Thanks in advance

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

It sounds like you're using String.valueOf() or string concatenation (the "+" operator), which indirectly calls String.valueOf(). This would cause that problem to occur. Instead, assign the value directly, or use an if-then or ternary operator within your existing expressions. A ternary operator structure might look like this:

 

String abc = 'Hello, '+(world==null?'':world);

Where the first part is a condition, as you'd put in to an IF condition clause, followed by a question mark ("?"), followed by a value to return if the condition is true, followed by a colon (":"), followed by a value to return if the condition is false.

 

Use ternary operators sparingly, as they can be confusing to read, but use them to great effect when you want to eliminate a great number of if-then-else clauses. Alternatively, just use a function that does the same thing:

 

private String getBlankIfNull(String source) {
    if(source==null) {
        return '';
    }
    return source;
}

 

All Answers

sfdcfoxsfdcfox

It sounds like you're using String.valueOf() or string concatenation (the "+" operator), which indirectly calls String.valueOf(). This would cause that problem to occur. Instead, assign the value directly, or use an if-then or ternary operator within your existing expressions. A ternary operator structure might look like this:

 

String abc = 'Hello, '+(world==null?'':world);

Where the first part is a condition, as you'd put in to an IF condition clause, followed by a question mark ("?"), followed by a value to return if the condition is true, followed by a colon (":"), followed by a value to return if the condition is false.

 

Use ternary operators sparingly, as they can be confusing to read, but use them to great effect when you want to eliminate a great number of if-then-else clauses. Alternatively, just use a function that does the same thing:

 

private String getBlankIfNull(String source) {
    if(source==null) {
        return '';
    }
    return source;
}

 

This was selected as the best answer
InternalServerErrorInternalServerError

Indeed, I am using string concatenation inserting many fields values on the same text field, I think the ternary operator will be the solution. Thanks!