1

Hello I have an LWC that features a lightning-tree-grid. The intent is to take a set of data from a junction object. I am not able to use a subquery see figure 1 but contains the fields to build this in. The field Product_Family__c can be the top level element in the treeview.

The goal is to build in that relationship and design the JSON as shown in figure 2.

I should mention that the SOQL for pulling data is dynamic and works with the designer properties. I was able to generate the drill down relationship in Apex but given the dynamic nature, it was a lot of code and heavy use of the Schema APIs.

That said I was wondering could you take the JSON in figure 1 and through the use of JavaScript loop over the JSON and reform as shown in figure 2. Has anyone done this before? I have manipulated JSON in JavaScript before but not to this extent. I was just wondering if anyone has written anything like this and if they would be able to give me a few hints on how I turn figure 1 into figure 2.

Figure 1:

[{"Product_Family__c":"ArcGIS Desktop Core Extensions Concurrent Use","Distributor_Request_Product_Name_fx__c":"ArcGIS Data Interoperability for Desktop Concurrent Use License","Existing_Qty__c":2,"Qty_Seats_to_be_Deployed__c":8,"Id":"aK0S00000004FOHKA2"},{"Product_Family__c":"ArcGIS Desktop Core Extensions Concurrent Use","Distributor_Request_Product_Name_fx__c":"ArcGIS Image Analyst for Pro Concurrent Use License","Existing_Qty__c":4,"Qty_Seats_to_be_Deployed__c":10,"Id":"aK0S00000004FOIKA2"},{"Product_Family__c":"ArcGIS Desktop Core Extensions Concurrent Use","Distributor_Request_Product_Name_fx__c":"ArcGIS LocateXT for Desktop Concurrent Use License","Existing_Qty__c":6,"Qty_Seats_to_be_Deployed__c":12,"Id":"aK0S00000004FOJKA2"},{"Product_Family__c":"ArcGIS Desktop Core Extensions Single Use","Distributor_Request_Product_Name_fx__c":"ArcGIS Data Interoperability for Desktop Single Use License","Existing_Qty__c":20,"Qty_Seats_to_be_Deployed__c":22,"Id":"aK0S00000004FrHKAU"},{"Product_Family__c":"ArcGIS Desktop Core Extensions Single Use","Distributor_Request_Product_Name_fx__c":"ArcGIS Image Analyst for Pro Single Use License","Existing_Qty__c":24,"Qty_Seats_to_be_Deployed__c":26,"Id":"aK0S00000004FrIKAU"},{"Product_Family__c":"ArcGIS Desktop Core Extensions Single Use","Distributor_Request_Product_Name_fx__c":"ArcGIS LocateXT for Desktop Single Use License","Existing_Qty__c":28,"Qty_Seats_to_be_Deployed__c":30,"Id":"aK0S00000004FrJKAU"}]

Figure 2:

[
    {
        'Product_Family__c': 'ArcGIS Desktop Core Extensions Concurrent Use',
        'Id': 'aK0S00000004FOHKA2',
        _children: [
            {
                'Distributor_Request_Product_Name_fx__c': 'ArcGIS Data Interoperability for Desktop Concurrent Use License',
                'Existing_Qty__c': '2',
                'Qty_Seats_to_be_Deployed__c': '8',
                'Id': 'aK0S00000004FOHKA2',
            },
            {
                'Distributor_Request_Product_Name_fx__c': 'ArcGIS Image Analyst for Pro Concurrent Use License',
                'Existing_Qty__c': '4',
                'Qty_Seats_to_be_Deployed__c': '10',
                'Id': 'aK0S00000004FOIKA2',
            },
            {
                'Distributor_Request_Product_Name_fx__c': 'ArcGIS LocateXT for Desktop Concurrent Use License',
                'Existing_Qty__c': '6',
                'Qty_Seats_to_be_Deployed__c': '12',
                'Id': 'aK0S00000004FOJKA2',
            },
            
        ],
    },
    {
        'Product_Family__c': 'ArcGIS Desktop Core Extensions Single Use',
        'Id': 'aK0S00000004FOHKA2',
        _children: [
            {
                'Distributor_Request_Product_Name_fx__c': 'ArcGIS Data Interoperability for Desktop Single Use License',
                'Existing_Qty__c': '20',
                'Qty_Seats_to_be_Deployed__c': '22',
                'Id': 'aK0S00000004FrHKAU',
            },
            {
                'Distributor_Request_Product_Name_fx__c': 'ArcGIS Image Analyst for Pro Single Use License',
                'Existing_Qty__c': '24',
                'Qty_Seats_to_be_Deployed__c': '26',
                'Id': 'aK0S00000004FrIKAU',
            },
            {
                'Distributor_Request_Product_Name_fx__c': 'ArcGIS LocateXT for Desktop Single Use License',
                'Existing_Qty__c': '28',
                'Qty_Seats_to_be_Deployed__c': '30',
                'Id': 'aK0S00000004FrJKAU',
            },
            
        ],
    },
];
1
  • Thanks Rahul. Spelling is certainly key for searching!
    – bluray
    Oct 23, 2020 at 16:32

1 Answer 1

0

The below code will yield similar result. Although there is scope of improvement for the below code, but you can use this as a boilerplate -

var jsonStr = '[{"Product_Family__c":"ArcGIS Desktop Core Extensions Concurrent Use","Distributor_Request_Product_Name_fx__c":"ArcGIS Data Interoperability for Desktop Concurrent Use License","Existing_Qty__c":2,"Qty_Seats_to_be_Deployed__c":8,"Id":"aK0S00000004FOHKA2"},{"Product_Family__c":"ArcGIS Desktop Core Extensions Concurrent Use","Distributor_Request_Product_Name_fx__c":"ArcGIS Image Analyst for Pro Concurrent Use License","Existing_Qty__c":4,"Qty_Seats_to_be_Deployed__c":10,"Id":"aK0S00000004FOIKA2"},{"Product_Family__c":"ArcGIS Desktop Core Extensions Concurrent Use","Distributor_Request_Product_Name_fx__c":"ArcGIS LocateXT for Desktop Concurrent Use License","Existing_Qty__c":6,"Qty_Seats_to_be_Deployed__c":12,"Id":"aK0S00000004FOJKA2"},{"Product_Family__c":"ArcGIS Desktop Core Extensions Single Use","Distributor_Request_Product_Name_fx__c":"ArcGIS Data Interoperability for Desktop Single Use License","Existing_Qty__c":20,"Qty_Seats_to_be_Deployed__c":22,"Id":"aK0S00000004FrHKAU"},{"Product_Family__c":"ArcGIS Desktop Core Extensions Single Use","Distributor_Request_Product_Name_fx__c":"ArcGIS Image Analyst for Pro Single Use License","Existing_Qty__c":24,"Qty_Seats_to_be_Deployed__c":26,"Id":"aK0S00000004FrIKAU"},{"Product_Family__c":"ArcGIS Desktop Core Extensions Single Use","Distributor_Request_Product_Name_fx__c":"ArcGIS LocateXT for Desktop Single Use License","Existing_Qty__c":28,"Qty_Seats_to_be_Deployed__c":30,"Id":"aK0S00000004FrJKAU"}]';
var jObj= JSON.parse( jsonStr);
var result = new Array();
var map = new Map();
jObj.forEach( function(element) { 
     
    if( map.has( element.Product_Family__c) )
    {
        var tempArr = map.get(element.Product_Family__c);
        tempArr.push(element);
        map.set(element.Product_Family__c, tempArr);
    }
    else{
        var newArr = new Array();
        newArr.push( element);
        map.set( element.Product_Family__c , newArr);
    }
    
} );

map.forEach((value, key, map) => {
        var tempMap = new Map();
        tempMap.set( "Product_Family__c" , key);
        tempMap.set("Id", map.get(key)[0].Id);
        map.get( key).forEach( element => delete element.Product_Family__c);
        tempMap.set("_children" , map.get(key) );
        let obj = Object.fromEntries(tempMap.entries()); // make a plain object (*)
        result.push(obj);
                
});

console.log ( JSON.stringify ( result) ) ;
3
  • Many thanks for the code Ysr Shk
    – bluray
    Oct 23, 2020 at 13:55
  • if this code/answer works for you, please select it as answer.
    – Ysr Shk
    Oct 23, 2020 at 14:15
  • 1
    This was great start. Out of the gate it generated JSON that could be parsed by a number of online parsers as well as JavaScript. It turns out that the lightning-tree-grid is really fussy about the format of the JSON. But this is really close. Ill add error handling and look for a way to reference the parent field by the use of a variable to make it truly dynamic.
    – bluray
    Oct 23, 2020 at 18:43

You must log in to answer this question.

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