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
ministe_2003ministe_2003 

Field not appearing in query results

Hi all, got a weird issue.  I'm trying to test run some batch apex in my sandbox using the developer console to schedule immediate runs and I'm hitting a weird issue.

 

I'm trying to query a custom currency field caled Total_Won_This_Year__c but it's not being returned in my results.  I'm on the standard System Administrator profile so I should have no access problems with regard to field level security (which is set to editable anyway), the query runs and doesn't error out but when I try to access the field I get null exceptions and when I look in the debug logs I see the field isn't in the results.  I'm on the Enterprise Edition.

 

atrb.query = 'SELECT Id, CurrencyIsoCode, Total_Won_This_Year__c FROM Account';
...
return Database.getQueryLocator(query);
...
DEBUG|query = (Account:{CurrencyIsoCode=EUR, Id=001e0000004pCLSAA2}

Anyone know why I'd be having this issue?  I need to query the field and update a value and I'm using a batch so I can run it monthy.

 

Thanks

ibtesamibtesam

if a field is null, it wont appear up in the results!

ministe_2003ministe_2003

Thanks for the quick reply but I have at least one record with a value in this field (tried to add an image but it wont let me)

Jeff MayJeff May

If you're getting the 'attempt to reference a null object' error, its because a field on an object with no value is actually null.  Therefore, you can't call methods on it.

 

For example:

 

A String field called MyString__c

 

If I retrieve it via SOQL, and then do the following, I'll get an error on any record where the field has not been populated:

 

if (obj.MyString__c.contains('A') {

 

}

 

note that the contains() requires that the string be non-null (it can be empty, but it has to be a String object

ministe_2003ministe_2003

Oh I think it's just clicked into place.  What you're both saying is, if the value for a single record is null, it won't even display in query results.  So if I had two records, A and B, and they looked like this:

 

Name RecordA      Total Won This Year     Currency GBP

 

Name RecordB     Total Won This Year  £1000    Currency GBP

 

Then the debug log would show

 

(Account:{Name=RecordA, CurrencyIsoCode=GBP; Name=RecordB, CurrencyIsoCode=GBP, Total_Won_This_Year__c=1000;}

ie in the record where the value is null, it won't come through the query as null, it will just be completely left out?

 

I can't believe I didn't know that...

Jeff MayJeff May

The field is not left out, but there is nothing to show.  If you add a System.debug() statement and include the field, you'll see null where the field value would be.

 

If you are doing read-only queries on a VF page, you can detect the null and display a value of your choice:

 

List<things> l = [select id, fielda from things];

 

for (thing t : l){

  if (t.fielda == null) {

      t.fielda = 'Special';

  }

}

 

Then if the VF page is displaying fielda, you'll see 'Special'.  And, since you're not doing a record update, the value is not saved.

 

ministe_2003ministe_2003

Well as you can see from my original post, the null field is not simply displaying null in the debug log, it is being left out entirely which is what caused my confusion. I've worked around this issue by changing my code to assume the field is null so it doesn't hit the error again. Thanks

Steven

Anil KamisettyAnil Kamisetty
I got into the same problem. Looks like the scenaio is "It is an existing table, created new columns to the table. The value will be null by default for the existing data records. If we enter the value for the existing data records, some reason the value for the new fields is still NULL. Hence it doesnot show in the database / soql querries."

Solution is to, delete the existing data records and recreate the record and enter the value for all fields. It does like a charm. 

I really don't understand, why is this behavior. Very Strange though.

*** If it is answer the question, please choose it for everyones reference.
Vaidehi Dabir 7Vaidehi Dabir 7
@Anil Kamisetty
Deleting all existing records and creating them again worked for me. I wasted 2 hours checking object-level and record-level sharing settings of the object as well as the apex class. Then I found that the issue was with the existing records.