function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sana123Sana123 

How can i get a nested map as return type .

Hlo community ,

public class getTheSobjectDetails {
    public static Map<Sobject, Map<String, List<Sobject>>>  getSobjectfield(Map<String,List<String>> mapOfsobject ,Map<String,List<String>> mapOfsobjectField) {
        
        List<Sobject> accList = new List<Sobject>();

        //Creating set of all account Ids
        Set<id> accIdsSet = new Set<id>() ;
        List<ID> listOfObj = new List<ID>();
            listOfObj.addAll(accIdsSet);
        
        Map<Sobject, Map<String, List<Sobject>>> mapOfParentChildObject = new Map<Sobject, Map<String, List<Sobject>>>();
        //system.debug
        for(String objname : mapOfsobjectField.keySet()){
            system.debug(objname);
            String dynamicParentfields = string.join(mapOfsobjectField.get(objname),',');
           system.debug('dynamicParentfields   '+ '  '+   dynamicParentfields);
            String dynamicsoql = ' SELECT ' + '' +  dynamicParentfields   + ' FROM ' +  objname ;
            for(List<sObject> sobjList1 :Database.query(' SELECT ' + '' +  dynamicParentfields   + ' FROM ' +  objname)){
               Map<Id,Sobject> accountIdObjMap = new Map<Id,Sobject>(sobjList1);
                system.debug('accountIdObjMap' + accountIdObjMap);
            system.debug(dynamicsoql);
            system.debug(sobjList1);  
                
                accList = accountIdObjMap.values();

                //getting set of account Id’s using map.keySet method
                accIdsSet = accountIdObjMap.keySet();
                
                 system.debug(accList);
            system.debug(accIdsSet); 
            }
            if(accList != listOfObj){

            }
        }
    
    }
}

This is my code ..But i cant understand how can i populate data to my nested return type ........

----------In my argument 1i am passing :Map<String, List<String>>: Key: Will be the Parent Object and 
##### Value: List of Child Objects 

---------In my argument 2 i am passing : Map<String, List<String>>: Key: Will be the Object (the object which i pass in 2 argument include all the object which i pass in argument 1)
### and  Value: Listof Fields of the object

now i am trying dynamic soql on object by which i get my output in this form for return type:-
1 Recrod => new Map<String, List<Sobject>>{'Contact' => List<Contact>; 'Opportunity' => List<Opportunity>}

In my execution window i am passing:-
getTheSobjectDetails.getSobjectfield(new Map<String, List<String>>{'Account'=>new List<String>{'Contact''}}, new Map<String, List<String>>{'Account'=>new List<String>{'Id', 'Name'},'Contact' => new List<String>{'Id', 'Name', 'AccountId', 'Account.Name'}});


Please Help me in this ...how can i solve this


 
SwethaSwetha (Salesforce Developers) 
HI Sana,
Recommend reviewing approach mentioned in https://salesforce.stackexchange.com/questions/318037/pass-nested-map-from-apex-to-javascript-controller
https://salesforce.stackexchange.com/questions/203022/apex-nested-map-returns-null

If this information helps, please mark the answer as best. Thank you