objectApiName = 'Account'    

I have these two values that I need to serialize in the below given format:

 {"objectApiName":"Account", "fields": [ {"Type": "LABEL", "Value": "Please enter additional details below.", "Order": 1 }, {"Type": "FIELD", "Value": "AccountNumber", "Order": 2 }, {"Type": "FIELD", "Value": "NumberOfEmployees", "Order": 3 } ] }

How can I do this?

2 Answers 2


You could create DTO (Data transfer object) class, set values as appropriate and serialize them.

For ex.,

class DtoField {
    public String Type;
    public String Value;
    public Integer Order;
    public DtoField(String type, String value, Integer order) {
        this.Type = type;
        this.Value = value;
        this.Order = order;

class DtoObject {
    public String objectApiName;
    public List<DtoField> fields;

DtoObject obj = new DtoObject();
obj.objectApiName = 'Account';
obj.fields = new List<DtoField>{
  new DtoField('Order', 'Please enter additional details below.', 1),
  new DtoField('FIELD', 'AccountNumber', 2),
  new DtoField('FIELD', 'NumberOfEmployees', 3)


Which prints

{"objectApiName":"Account","fields":[{"Value":"Please enter additional details below.","Type":"Order","Order":1},{"Value":"AccountNumber","Type":"FIELD","Order":2},{"Value":"NumberOfEmployees","Type":"FIELD","Order":3}]}
  • how can I make this dynamic if I want the values to be supplied from a controller
    – code234
    Mar 5, 2022 at 2:49
  • 1
    The code example above can be put into a controller and create instances of DtoField as many times as you want based on the soql data.
    – metasync
    Mar 5, 2022 at 3:01

Another choice is to use generic maps and lists in your controller:

Map<String, Object> root = new Map<String, Object>{
    'objectApiName' => 'Account',
    'fields' => new List<Object>{
        new Map<String, Object>{
            'Type' => 'LABEL',
            'Value' => 'Please enter additional details below.',
            'Order' => 1


but +1 to metasync's DTO approach if you have many field instances because it locks down the names in the JSON rather than requiring them to be repeated.

If you have many values on an object to set, it can be better to use the fluent interface approach so each value is named rather than relying on the position in the constructor parameters:

class DtoField {

    public String Type;
    public String Value;
    public Integer Order;

    DtoField type(String Type) {
        this.Type = Type;
        return this;

    DtoField value(String Value) {
        this. Value = Value;
        return this;

    DtoField order(Integer Order) {
        this.Order = Order;
        return this;


new DtoField().type('LABEL').value('Please enter additional details below.').order(1);


new DtoField()
    .value('Please enter additional details below.')

This approach is particularly good if some values are optional as it lets you leave the values out without having to have null place keepers.

You must log in to answer this question.

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