I am trying to iterate through the list which is stored inside a wrapper list. I want to extract a field value from it and store inside a Set (wrapper again).
Here is my code -
@AuraEnabled(cacheable=true)
public static List<myWrap> getObjectData(
String accId
) {
List<myWrap> wrapperList = new List<myWrap>();
try{
for(Object1__c xyz : [ SELECT Id, Name,
(SELECT Id, Picklist_Names__c FROM Object2__r)
FROM Object1__c
WHERE Account__c =: accId]){
myWrap wr = new myWrap();
wr.objName = xyz.Name;
wr.obj2Data = xyz.Object__r;
//I know I cannot do xyz.Object__r.Picklist_Names__c since it's a list<Object2>
}
}
catch(Exception e){
system.debug('Inside Catch');
throw new AuraHandledException('Exception occured');
}
return wrapperList;
}
public class myWrap{
@AuraEnabled
public String objName{get;set;}
@AuraEnabled
public List<Object2__c> obj2Data{get;set;}
@AuraEnabled
public Set<String> uniqueNames{get;set;}
}
}
I want to store the Picklist_names__c from obj2Data in the uniqueNames(wrapper Set).
The Why? I am using this returned data from Apex in my LWC JS and showing it inside a lightning-combobox. Right now duplicate values show up on the screen for Picklist_Names__c when I iterate in my LWC JS (using forEach). I am trying to remove the duplicates so I thought of cutting them off in Apex itself before they can be returned to LWC.
I did try to iterate through the wrapper list List but I'm somehow unable to get hold of public List<Object2__c> obj2Data
. (keep running into syntax issues)
Would really appreciate if anyone can help here or suggest a better approach.
Thanks!
Object1__r.Name
; But this means conditional logic to know when to build a new wrapper