0

Hi i'm executing this query using

Database.Query(query)

Where query is

SELECT field1__c, field2__c, field3__c FROM Object1__c

On the current record, field3__c is NULL but when i query on the record instead of getting

"field1__c = value1", "field2__c = value2", "field3__c = null"

I get

"field1__c = value1", "field2__c = value2"

Just like the field has been deleted/not returned, which is a problem for our purpose.

The question is:

How can i get the null values returned as NULL or BLANK and not just "not returned"?

  • what is your purpose with that - there is actually no problem either in apex or in UI javascript? – salesforce-sas Sep 4 '19 at 14:38
  • that class is returning values to a flow which uses that field. If the field was returned as null or blank there are no issues, but if the field is not returned the flow will raise error cause "unable to access to the field" – gmemo Sep 4 '19 at 14:39
3

If you are checking in the debug logs, then that is how it will be displayed. It behaves the same for the normal SOQL query as well as for the dynamic SOQL i.e. with Database.Query

So, if you perform a SOQL on Object1__c with field3__c as null, then the debug will always show you

USER_DEBUG [2]|DEBUG|Object1__c:{Id=0010o00002GxDELAA3, field1__c=CD355119-A, field2__c=1231313}

irrespective of whether you used normal SOQL or Database.Query. If you want to add a null check for field3__c, then you can do

List<Object1__c> objList = Database.Query('SELECT field1__c, field2__c, field3__c FROM Object1__c');
for(Object1__c obj : objList){
    //check for null for field3__c
    if(obj.field3__c != null){
    }
}

If you are passing this field3__c which to a flow i.e. a formula field, then you should have an additional check for related lookup field i.e. if your formula field's formula is Account.AccountNumber then have a check for AccountId != NULL && field3__c != NULL in flow. If you try to directly access field3__c or check for NULL, it will give you error. So always have an additional check for its lookup field as first check and then check for its related field.

  • I'm doing this but field3__c is a Formula Field so it's unwritable. I need this field to be blank as needed but i need my query to return it as empty string or null. I really cant realize why it's just not returned, how this logic of "not returning" could help any user? – gmemo Sep 4 '19 at 14:53
  • @gmemo I'm doing this but field3__c is a Formula Field so it's unwritable What's that got to do with anything? The suggestion is to use it to check (if-statement), not write to. I need this field to be blank as needed but i need my query to return it as empty string or null. It is returning as blank/null. Do a System.debug() on the field itself and it will spit out its value of blank/null. – Drew Kennedy Sep 4 '19 at 15:02
  • if i do a debug on field3 it corretly display as null. but if i pass the record to another method that access to that field from the record it says "unable to access to the field" If i change the value of field3 to "test" or anything else different from null, it works. – gmemo Sep 4 '19 at 15:10
  • If you are passing this field3__c which is a formula field to a flow, then have an additional check for related lookup field i.e. if your formula field is Account.AccountNumber then have a check for AccountId != NULL && field3__c != NULL in flow. If you directly try to access field3__c or check for NULL, it will give you error. So always have an additional check for its lookup field as first check and then check for its related field. – Vijay Ganji Sep 4 '19 at 15:57

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.