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>