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"}
]}]