1

I am facing issues while adding list data: Here is my code:

public class Wrapper{    
    public Boolean open{get;set;}
    public String label{get;set;}        
    public String key{get;set;}        
    public List<Children> children;        
}

public class Children{
    public String key{get;set;}
    public String label{get;set;}
}

Getting data from List:

List<object> allData = [SELECT Id, Name
                                            , Impact__c
                                            , Implementation_Start_Date__c
                                            , Implementation_Stop_Date__c
                                            , Primary_Stakeholder__c    
                                            , Project__c
                                            FROM Object
                                            WHERE Project__c = '5458652'];

I need to parse data into JSON; So the reason I created MAP Object:

    Map<String, List<Wrapper>> stkMap = new Map<String, List<Wrapper>>();
    List<Wrapper> prList = new List<Wrapper>();
    List<Children> chList = new List<Children>();

for (Object obj: allData ) {
    Wrapper pr = new Wrapper();       
    Children ch = new Children();

    ch.key = obj.Project__c;
    ch.label =  obj.Impact_Type__c;
    //chList.add(ch);
    //system.debug('stkMap.containsKey(obj.Primary_Stakeholder__c)' + stkMap.containsKey(obj.Primary_Stakeholder__c));
    if(stkMap.containsKey(obj.Primary_Stakeholder__c)){ 
       *****Need Help HERE*****************************************************/
       //I need to check if key is present then get data from MAP list and udpate it with child list only
       //Below is one of the try I did to do so
        /*System.debug('PTrace: In stkMap.containsKey Method ');        
        jsonString =  JSON.serialize(new Map<String, Object> {            
            'children' => ch
                });
        System.debug('PTrace:jsonString-' + JSON.serialize(jsonString));
        pr.children = New List<Children>{ch};  
        prList.add(pr1);
        stkMap.put(obj.Primary_Stakeholder__c, prList);  */     
        
    }else{
        pr.key = obj.Id;
        pr.label =  obj.Primary_Stakeholder__c;
        pr.open = true;

        pr.children = New List<Children>{ch};
        prList.add(pr); 
        
        stkMap.put(obj.Primary_Stakeholder__c, prList);
    }
} 

Could you please help me write the condition in if?

Here is the output I'm expecting:

[{key:110, label:"Parent", open: true, children: [
            {key:110, label:"Child 1"},
            {key:50, label:"Child 2"},
            {key:60, label:"Child 3"},
            {key:70, label:"Child 4"}
        ]}]

1 Answer 1

1

Here is what I think you are looking for:

Map<String, Wrapper> prs = new Map<String, Wrapper>();
for (Object obj: allData) {

    Wrapper pr = prs.get(obj.Primary_Stakeholder__c);
    if (pr == null) {
        // Need a new object to represent the primary stakeholder
        pr = new Wrapper();
        pr.key = obj.Id;
        pr.label =  obj.Primary_Stakeholder__c;
        pr.open = true;
        pr.children = new List<Children>();
        prs.put(obj.Primary_Stakeholder__c, pr);
    } else {
        // Already a primary stakeholder object so use that
    }

    // Add the child
    Children ch = new Children();
    ch.key = obj.Project__c;
    ch.label =  obj.Impact_Type__c;
    pr.children.add(ch);
}

// The map avoided duplicates, but now want to serialise the list of values
String jsonString = JSON.serializePretty(prs.values());

if the aim is to collect together all the "children" that have a common Primary_Stakeholder__c.

I suggest you choose clearer names than Wrapper and Children to make the code read better e.g. Stakeholder and Project. And you can do a bit more in those classes e.g.:

public class Stakeholder {

    public String key;
    public String label;     
    public Boolean open = true; 
    public List<Project> projects = new List<Project>();     

    public Stakeholder(String key, String label) {
        this.key = key;
        this.label = label;
    }
}

allowing:

if (pr == null) {
    // Need a new object to represent the primary stakeholder
    pr = new Stakeholder(obj.Id, obj.Primary_Stakeholder__c);
    prs.put(obj.Primary_Stakeholder__c, pr);
}
2
  • Thank you very much Keith sir!, this worked perfectly for me..
    – Prasad
    Commented Nov 13, 2020 at 14:22
  • 1
    @Prasad You are welcome. Do take the time to be sure you follow what is going on; these parent/child situations are quite common.
    – Keith C
    Commented Nov 13, 2020 at 16:07

You must log in to answer this question.

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