28-6-16 12:00

Every release Salesforce brings us many enhancements. Summer ’16 release is not an exception. Let’s take a on very handy enhancement for JSON serialisation. In our showcase we will use new method for suppressing null values when serialising apex object.

Many times different system’s web services expect null values in different ways. Sometimes they don’t accept null values at all. So it is better just skip null values in JSON that is being sent. Lets say that we have to send new accounts from Salesforce to some external system daily. As well we can send some additional info with list of accounts. For that reason we will create a container class that will hold accounts and additional information.

Please, find apex class below.

public class AccountContainer {

public List<Account> accounts {get; set;}

public String someAdditionalInfo {get; set;}

}

Our class that contains method for serialising this object into JSON will look like this:

public class Summer16 {

public static String generateJson(Boolean hasNull) {

List<Account> accounts = [SELECT Id, Name, BillingCity, BillingCountry, BillingStreet, BillingPostalCode

FROM Account where CreatedDate >= TODAY];

AccountContainer container = new AccountContainer();

if (!accounts.isEmpty()) {

container.accounts = accounts;

}

String jsonText = JSON.serialize(container, hasNull);

System.debug(‘jsonText =’ + jsonText);

return jsonText;

}

}

The method that is called generateJSON as argument takes boolean which checks if we have to suppress null values or not. We will fill account, but some additional info variable will remain null. Next what we have to do is to test.

For generation of test data most time I use very handy method. That takes sObject type,number of records that should be created and map of field values by field name. In return you get list of sObjects that you can cast if needed and insert all records. It is great to use it when ever you need to test your code to be bulk proof.

public class TestDataGeneraion {

public static List<sObject> generatesObjects(String sObjType, Integer recNum, Map<String, Object> fieldsValueByName) {

List<sObject> sObjList = new List<sObject>();

Map<String,Schema.SObjectType> globalDescribeData = Schema.getGlobalDescribe();

Schema.sObjectType sObjectType = globalDescribeData.get(sObjType);

Map<String, SObjectField> fieldsMap = sObjectType.getDescribe().fields.getMap();

for (Integer i = 1; i <= recNum; i++){

//Create new record of a specified objType

sObject sObj = sObjectType.newSObject();

//Populate required fields

for (String field : fieldsValueByName.keySet()){

sObj.put(field, fieldsValueByName.get(field));

}

sObjList.add(sObj);

}

return sObjList;

}

}

Our test class can be found here:

@isTest

public class TestSummner16 {

static testMethod void validateGenerateJsonNotSuppressApexObjectNulls() {

List<Account> accounts = new List<Account>();

Map<String, Object> fieldsValueByName = new Map <String, Object>();

fieldsValueByName.put(‘Name’, ‘TestAcc’);

fieldsValueByName.put(‘BillingCity’, ‘Amsterdam’);

fieldsValueByName.put(‘BillingCountry’, ‘Netherlands’);

fieldsValueByName.put(‘BillingStreet’, ‘Amstel 1’);

accounts = (List<Account>)TestDataGeneration.generatesObjects(‘Account’, 1, fieldsValueByName);

insert accounts;

String jsonText = Summer16.generateJson(false);

System.assert(jsonText.contains(‘null’));

}

static testMethod void validateGenerateJsonSuppressApexObjectNulls() {

List<Account> accounts = new List<Account>();

Map<String, Object> fieldsValueByName = new Map <String, Object>();

fieldsValueByName.put(‘Name’, ‘TestAcc’);

fieldsValueByName.put(‘BillingCity’, ‘Amsterdam’);

fieldsValueByName.put(‘BillingCountry’, ‘Netherlands’);

fieldsValueByName.put(‘BillingStreet’, ‘Amstel 1’);

accounts = (List<Account>)TestDataGeneration.generatesObjects(‘Account’, 1, fieldsValueByName);

insert accounts;

String jsonText = Summer16.generateJson(true);

System.assert(!jsonText.contains(‘null’));

}

}

We test both test cases when it contains and not contains null values. Lets run tests and take a look in debug what is generated JSON looks like.

As expected in first test we get:

As we can see it contains some additional info with it’s value null. For the second test we will get something like this:

Now field with null value is suppressed. It might be not that exciting, but it is very handy when you a lot of integrations. I will be happy to hear from you about thing that you like in development part of Salesforce Summer ’16 release in comments. Have fun creating awesome things with Salesforce.

Are you ready to take the next step in your career? At Beethree we are looking for Salesforce experts to join our awesome team! Interested? Please get in touch.