You need to sign in to do that
Don't have an account?

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
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
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
It's sounding like the issue is that I need to be more careful with how I retrieve values? Here's the method:
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.....
}
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];
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
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. :)