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
Sharon FreasSharon Freas 

Checking for null

Probably a stupid question but here goes. I'm a long-time developer but pretty new to SalesForce Apex.

I have a custom object table containing a bunch of user Ids that is partially filled in. I need to assign fields from table to fields on account records.

Right now I am checking before each field assignment if that particular field is null before doing the assignment like:
if (ct.Id1 != null) {
   a.Id1 = ct.Id1;
}

Is there a better way to do this? There's a bunch of fields and it's a pain to write this over and over.

Thanks! 
MoonpieMoonpie
Hi Sharon,

If I am understanding your question, you are wanting to use a variable for your field name.
Or am I misunderstanding?

Please elaborate a bit more on what you are doing - with as much info as you can give (that is pertinent to this context) on the custom object and fields.
Sharon FreasSharon Freas
The custom object is a table of potential account owners for different situations. 
ct
Name__c
Id1__c
Id2__c
Id3__c

Records:
Name__c: Row1  Id1__c: userId1   Id2__c: null         Id3__c: userId2
Name__c Row2   Id1__c: null          Id2__c: userId3  Id3__c: null
Name__c Row3   Id1__c: userId4    Id2__c: userId5  Id3__c: userId6

Here's what I'm writing to avoid getting a null exception
if (ct.Id__c1 != null) {
   a.Id1__c = ct.Id1__c;
}

if (ct.Id__c2 != null) {
   a.Id2__c = ct.Id2__c;
}

if (ct.Id__c3 != null) {
   a.Id3__c = ct.Id3__c;
}

Am I overdoing it and assigning null to a lookup field in accounts won't throw an exception? If I do need to be checking for this, is there a better / faster way to code this?

Thanks for your help! I really appreciate it.
Sharon FreasSharon Freas
I should add that by the time I've gotten to the assignment, I've already found the right row in the table
MoonpieMoonpie
Ok, thanks for the extra info.

Full disclosure: I'm fairly new to this, too, but have spent the last couple of weeks working on Apex code: a particular trigger (and how to bypass it in certain situations) and when creating the test methods for the trigger I decided to try to create a test data factory for Accounts.  I tell you all that because part of what I've been trying to discover is how to iterate through object fields and I literally just this afternoon posted and received a helpful response to a question on Salesforce Stackexchange about how to set a field value using a variable for the field name.

That said, I honestly have no clue whether it is safe to assign null to a lookup field.  My gut tells me that if it is not a required field, it would be OK.  But I really have no idea.  But in my book, it is (almost) never wrong to do a null check!

Other, more experienced & better folks may come along and give additional answers to this.  If so, you might want to listen to them.  But since no one has yet to reply, let me tell you how I think you could do it - although it might not be the best or most elegant way....


If you want to iterate through only some of the fields on the custom object, and you know those fields won't dynamically change, you could create a list of the field names:
List<String> fieldList = new List<String> {'Id1__c', 'Id2__c', 'Id3__c'};
(I hate hardcoding, but it beats all the copying/pasting and/or typing your doing!)

Then you could try iterating through the list with a for loop:
for (String fieldName : fieldList) {
    // do your stuff here
}
I'll elaborate on the "do your stuff here" part, but first...

You can retrieve a field value using a variable for a field name like this:
ct.get(fieldName);

And what I learned today from the answer to my question, is that you can set a field value using a variable for a field name like this:
a.put(fieldName, 'value');
(NOTE: put is a method of SObject class (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_sobject.htm#apex_System_SObject_put), and it returns the previous value of the field.  That shouldn't really matter in your case, and it didn't affect what I was doing with it; but I wanted to point that out just in case while trying to get it to work you saw something wonky.)

That means your "do stuff here" part might look like this:
if (ct.get(fieldName) != null) {
        a.put(fieldName, ct.get(fieldName));
}
-----
Putting it all together, you could try something like:
List<String> fieldList = new List<String> {'Id1__c', 'Id2__c', 'Id3__c'};
for (String fieldName : fieldList) {
    if (ct.get(fieldName) != null) {
        a.put(fieldName, ct.get(fieldName));
    }
}

DISCLAIMER: I just wrote all of this in the reply - I did not test any of this myself.
Sharon FreasSharon Freas
@Moonpie Thanks for the idea! I will give this a try when I get a chance
MoonpieMoonpie
Hi Sharon,

Did you ever get a chance to try it out?

I wanted to add: if you had a longer list of fields, but they were all named the same thing in sequence as you have shown, you could also do something like this (for instance, if you had 10 such fields):
List<String> fieldList = new List<String>();
for (Integer i = 1; i <= 10; i++) {
    fieldList.add('Id' + i + '__c');
}
for (String fieldName : fieldList) {
    ...< as above >...
}
There are other ways to iterate through a number of fields using Dynamic Apex, but they are way more complex.