1

I'm new to Apex as a Salesforce admin and I'm trying to figure out a simple Apex trigger exercise. The goal of this trigger is to ensure that when a Lead's first name or last name = 'test', the lead's status should be set to 'Disqualified'.

This is my code:

trigger LeadDisqualification on Lead (before insert, before update) {

    // If first name or last name equals 'test', Lead Status should equal Disqualified.
    for (Lead myLeads : Trigger.new) {
        if (((myLeads.FirstName != null) || (myLeads.LastName != null)) && 
           (myLeads.FirstName.equalsIgnoreCase('test') ||
            myLeads.LastName.equalsIgnoreCase('test'))) {
                myLeads.Status = 'Disqualified';
        }
    }
}

As it is, when I tried to create a lead with the first name blank / null, I'm getting an error message:

LeadDisqualification: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.LeadDisqualification: line 6, column 1

However, I was able to create the lead with the first name null with no issues when I updated line 5 by removing:

(myLeads.LastName != null)

I basically changed line 5 from:

        if (((myLeads.FirstName != null) || (myLeads.LastName != null)) && 
           (myLeads.FirstName.equalsIgnoreCase('test') ||
            myLeads.LastName.equalsIgnoreCase('test'))) {

to

    if (((myLeads.FirstName != null)) && 
       (myLeads.FirstName.equalsIgnoreCase('test') ||
        myLeads.LastName.equalsIgnoreCase('test'))) {

Does anyone know why removing myLeads.LastName != null would cause the null pointer exception error message to go away? Just trying to understand this!

0

Note that there is a null safe way to compare values in a case insensitive manner:

for (Lead record : records)
{
    if (record.FirstName == 'test' || record.LastName == 'TEST')
    {
        // the == operator performs case-insensitive comparison
        // it is also null safe
        // the left or right operand can be null
    }
}

Regardless, from a logical perspective it is useful to know how to properly perform two separate null checks here.

You should use an and join for each field. Let's consider separate loops. You would do:

for (Lead record : records)
{
    if (record.FirstName != null && record.FirstName.equalsIgnoreCase('some value'))
    {
        // do stuff
    }
}
for (Lead record : records)
{
    if (record.LastName != null && record.LastName.equalsIgnoreCase('some value'))
    {
        // do stuff
    }
}

Now if you were to combine them, you would do:

if ((record.FirstName != null && record.FirstName.equalsIgnoreCase('...')) || record.LastName != null && record.LastName.equalsIgnoreCase('...'))
{
    // do stuff
}
1
  • 1
    Thanks so much for taking the time to respond! I didn't realize == would be null safe. And thanks for explaining the logic on what went wrong with my code too! – Infection Sep 17 '17 at 3:25
1

There's no need to check for null, because you don't need to use equalsIgnoreCase. == has the same behavior, but is null-safe:

if(myLeads.FirstName == 'test' || myLeads.LastName == 'test') {

In the general case, if you need to use equalsIgnoreCase, you can reverse the test:

if('test'.equalsIgnoreCase(myLeads.FirstName) || 'test'.equalsIgnoreCase(myLeads.LastName)) {

However, I would recommend never, ever using equalsIgnoreCase, because it is never as efficient as simply using ==.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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