You need to sign in to do that
Don't have an account?
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
if a field is null, it wont appear up in the results!
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)
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
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
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...
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.
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
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.
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.