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
bvoybvoy 

Testing for a field's existence

Hi folks,

 

New to Apex development. I've written an Apex method to check data (zipcode) on new cases. If they're in the U.S. and there's a match with a group of accounts, it fills in a lookup field. It's called by a simple trigger that passes on all cases before insert & update.

 

I've learned in doing this that Apex does not even create an empty variable for an empty field, whether it's passed in from Trigger.new (the cases) or a SOQL query (the accounts). Most of the fields this method uses aren't and can't be required. So both in looping through accounts that may not have zipcodes filled in, and in dealing with cases that don't have the information I'm testing for filled in, this method has a big problem with running into null pointer exceptions due to empty fields.

 

For now, I'm simply catching any null pointer errors to make sure this doesn't stop any records from being saved and edited, but this seems like a terrible way of doing things. Does Apex have a way of testing if a field/variable exists? Or is there some other way of dealing with this I'm missing? I suppose I could simply set up try/catch for each individual variable as a way of setting "this variable exists" booleans, but again, I'm used to the idea that there should be a better way.

 

Thanks,

 

-Ben

Best Answer chosen by Admin (Salesforce Developers) 
netspidernetspider

2 potential breaks are

p.ir_zip__c.length() == 5 && p.ir_zip__c.isNumeric()

 

so you would want to if (p.ir_zip__c != null)

 

and also

if(an_aff.affiliate_zips__c

should change to 

 

if(an_aff.affiliate_zips__c != null){

 

if(an_aff.affiliate_zips__c.Contains.....

 

}

 

All Answers

netspidernetspider

It depends what you are doing, share your code which may help

 

but if you are doing something like map1.get(val).XXX__c, you should test if the map return a null before running this line, like

if (map1.get(val) != null) myvar = map1.get(val).XXX__c;

else ......

 

Or if you do a soql    Y =  [Select XXX__c From Case];

myvar = integer.valueof(Y.XXX__c);

Instead 

myvar = (Y.XXX__c != null) ? integer.valueof(Y.XXX__c) : 0;

 

These are just examples of course, share your code which may help though

bvoybvoy

It's sounding like the issue is that I need to be more careful with how I retrieve values? Here's the method:

 

    public static void search(Case[] packs) {
        Account[] affs = [SELECT Id, affiliate_zips__c FROM Account WHERE RecordTypeID = '01270000000DzZFB0'];
        
        for(Case p :packs){
            try{
        	    Boolean found_aff = false;
                if(p.ir_zip__c.length() == 5 && p.ir_zip__c.isNumeric() && p.IR_Country__c == 'United States of America'){
                    for(Account an_aff: affs){
                        if(an_aff.affiliate_zips__c.contains(p.ir_zip__c)){
                            p.local_aff__c = an_aff.Id;
                            found_aff = true;
                            break;
                        }
                    }
                }
                if(!found_aff){
                    p.local_aff__c = null;
                }
        	}
            catch (System.NullPointerException e) {
                //to be expected if an entry doesn't have zips
            }
        }
    }

 

netspidernetspider

2 potential breaks are

p.ir_zip__c.length() == 5 && p.ir_zip__c.isNumeric()

 

so you would want to if (p.ir_zip__c != null)

 

and also

if(an_aff.affiliate_zips__c

should change to 

 

if(an_aff.affiliate_zips__c != null){

 

if(an_aff.affiliate_zips__c.Contains.....

 

}

 

This was selected as the best answer
netspidernetspider

Also you shouldnt ever hard code ids, where you have 

 

 

Account[] affs = [SELECT Id, affiliate_zips__c FROM Account WHERE RecordTypeID = '01270000000DzZFB0'];

 

change to 

 

Account[] affs = [SELECT Id, affiliate_zips__c FROM Account WHERE RecordTypeID =:[select Id,Name,SObjectType from RecordType where Name = '' and SObjectType ='Account'].id];

netspidernetspider

Sorry wanted to reply with separate login

 

It depends what you are doing, share your code which may help

 

but if you are doing something like map1.get(val).XXX__c, you should test if the map return a null before running this line, like

if (map1.get(val) != null) myvar = map1.get(val).XXX__c;

else ......

 

Or if you do a soql    Y =  [Select XXX__c From Case];

myvar = integer.valueof(Y.XXX__c);

Instead 

myvar = (Y.XXX__c != null) ? integer.valueof(Y.XXX__c) : 0;

 

These are just examples of course, share your code which may help though

bvoybvoy

netspider wrote:

so you would want to if (p.ir_zip__c != null)


Ah, thank you! Exactly what I needed.

 

After looking in the debug log I'd noticed these fields weren't even showing up there when they were empty, so I'd assumed any operation on the variable would raise a null pointer. Dangers of assuming. :)