0

I am fairly new to apex and lightning components so I struggle with a probably simple request. I want to pass an account field (berufskategorie__c) to the lightning component paramater (main) which then should get passed to the apex class variable (:main).

Here is my apex class:

public with sharing class ReferenzkundeNachKategorie {
    @AuraEnabled
    
   public static List<AggregateResult> getList (String main) {
     
   list<AggregateResult> CampaignList = [
     
   SELECT company__c, Count(Id)
    FROM Anzeigen_bersicht__c 
      WHERE Start_in_Tagen__c <= 365 AND id in (select Anzeigen__c FROM Kategorie__c WHERE Main__c = :main)
   GROUP BY company__c 
ORDER BY Count(Id) DESC
LIMIT 10

 ];

return CampaignList;
 }}

Here my component:

<aura:component controller = "ReferenzkundeNachKategorie" implements="flexipage:availableForRecordHome,force:hasRecordId,lightning:availableForFlowScreens" access="global" >
 
    <aura:attribute name="AnzeigenList" type="list" />  
    <aura:attribute type="AggregateResult[]" name="CampaignList"  />
    <aura:attribute name="mycolumns" type="list" />
   
        
  <aura:handler name="init" value="{!this}" action="{!c.init}" /> 

    
   <lightning:datatable data="{! v.CampaignList }" 
                     columns="{! v.AnzeigenList }" 
                     keyField="id"
                 
                     hideCheckboxColumn="true"/>
 
</aura:component>

Here my controller

({
 
   init: function(component, event, helper) {
       component.set('v.AnzeigenList', [
            {label: 'Unternehmen', fieldName: 'company__c', type: 'text'},
          {label: 'Anzahl Anzeigen', fieldName: 'expr0', type: 'integer'}
           
        
        ]);
    
        var campList = component.get("c.getList");
        campList.setParams
        ({
            main: component.get("v.main__c")
        });
        
        campList.setCallback(this, function(data) 
                           {
                               component.set("v.CampaignList", data.getReturnValue());
                           });
        $A.enqueueAction(campList); 
 
    } 
  
    
})

When I hardcode my apex class to

WHERE Start_in_Tagen__c <= 365 AND id in (select Anzeigen__c FROM Kategorie__c WHERE Main__c = 'somehardcodedvalue')

the result looks like this

enter image description here

The part that baffles me is where do I say that the value for 'main' should be from the account field "berufskategorie__c" as I need dynamic results based on the account values.

Thanks in advance!

1 Answer 1

1

main__c isn't defined in your Aura component.

Without context, I'm not sure if this is correct, but you probably need to use force:recordData here:

<aura:attribute name="record" type="Kategorie__c" />
<force:recordData aura:id="recordLoader"
    recordId="{!v.recordId}"
    fields="Main__c"
    targetFields="{!v.record}"
    recordUpdated="{!c.init}" />

Note that we can't cannot call init before recordUpdated has fired, because the aura:valueInit method fires before recordUpdated does. You'll want to remove the following line of code:

<aura:handler name="init" value="{!this}" action="{!c.init}" /> 

From there, you need to read the property from the record:

init: function(component, event, helper) {
  let main = component.get("v.record").Main__c;
  ...

Note that JavaScript is case-sensitive, so make sure you have the capitalization of the field correct.

2
  • Thanks for reaching out @sfdcfox - you pushed me in the right direction. I had to do some minor changes (switching from "Kategorie__c" to "Account" and "Main__c" to "Berufskategorie__c" but it does what I want to!
    – Jan Tonnet
    Commented Sep 8, 2022 at 12:55
  • any idea why the table works on the account layout but crashes inside a screen flow? Table is basically only one big bar
    – Jan Tonnet
    Commented Sep 8, 2022 at 14:56

You must log in to answer this question.

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