控制器未从闪电组件初始化处理程序接收对象

时间:2019-07-03 18:55:02

标签: javascript salesforce apex salesforce-lightning

我有一个Lightning Accordion,其中包含一个数据表,该表未接收填充该表的对象数据。该组件获取Case对象,将其存储为recordData并将其发送到我的控制器类。 它应显示父帐户的所有子域的数据表,如下面的示例所示。 enter image description here

CONTROLLER CLASS:
public class CollectionCaseDomainsController {

    @AuraEnabled
    public static List<Domain__c> queryDomains(Case CurrentCase) {        
        // if (CurrentCase.AccountId == null) { return;}

        //query all the children accounts (if any)
        Set<Id> allAccountIds = new Set<Id>{CurrentCase.AccountId};
        Boolean done = false;
        Set<Id> currentLevel = new Set<Id>{CurrentCase.AccountId};
        Integer count = 0;
        while(!done) {
            List<Account> children = [ SELECT Id FROM Account WHERE Parent.Id IN :currentLevel ];
            count++;
            currentLevel = new Set<Id>();
            for (Account child : children) {
                currentLevel.add(child.Id);
                allAccountIds.add(child.Id);
            }

            //added in a count, to prevent this getting stuck in an infinate loop
            if (currentLevel.size() == 0 || count > 9) {
                done = true;
            }
        }

        //query the assets
        List<Asset> assets = [ SELECT Domain__c FROM Asset WHERE AccountId IN :allAccountIds ];

        Set<Id> domainIds = new Set<Id>();
        for (Asset a : assets) {
            domainIds.add(a.Domain__c);
        }

        return [ SELECT Name FROM Domain__c WHERE Id IN :domainIds ];
    }
}
LIGHTNING COMPONENT:
<aura:component controller="CollectionCaseDomainsController" implements="flexipage:availableForRecordHome,force:hasRecordId" access="global" >

    <aura:attribute name="recordId" type="Id" />
    <aura:attribute name="CurrentCase" type="Case" />
    <aura:attribute name="Domains" type="List" />
    <aura:attribute name="Columns" type="List" />

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

    <force:recordData aura:id="caseRecord"
                      recordId="{!v.recordId}"
                      targetFields="{!v.CurrentCase}"
                      layoutType="FULL"
                      />

    <lightning:accordion activeSectionName="Domains">

        <lightning:accordionSection name="Domains" label="Domains">

            <lightning:datatable data="{ !v.Domains }" columns="{ !v.Columns }" keyField="Id" hideCheckboxColumn="true"/>

        </lightning:accordionSection>

    </lightning:accordion>
</aura:component>
COMPONENT CONTROLLER:
({
    doInit : function(component, event, helper) {
        component.set("v.Columns", [
            {label:"Domain Name", fieldName:"Name", type:"text"}
        ]);

        var action = component.get("c.queryDomains");

        action.setParams({
            CurrentCase: component.get("v.CurrentCase")
        });

        action.setCallback(this, function(data) {
            var state = data.getState();
            if (state === "SUCCESS") {
                component.set("v.Domains", data.getReturnValue());
            } else {
                console.log("Failed with state: " + state);
            }
        });

        // Send action off to be executed
        $A.enqueueAction(action);
    }
})

1 个答案:

答案 0 :(得分:0)

您的问题对数据出问题的地方并没有太多的了解,但是我对这个问题有一个有根据的猜测:您的init处理程序和{{1} }的加载过程。

force:recordData中,您要做

doInit()

但是 action.setParams({ CurrentCase: component.get("v.CurrentCase") }); 由您的v.CurrentCase填充:

<force:recordData>

异步加载。您无法保证<force:recordData aura:id="caseRecord" recordId="{!v.recordId}" targetFields="{!v.CurrentCase}" layoutType="FULL" /> 将(永远或每次)在调用<force:recordData>处理程序之前完成加载。

幸运的是,您不需要这样。只需将您的init属性传递给服务器端控制器,然后在此执行您需要的所有SOQL。