3

I have written a REST API endpoint for Account object for GET operation. The code is below -

@httpget
global static list<Account> doget(){

RestRequest req = RestContext.request;
RestResponse res = RestContext.response;

list<Account> accountList = new list<Account>();
String searchtext = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);

IF (searchtext !=null && searchtext !=''){
accountList = [select ID,
                Email__c,
                Name,
                Phone,
                Send_Owner_Reminders__c,
                ZTS_EU_Opt_Out_Email__c,
                ZTS_EU_Opt_Out_Date_Email__c,
                ZTS_EU_City__c,
                ZTS_EU_Zip_Postal_Code__c,
                ZTS_EU_District__c,
                ZTS_EU_Sub_Type__c,
                RecordType.Name,
                Type,
                ZTS_EU_Country__c,
                ZTS_EU_Market__c,
                Email_Preference__c,
                ZTS_EU_Address_Line_1__c,
                ZTS_EU_Address_Line_2__c,
                ZTS_EU_Account_Status__c,
                ZTS_EU_Rep_Account_Status__c,
                ZTS_EU_TSF_PBC__c,
                OwnerID,
                ZTS_EU_Account_Count__c,
                ZTS_EU_Preferred_Language__c,
                ZTS_EU_Potential_Region__c,
                ZTS_EU_Customer_ID__c,
                ZTS_EU_Zoetis_SFDC_ID__c from Account where ZTS_EU_Customer_ID__c =:searchtext or Email__c =:searchtext];


return accountList;
}

else{

system.debug(searchtext);
accountList =  [select ID,
                Email__c,
                Name,
                Phone,
                Send_Owner_Reminders__c,
                ZTS_EU_Opt_Out_Email__c,
                ZTS_EU_Opt_Out_Date_Email__c,
                ZTS_EU_City__c,
                ZTS_EU_Zip_Postal_Code__c,
                ZTS_EU_District__c,
                ZTS_EU_Sub_Type__c,
                RecordType.Name,
                Type,
                ZTS_EU_Country__c,
                ZTS_EU_Market__c,
                Email_Preference__c,
                ZTS_EU_Address_Line_1__c,
                ZTS_EU_Address_Line_2__c,
                ZTS_EU_Account_Status__c,
                ZTS_EU_Rep_Account_Status__c,
                ZTS_EU_TSF_PBC__c,
                OwnerID,
                ZTS_EU_Account_Count__c,
                ZTS_EU_Preferred_Language__c,
                ZTS_EU_Potential_Region__c,
                ZTS_EU_Customer_ID__c,
                ZTS_EU_Zoetis_SFDC_ID__c from Account limit 40];

            return accountList;
    }
}

Now when I am testing with Workbench REST Explorer I am seeing some fields are not coming in response, because those fields are having null values in that specific Record. I want to show all fields irrespective of its values.

Please let me know what I need to change in code.

1 Answer 1

2

SOQL does not returns field with null values when serialized directly.

Easiest work around is to copy the field values to a new sObject. Example:

Account account = [SELECT Id, Name, BillingCity FROM Account WHERE Name = 'Test Account'];
Account newAccount = new Account(
    Id = account.Id,
    Name = account.Name,
    BillingCity = account.BillingCity
);
// newAccount would return fields with null values

This could be scaled with use of a wrapper class:

public with sharing class MainClass {

    public static String getAccounts() {
        List<AccountWrapper> accounts = new List<AccountWrapper>();
        for(Account account: [SELECT Id, Name, BillingCity FROM Account 
            WHERE Name = 'Test Account']) {
            accounts.add(new AccountWrapper(account));
        }
        return JSON.serialize(accounts);
    }

    public class AccountWrapper {
        public Account account;
        public AccountWrapper(Account account) {
            // copy fields in new object instance
            this.account = new Account(
                Id = account.Id,
                Name = account.Name,
                BillingCity = account.BillingCity
            );
        }
    }
}

Calling get Account System.debug(MainClass.getAccounts()); outputs:

[{"account":{"attributes":{"type":"Account","url":"/services/data/v46.0/sobjects/Account/0012p00002NlX48AAF"},"Id":"0012p00002NlX48AAF","Name":"Test Account","BillingCity":null}}]


Excellent improvement by Bartosz Śliwiński:

You could also think about using dynamic SOQL with list of fields and then use same list to populate values on this new instance of an Account. If done well it will be easier to maintain one list than SOQL + copying fields one by one.

1
  • 2
    You could also think about using dynamic SOQL with list of fields and then use same list to populate values on this new instance of an Account. If done well it will be easier to maintain one list than SOQL + copying fields one by one. Jun 24, 2019 at 12:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .