I use a managed package that gives me the ability to invoke Apex class, get result and handle it. My Static Resource (JS code) invokes Apex to fetch one or more records from the DB and return them back. I want to get all the fields of this record including null/empty values. So, because Database.Query(query)) does not include empty/null values, i modify a bit the sObject record to include them. However, when my Apex class returns the results to JS, my map does not include the null values!!! Is this logical? How can i overcome this?
/**
/**
* ? 16/07/2021 @ggalaios
* ! Generic Apex class called from OE Static Resources (Implements the CS Interface)
* ! Consists of "Execute" method to return OE Data back to the static Resource that invoked the Class
*/
global class el_OE_DataHandler implements csoe.IRemoteAction {
global Map<String,Object> execute(Map<String,Object> inputMap) {
try {
system.debug('called.' + inputMap);
//Define the necessary variables that are expected to exist in the map
String sObjectAPIName = (string) inputMap.get('sObjectAPIName'); //Mandatory
system.debug('sObjectAPIName is: ' +sObjectAPIName);
String recordId = (string) inputMap.get('recordId'); //Mandatory
String fieldAPIName = (string) inputMap.get('fieldAPIName'); //Mandatory
String orderByFieldAPIName = (string) inputMap.get('orderByFieldAPIName'); //Optional
String queryString; //Query String to be used for SOQL Operation
//get a Comma Separated List of fields
String sObjectFields = el_GenericQuery.getSobjectFields(sObjectAPIName);
inputMap.clear(); //clear the input map
//Handle QueryString depending the 'Order By' optional field
if(String.isBlank(orderByFieldAPIName)) {
//get all the fields of the given sObjectAPIName
queryString = 'SELECT ' +el_GenericQuery.getSobjectFields(sObjectAPIName) +
' from ' +sObjectAPIName+ ' where ' +fieldAPIName+ ' = \'' + recordId + '\'';
} else {
//get all the fields of the given sObjectAPIName
queryString = 'SELECT ' +el_GenericQuery.getSobjectFields(sObjectAPIName) +
' from ' +sObjectAPIName+ ' where ' +fieldAPIName+ ' = \'' + recordId + '\''
+ 'order by ' +fieldAPIName;
}
//Execute the query
List<sObject> sObjectList = new List<sObject>();
Schema.SObjectType targetType = Schema.getGlobalDescribe().get(sObjectAPIName);
Map<String, Schema.SObjectField> fieldMap = targetType.getDescribe().fields.getMap();
//Iterate through each record returned
for(sObject tmpSobject : Database.query(queryString)) {
//for each field
for(String field: fieldMap.keySet()) {
//check if field's value is null. If yes, add it to the sObject record in order to be included
if(tmpSobject.get(field) == null) {
tmpSobject.put(field, null);
}
}
sObjectList.add(tmpSobject);
}
inputMap.put(recordId, sObjectList);
system.debug('just before return...' +inputMap); //null values included in the map!
return inputMap;
} catch (Exception e) {
system.debug('Exception occured: ' +e.getMessage() +'line is: ' +e.getLineNumber());
return inputMap;
}
}
}
Here is my static resource code that receives data from Apex. On console.log, null values are not included!!!
//function called when Apex returns Rate Card data
function updateRateCard(data) {
console.log('upddata is: ' + JSON.stringify(data));
}
UPDATE 19/07/2021 Please find my updated code where i serialize the map in order to include null values. I replaced
inputMap.put(recordId, sObjectList);
with
inputMap.put(recordId, JSON.serialize(sObjectList));
inputMap
and also share console log of javascript end?