0

I am quite new to APex and LWC and I am not sure how to format my code I would like to trigger an apex class from a loop if it was another LWC I would do but since it is from the same component I am not sure how to do it.

So I am looping through the status of one of my objects in the following way:

<div class="card_wrapper"> 
            <template for:each={pickVals} for:item="item">
                <div class="stageContainer" key={item} style={calcWidth}>
                {item}
                </div>
            </template>
</div> 

now instead of item I would like there to trigger an apex class that count the number of object with that Status:

@AuraEnabled(cacheable=true)
    public static list<AggregateResult> PropCountStatus(string CountryId, string Status){
        return [SELECT count (Name)
                FROM Lead 
                WHERE Market__c = :CountryId 
                AND Status = :Status ];
    }

How in my HTML can I trigger the current item in my apex class and return the value from my APEX class

Thx you very much

1 Answer 1

2

It would be more efficient to do all of the values at once:

@AuraEnabled(cacheable=true)
public static list<AggregateResult> PropCountStatus(string CountryId){
    return [SELECT Market__c market, count(Id) sum
            FROM Lead 
            WHERE Market__c = :CountryId
            GROUP BY Market__c];
}

<div class="stageContainer" key={item.market} style={calcWidth}>
    {item.sum}
</div>

But, if you wanted to not modify the Apex, you'd end up calling the server many times:

const marketByStatus = await Promise.all(statuses.map(status => PropCountStatus({ CountryId: countryId, Status: status}));

Either way, there's no real way to call Apex in a loop in the template, you need to call Apex in a loop in your JavaScript.

You must log in to answer this question.

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