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
John L Schneider IIJohn L Schneider II 

Apex Trigger works for records created in GUI but not through API

trigger Contact on Contact (before insert, before update) {
    for (Contact c1 : trigger.new)
    {
        string fullname = c1.LastName;
        string firstname = c1.FirstName;
        if( fullname.containsWhitespace() == true )
        {
            if (String.isBlank(firstname) == true)
            {
                string fn;
                string ln;
                fn = fullname.substring(0,fullname.indexOf(' '));
                ln = fullname.substring(fullname.indexOf(' ')+1);
                c1.LastName = ln;
                c1.FirstName = fn;
            }
        }
    }
}

As the topic says, this works when I create a record through the GUI but not when loading records through Data Loader (CLI Batch or Bulk).

I am attempting to split the value in LastName whenever the FirstName is blank and the LastName has a space in it.
Best Answer chosen by John L Schneider II
Salesforce DeveloperSalesforce Developer
Please check your data that you are uploading. If there is any one row where lastName is blank you will get the error for the whole batch. This is more seems like a Data issue. Also you can refine your code to check Not Null before processing further.

All Answers

Nithesh NNithesh N
Hi John, Try chnaging the batch size to 100 and check once. 
It is working for small batches.

Let me know the result. 

Best,
Nithesh
John L Schneider IIJohn L Schneider II
Still receiving this error. 
Contact: execution of BeforeInsert

caused by: System.NullPointerException: Attempt to de-reference a null object

Trigger.Contact: line 6, column 1
Nithesh NNithesh N
I think fullname is a null.
So, When doing fullname.containsWhitespace()​ , it is throwing null pointer exception.
Lets add
if(fullname != null)
{

}

But its Strange, Contact last name wont be null, Since its a required field and System Validation Rule will catch it. Do you hae any idea which particular records were causing this problem. Try adding System.Debug(c1) in the third line. We can make changes accordingly.


Best,
Nithesh
Salesforce DeveloperSalesforce Developer
Please check your data that you are uploading. If there is any one row where lastName is blank you will get the error for the whole batch. This is more seems like a Data issue. Also you can refine your code to check Not Null before processing further.
This was selected as the best answer
John L Schneider IIJohn L Schneider II
A different issue I ran into is the trigger is firing even when there is data in the first name field.  Any thoughts?
Salesforce DeveloperSalesforce Developer
I would put some debug statements inside the inner block to find the row on which it is going in. Also, check if you have any other trigger on the same events ( before insert, before update) on Contact and they have any code which is modifying/deleting the FirstName. 
Also, you don't need to add == TRUE in the IF condition block, as these functions return boolean type only. So:

line 6 could be : if( fullname.containsWhitespace() )

and line 8 : if (String.isBlank(firstname) )