function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ChrisMcLChrisMcL 

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
TehNrdTehNrd
What about before the concatenation check to see if the first name is null and if it is give it a string value of nothing.

if(firstName == null){
firstName = '';
}

if(lastName == null){
lastName = '';
}

fullName = firstName + ' ' + lastName
ChrisMcLChrisMcL
This is what I've done to get around this for now:
Code:
if (Trigger.old[i].FirstName == null) {
   contactNameOld = Trigger.old[i].LastName;
 } else {
   contactNameOld = Trigger.old[i].FirstName + ' ' + Trigger.old[i].LastName;
 } 

 

KevinBrKevinBr

You can insert something like the following in your class:
// replace arg1 with arg2, if arg1 is null
public string ifnull(string s1,string s2) {
  string result = s1;
  if (s1 == null) { result = s2; }
  return result;
}
You can then "overlay" the same function with other data types as needed:
public integer ifnull(integer s1,integer s2) {
  integer result = s1;
  if (s1 == null) { result = s2; }
  return result;
}
public boolean ifnull(boolean s1,boolean s2) {
  boolean result = s1;
  if (s1 == null) { result = s2; }
  return result;
}

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:

public with sharing class apexCodingUtilities () {

  // ----------------------------------------------
  // IFNULL :: replace arg1 with arg2, if arg1 is null
  //----------------------------------------------
  public string ifnull(string s1,string s2) {
    string result = s1;
    if (s1 == null) { result = s2; }
    return result;
  }
  // ----------------------------------------------
  public integer ifnull(integer s1,integer s2) {
    integer result = s1;
    if (s1 == null) { result = s2; }
    return result;
  }
  // ----------------------------------------------
  public boolean ifnull(boolean s1,boolean s2) {
    boolean result = s1;
    if (s1 == null) { result = s2; }
    return result;
  }
  // ----------------------------------------------
}

@isTest
public class Test_apexCodingUtilities() {
  
 public static apexCodingUtilities acu = new  apexCodingUtilities();
 
 // ----------------------------------------------
  static testMethod void test_isif () {
    // define variable methods for null tests
    string s = null;
    boolean b = null;
    integer i = null;

    // replaces arg1 with arg2, if arg1 is null
    // testing strings..
    system.assertEquals(acu.ifnull(s,s)      ,null);
    system.assertEquals(acu.ifnull(s,'1')    ,'1');
    system.assertEquals(acu.ifnull('1','2')  ,'1');
    system.assertEquals(acu.ifnull(s,'2')    ,'2');
    // testing booleans..
    system.assertEquals(acu.ifnull(true,false)  ,true);
    system.assertEquals(acu.ifnull(b,false)   ,false);
    // testing integers..
    system.assertEquals(acu.ifnull(0,1)      ,0);
    system.assertEquals(acu.ifnull(i,1)      ,1);

  }
}



KevinBrKevinBr
Another method, perhaps simpler, regarding your specific scenario with contact fields:  firstname + lastname:

// 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;
}

PtochosPtochos
Avoid that pesky "Attempt to de-reference a null object" by using ternary operations.
Convert a  suspected null to an empty, or keep the original string:

String EmptyOrOriginal = SuspectNull == null ? '' : SuspectNull;
// Use ternary operations to convert possibly null to empty strings.

String FirstName = Trigger.old[i].FirstName;
String LastName = Trigger.old[i].LaststName;

FirstName = FirstName == null ? '' : FirstName;
LastName = LastName == null ? '' : LastName;
 
contactNameOld = FirstName + ' ' + LastName;