Skip to main content
Tweeted twitter.com/StackSalesforce/status/846569835584770049
Tidied up code formatting
Source Link
Derek F
  • 64.1k
  • 15
  • 55
  • 104

I am trying to create some config for front end based on config objects. I have one custom object where I specify options that I need. Each option is separate custom object of this type. I want to het all the options of some group and make a JSON object to use it at front end to render, disable and so on different parts of UI and buttons.

I have tried something like this:

public static String newSubmissionConfig(){

public static String newSubmissionConfig(){

    NewSubmissionConfig con = new NewSubmissionConfig();
    List<NewSubmissionConfigOption> optList = new List<NewSubmissionConfigOption> ();
    List<FH_List_of_values__C> options = [select Name__c, isActive__c, isRequired__c from options__C where group__c = 'NEW_SUB_CONF']; 

    for (options__C opt:options){
        NewSubmissionConfigOption option = new NewSubmissionConfigOption();
        NewSubmissionConfigOptions o = new NewSubmissionConfigOptions();
        option.name = opt.Name__c;
        o.isActive = opt.isActive__c;
        o.isRequired = opt.isRequired__c;
        option.NewSubmissionConfigOptions = o;
        optList.add(option);
    } 

    con.newSubmissionConfigOption = optList;


    return JSON.serialize(optList);
}

public class NewSubmissionConfig {
    List<NewSubmissionConfigOption> newSubmissionConfigOption {get;set;}    
} 

public class NewSubmissionConfigOption {
    public String name {get;set;}
    public NewSubmissionConfigOptions newSubmissionConfigOptions {get;set;}
} 

public class NewSubmissionConfigOptions{        
    public Boolean isActive {get;set;}
    public Boolean isRequired {get;set;}
}

As expected I get object in the following format Array with Arrays inside:

config[
name: option1, newSubmissionConfigOptions [isActive: true, isReuired: true]
name: option2, newSubmissionConfigOptions [isActive: true, isReuired: true]
]

but what I need is just one object:

config: {option1: {isActive: true, isRequired: true},
         option2: {isActive: true, isRequired: true}}

So I can access this config like: config.option1.isActive Now I'm converting this JSON to proper format in JavaScript, but would be nice to get proper object directly from backend.

option1, option2... is Name__c field from my config custom object. Is there are any way to create such JSON objects right in APEX code?

I am trying to create some config for front end based on config objects. I have one custom object where I specify options that I need. Each option is separate custom object of this type. I want to het all the options of some group and make a JSON object to use it at front end to render, disable and so on different parts of UI and buttons.

I have tried something like this:

public static String newSubmissionConfig(){

NewSubmissionConfig con = new NewSubmissionConfig();
List<NewSubmissionConfigOption> optList = new List<NewSubmissionConfigOption> ();
List<FH_List_of_values__C> options = [select Name__c, isActive__c, isRequired__c from options__C where group__c = 'NEW_SUB_CONF'];
for (options__C opt:options){
    NewSubmissionConfigOption option = new NewSubmissionConfigOption();
    NewSubmissionConfigOptions o = new NewSubmissionConfigOptions();
    option.name = opt.Name__c;
    o.isActive = opt.isActive__c;
    o.isRequired = opt.isRequired__c;
    option.NewSubmissionConfigOptions = o;
    optList.add(option);
}
con.newSubmissionConfigOption = optList;


  return JSON.serialize(optList);
}

public class NewSubmissionConfig {
    List<NewSubmissionConfigOption> newSubmissionConfigOption {get;set;}    
}
public class NewSubmissionConfigOption {
    public String name {get;set;}
    public NewSubmissionConfigOptions newSubmissionConfigOptions {get;set;}
}
public class NewSubmissionConfigOptions{        
    public Boolean isActive {get;set;}
    public Boolean isRequired {get;set;}
}

As expected I get object in the following format Array with Arrays inside:

config[
name: option1, newSubmissionConfigOptions [isActive: true, isReuired: true]
name: option2, newSubmissionConfigOptions [isActive: true, isReuired: true]
]

but what I need is just one object:

config: {option1: {isActive: true, isRequired: true},
         option2: {isActive: true, isRequired: true}}

So I can access this config like: config.option1.isActive Now I'm converting this JSON to proper format in JavaScript, but would be nice to get proper object directly from backend.

option1, option2... is Name__c field from my config custom object. Is there are any way to create such JSON objects right in APEX code?

I am trying to create some config for front end based on config objects. I have one custom object where I specify options that I need. Each option is separate custom object of this type. I want to het all the options of some group and make a JSON object to use it at front end to render, disable and so on different parts of UI and buttons.

I have tried something like this:

public static String newSubmissionConfig(){

    NewSubmissionConfig con = new NewSubmissionConfig();
    List<NewSubmissionConfigOption> optList = new List<NewSubmissionConfigOption> ();
    List<FH_List_of_values__C> options = [select Name__c, isActive__c, isRequired__c from options__C where group__c = 'NEW_SUB_CONF']; 

    for (options__C opt:options){
        NewSubmissionConfigOption option = new NewSubmissionConfigOption();
        NewSubmissionConfigOptions o = new NewSubmissionConfigOptions();
        option.name = opt.Name__c;
        o.isActive = opt.isActive__c;
        o.isRequired = opt.isRequired__c;
        option.NewSubmissionConfigOptions = o;
        optList.add(option);
    } 

    con.newSubmissionConfigOption = optList;


    return JSON.serialize(optList);
}

public class NewSubmissionConfig {
    List<NewSubmissionConfigOption> newSubmissionConfigOption {get;set;}    
} 

public class NewSubmissionConfigOption {
    public String name {get;set;}
    public NewSubmissionConfigOptions newSubmissionConfigOptions {get;set;}
} 

public class NewSubmissionConfigOptions{        
    public Boolean isActive {get;set;}
    public Boolean isRequired {get;set;}
}

As expected I get object in the following format Array with Arrays inside:

config[
name: option1, newSubmissionConfigOptions [isActive: true, isReuired: true]
name: option2, newSubmissionConfigOptions [isActive: true, isReuired: true]
]

but what I need is just one object:

config: {option1: {isActive: true, isRequired: true},
         option2: {isActive: true, isRequired: true}}

So I can access this config like: config.option1.isActive Now I'm converting this JSON to proper format in JavaScript, but would be nice to get proper object directly from backend.

option1, option2... is Name__c field from my config custom object. Is there are any way to create such JSON objects right in APEX code?

Source Link

How to create nested JSON object from custom sObject?

I am trying to create some config for front end based on config objects. I have one custom object where I specify options that I need. Each option is separate custom object of this type. I want to het all the options of some group and make a JSON object to use it at front end to render, disable and so on different parts of UI and buttons.

I have tried something like this:

public static String newSubmissionConfig(){

NewSubmissionConfig con = new NewSubmissionConfig();
List<NewSubmissionConfigOption> optList = new List<NewSubmissionConfigOption> ();
List<FH_List_of_values__C> options = [select Name__c, isActive__c, isRequired__c from options__C where group__c = 'NEW_SUB_CONF'];
for (options__C opt:options){
    NewSubmissionConfigOption option = new NewSubmissionConfigOption();
    NewSubmissionConfigOptions o = new NewSubmissionConfigOptions();
    option.name = opt.Name__c;
    o.isActive = opt.isActive__c;
    o.isRequired = opt.isRequired__c;
    option.NewSubmissionConfigOptions = o;
    optList.add(option);
}
con.newSubmissionConfigOption = optList;


  return JSON.serialize(optList);
}

public class NewSubmissionConfig {
    List<NewSubmissionConfigOption> newSubmissionConfigOption {get;set;}    
}
public class NewSubmissionConfigOption {
    public String name {get;set;}
    public NewSubmissionConfigOptions newSubmissionConfigOptions {get;set;}
}
public class NewSubmissionConfigOptions{        
    public Boolean isActive {get;set;}
    public Boolean isRequired {get;set;}
}

As expected I get object in the following format Array with Arrays inside:

config[
name: option1, newSubmissionConfigOptions [isActive: true, isReuired: true]
name: option2, newSubmissionConfigOptions [isActive: true, isReuired: true]
]

but what I need is just one object:

config: {option1: {isActive: true, isRequired: true},
         option2: {isActive: true, isRequired: true}}

So I can access this config like: config.option1.isActive Now I'm converting this JSON to proper format in JavaScript, but would be nice to get proper object directly from backend.

option1, option2... is Name__c field from my config custom object. Is there are any way to create such JSON objects right in APEX code?