1

We have following values:


|Id  | Name  |Language|
|1   | abc   |        |

When I query the above record and debug this record in apex class it shows:-

(Library__c:{Id=xxxxxxxxxxxx, Name=abc})

It is not showing language field Why?

Actually, I need to create JSON from the query result. As the query result not showing the null or empty values, therefore, Json is also not including the null and empty values.

  • 1
    Post your SOQL query that you are using. – TSmith Apr 19 '17 at 8:18
  • Query is Select id,name from Library__c where id='xxxxxxx' – Kiran Preet Apr 19 '17 at 9:15
  • 3
    In above query you have not mentioned Language field. – Ashwani Apr 19 '17 at 9:16
  • 1
    Query is Select id,name,Language__c from Library__c where id='xxxxxxx' – Kiran Preet Apr 19 '17 at 9:20
1

you can do the following:

list<Map<String, Object>> aa = new list<Map<String, Object>> ();
SObject[] b = [Select id, name, Language__c from Library__c];

for (SObject b : sobjects) {
    Map<String, Object> a = new Map<String, Object> ();
    for (SObjectField f : account.sobjecttype.getdescribe().fields.getmap().values()) {
        try { a.put(String.valueOf(f), b.get(f)); }
        catch(Exception e) { a.put(String.valueOf(f), null); }
    }
    aa.add(a);
}
System.debug(json.serialize(aa));

If you don't want all the non-query'd fields to be null values in your json you can adjust the list of Sobjectfield returned by the describe.

If it's just the language field you wan't to check you can do the following:

list<Map<String, Object>> aa = new list<Map<String, Object>> ();
SObject[] b = [Select id, name, Language__c from Library__c];

for (SObject b : sobjects) {
    Map<String, Object> a = new Map<String, Object> ();
    try { a.put('Language__c', b.get('Language__c')); }
    catch(Exception e) { a.put('Language__c', null); }
    aa.add(a);
}
System.debug(json.serialize(aa));
0

As far as I know,System.debug does not print fields with null value but I will be glad to be wrong about this.I am not able to find any documentation to support this but I tried this in my org and the debug doesn't print null field.

  • I think it is not because of System.debug. I think that SOQL not returns nothing when every object found has null in queried field. And this causes described Issue. – patryk Apr 19 '17 at 10:17
  • 1
    SOQL returns the field even when they are null.It's just that they are not shown in debug @pepper42 – Samir Apr 19 '17 at 10:33
  • Any proofs for this? – patryk Apr 19 '17 at 10:47
  • if the field is not mentioned in SOQL and you use it in apex, you get an error – Samir Apr 19 '17 at 11:30
  • Yes SOQL return the field but not shown in System.debug. Also when I try to make Jason from the SOQL query result. The fields with NULL values are not including in Jason. – Kiran Preet Apr 20 '17 at 11:24
0

Prepare a wrapper for Your Json object and then assign the values from objects from query to wrappers, then You will always have values or null.

I think that SOQL not returns nothing when every object found has null in queried field. And this causes described Issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.