0

This is my first time creating a lightning component and I am struggling with one aspect of it (although I am sure my code below has several other mistakes too). In my Case record, there is a field that has a lookup to an account. The account has Phone number, email and name. The account record also has a field value called TCode, which will always be populated. My lighting component, which will be on the Case lightning page, needs to display the account info as well as all related account info. The way we do that is by going to the account on the case and querying all accounts under the TCode value. Therefore, I wrote my apex controller as follows:

    public class CS_fetchAccountDetails {

    Private static Id FieldOfficeRTId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Field Office').getRecordTypeId();
    private static Case c;

    @AuraEnabled
    public static List<Account> fetchAccountList(Id caseId){
        c = [select Id, AccountId from Case WHERE Id=:caseId];

        Account acct = [SELECT Id, NSS_Territory_Code__c FROM Account where Id=:c.AccountId];

        return [select Id, Name, NSS_Territory_Code__c, Field_Office_Email__c, Phone, BillingAddress, RecordTypeId from Account WHERE NSS_Territory_Code__c =:acct.NSS_Territory_Code__c AND RecordTypeId =:FieldOfficeRTId];

    }

}

Again the part where I am struggling with is how do I pass the field value of Tcode to the apex in my lighting component. Also, my LC seems to be account focused when it should actually be on Case and then traverse to the account listed in the case record. Can someone please look at my code and pint my mistake and what I need to fix/add?

eachAccountonCase.cmp:

<aura:component >
    implements="flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes" access="global" >

    <aura:attribute name="accountName" type="String" />

    <aura:attribute name="phoneNumber" type="String" />

    <aura:attribute name="fieldEmail" type="String" /> 

    <aura:attribute name="fieldAddress" type="Address" />

    <aura:attribute name="territoryCode" type="String" />



        <div class="slds-tile__detail">

            <dl class="slds-list_horizontal slds-wrap">

                <dt class="slds-item_label slds-text-color_weak slds-truncate" title="First Label">Account Name:</dt>

                <dd class="slds-item_detail slds-truncate" title="Description for first label">{!v.accountName}</dd>

                <dt class="slds-item_label slds-text-color_weak slds-truncate" title="Second Label">Phone Number:</dt>

                <dd class="slds-item_detail slds-truncate" title="Description for second label">{!v.phoneNumber}</dd>

                <dt class="slds-item_label slds-text-color_weak slds-truncate" title="Territory Code Label">Territory Code:</dt>

                <dd class="slds-item_detail slds-truncate" title="Description for territory code label">{!v.territoryCode}</dd>

                <dt class="slds-item_label slds-text-color_weak slds-truncate" title="Third Label">Field Office Email:</dt>

                <dd class="slds-item_detail slds-truncate" title="Description for Third label">{!v.fieldEmail}</dd>  

                <dt class="slds-item_label slds-text-color_weak slds-truncate" title="Fourth Label">Field Office Address:</dt>

                <dd class="slds-item_detail slds-truncate" title="Description for Fourth label">{!v.fieldAddress}</dd>

                </dl>

        </div>



</aura:component>

accountListonCase.cmp:

    <aura:component controller="fetchAccountDetails" implements="flexipage:availableForAllPageTypes" access="global" >

<aura:attribute name="recordId" type="Id" />        
<aura:attribute name="accountList" type="account[]" />

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

        <div class="demo-only" style="width: 30rem;">

        <article class="slds-tile">

            <h3 class="slds-tile__title slds-truncate" title="Field Office Information"><a href="javascript:void(0);">Field Office Information</a></h3>

            <aura:iteration items="{!v.accountList}" var="item">

                <c:eachAccountonCase accountName="{!item.Name}" phoneNumber="{!item.Phone}" fieldEmail="{!item.Email}" fieldAddress = "{!item.BillingAddress}" territoryCode="{!item.NSS_Territory_Code}" />

                <br/>

            </aura:iteration>
            </article>

        </div>

    </aura:component>

accountListonCaseController.js:

    ({
    doInit : function(component, event, helper) {
        //var territory = component.get("v.territoryCode");
        var action = component.get("c.fetchAccountList");       
        action.setParams({
            "caseId": component.get("v.recordId")
        });

        action.setCallback(this, function(data){
            component.set("v.accountList", data.getReturnValue());
        });       

        $A.enqueueAction(action);

                }

})

MainApp:

<aura:application extends="force:slds">
    <c:accountListonCase />
</aura:application>

1 Answer 1

1

Update: Please read about force:hasRecordId here.

  1. You need to implement force:hasRecordId on accountListonCase component.
  2. You should not define a recordId attribute in your accountListonCase cmp.
  3. The v.recordId is populated only if the component is added to a record page.


The setParams method is used to pass the parameters to the Aura enabled methods in Apex controllers.

In this line

action.setParams({"territoryCode": territory});

setParams method takes a JSON object where the argument key should match the parameter name defined in your Apex controller. So you should change the line as:

action.setParams({tCode: territory}); // Passing parameters at once.

or

action.setParam('tCode', territory); // Passing parameters one at a time

You can also temporarily put a console log to make sure you are getting territory value.

3
  • I have updated my code above. The apex has changed. Also, what you mentioned was not the main problem. I just typed/copied it wrong in there. I did not have a recordId attribute (not sure if i put the in the right spot or not). But i have the latest code and its not working Commented Mar 15, 2020 at 18:55
  • Your latest code is not yet reflecting.
    – manjit5190
    Commented Mar 15, 2020 at 18:58
  • What do ou mean @manjit5190? My apex is significantly different now and I added a recordId line on accountListonCase component. Are you not seeing my changes? But as of now, with my latest code I dont know what the problem is and how I should display all the accounts on my case record. Thanks! Commented Mar 15, 2020 at 19:06

You must log in to answer this question.

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