1

I am trying to push a JSON input payload, but some fields might be blank. The receiving system has a problem with it. They want either the blank fields to not be sent all together or replace "Null" from JSON payload to "". I was planning to do the latter... I tried to use if-else condition to check if the field is blank and replace "Null" to "", but keep getting 500 Internal Error.

Incorrect Format ..

"displayName" : "Null",

Correct Format ..

"displayName" : "",

Does any one know how can this be done. The below code didn't work as callout failed with Internal Error , Status Code 500.

@InvocableMethod
public static void postAccountToSystem(List<Id> acctIds){
Account acc = [SELECT Name,Display_Name__c,
                   Bio_URL__c,Image_URL__c
                    FROM Account WHERE Id = :acctIds[0]];  

    if(acc.Display_Name__c == NULL){
        acc.Display_Name__c = '';
    }

    if(acc.Bio_URL__c == NULL){
        acc.Bio_URL__c = '';
    }
    if(acc.Image_URL__c == NULL){
        acc.Image_URL__c = '';
    }

   String jsonInput = '{\n' +
        ' "displayName" : "'+acc.Display_Name__c+'",\n'+
        ' "bioUrl" : "'+acc.Bio_URL__c+'",\n'+
        ' "imageUrl" : "'+acc.Image_URL__c+'",\n'+
       System.enqueueJob(new QueueableCall(jsonInput, acc.Id));   
}
1
  • In JSON, null should be null, without quotes. And your system should be able to handle those values correctly if they're using a real JSON parsing library.
    – sfdcfox
    Commented Jun 15, 2017 at 19:23

2 Answers 2

3

You should just serialize a Map<String, String>:

Map<String> data = new Map<String, String>
{
    'displayName' => acc.Display_Name__c,
    'bioUrl' => acc.Bio_URL__c,
    'imageUrl' => acc.Image_URL__c
};
String payload = JSON.serializePretty(data);
1
  • Hey Adrian.. I tried your approach , but two things are happening .. "Null" is becoming null and not ' '. Secondly , serializePretty is not removing null fields Map<String, String> data = new Map<String, String>{ 'email' => acc.Email__c, 'displayName' => acc.Display_Name__c, 'bioUrl' => acc.Bio_URL__c, 'imageUrl' => acc.Image_URL__c, }; String jsonInput = JSON.serializePretty(data, True); System.enqueueJob(new QueueableSeleneCall(jsonInput, acc.Id));
    – login2sf
    Commented Aug 3, 2017 at 17:00
0

You could also put default value to empty string on those kinds of nullable text fields.

And use JSON.serialise() to turn account as a json string object

You must log in to answer this question.

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