0

I have the following soql

 @AuraEnabled(cacheable=true)
    public static List<AggregatedResult__c> getTarget() {
        Id currentUserId  = UserInfo.getUserId(); 
        String query = 'SELECT ';
        for(Schema.FieldSetMember f : SObjectType.AggregatedResult__c.FieldSets.Dashboard_Fields.getFields()) {
            query += f.getFieldPath() + ', ';
        }
        query += 'Name, User__c, (Select id, Main_Target__c from Targets__r) FROM AggregatedResult__c where User__c =:currentUserId' ;
        List<AggregatedResult__c> aggr = Database.query(query);
        return aggr;
    }

How do I get the value for Main_Target__c in Javascript.

I tried the following but isnt working

@wire(getTarget)
     dataRecord({data, error}){
       if(data){
for (let i of data[0].Targets__r) {
  this.targetEvt = data[0].Targets__r.Main_Target__c
   console.log("targetEvt1: ",targetEvt);
}
       }
      

1 Answer 1

1

Problem lies in this line this.targetEvt = data[0].Targets__r.Main_Target__c. It should be this.targetEvt = i.Main_Target__c.

Datatype of data[0].Targets__r is an array. So, trying access the property of an array element directly from an array (without iterating thru) doesn't make sense. You are already using a for loop where i represents elements in the array Targets__r, so you should use i.Main_Target__c.

If you need the first element value of the child array, you can change the code to data[0].Targets__r[0].Main_Target__c and skip the for loop.

You must log in to answer this question.

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