You need to sign in to do that
Don't have an account?

Apex version of NULLVALUE? (convert nulls to empty string for example)
Has anyone wrote a null replacement apex method for strings? I keep getting the error, attempting to dereference null. Similar in behavior to the formula NULLVALUE(expression, substitute_expression).
In this example I am concatenating a contact first and last name together. But the first name is often null. the string then becomes null lastname instead of just last name
if(firstName == null){
firstName = '';
}
if(lastName == null){
lastName = '';
}
fullName = firstName + ' ' + lastName
You can insert something like the following in your class:
You can then "overlay" the same function with other data types as needed:
You may wish to include this in a "support class" of some sort, and then instantiate it in
your other projects for later re-use:
// last name is a required field; it could also be used for a business name
string nm = con.LastName;
if (con.FirstName != null) {
nm = con.FirstName + ' ' + con.LastName;
// or... nm += ', ' + con.FirstName;
}
Convert a suspected null to an empty, or keep the original string:
String EmptyOrOriginal = SuspectNull == null ? '' : SuspectNull;