0

I am working on one apex class which is returning MAP.

Can anyone help me out how to write wrapper which will add the returned map into wrapper class.

I want to pass returned map values to LWC component to display it

public class LTNG_HTERCA_RightTagWrapper {
@AuraEnabled(Cacheable=true)
public static Map<string,List<Shadow_Note__c>> populateTaggedNotesForCase(Id RecordId){
    String caseRecordId = '';
    List<Root_Cause_Analysis__c> rcaRecords = [Select Id, Case__c from Root_Cause_Analysis__c where Id =: RecordId order by CreatedDate desc];
    if(rcaRecords.size() > 0)
        caseRecordId = rcaRecords[0].Case__c;
    Map<string,List<Shadow_Note__c>> res = new Map<string,List<Shadow_Note__c>>();
    Schema.DescribeFieldResult fieldResult = Shadow_Note__c.Note_Tag__c.getDescribe();
    List<Schema.PicklistEntry> ple =fieldResult.getPicklistValues();
    for (Schema.PicklistEntry a:ple ){
            res.put(a.value , new List<Shadow_Note__c>());
    }
    List<LTNG_HTERCA_RightTagWrapper> shadowNotesTagList = new List<LTNG_HTERCA_RightTagWrapper>();
    List<Shadow_Note__c> allshadownotes = [SELECT Note__c,Note_Tag__c,Date_Time_Created_in_CSOne__c,CreatedBy_Name__c From Shadow_Note__c 
    WHERE Case__c =: caseRecordId AND Note_Tag__c != null 
    ORDER BY CreatedDate ASC];
    for(Shadow_Note__c sn: allshadownotes){
        List<String> noteTags = sn.Note_Tag__c.split(';');
        for(String tag: noteTags){
            if(res.containsKey(tag)){
                List<Shadow_Note__c> notesList = res.get(tag);
                notesList.add(sn);
                res.put(tag, notesList);
                system.debug('res is ++: ' + res);
            }
        }
    }
    System.debug('the result is:' + JSON.serializePretty(res));
    return res;
    
}
0

If you want to dynamically cast to Wrapper from some Map you can do it like this.

Map<String, Object> obj;// some values

WrapperClass wrapper = (WrapperClass) JSON.deserialize(JSON.serialize(obj), WrapperClass.class);
2
  • hello, thanks a lot for helping me out in this issue, actually I want to pass those map values to LWC component to display it
    – Harish
    Feb 11 at 12:03
  • then you can just return the map (or Object), what is the issue with that?
    – ytiq
    Feb 11 at 12:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.