'Salesforce: How to get field value in aura attribute and use if statement in component
I'm new to Salesforce development, but not new to web development (Angular, React, C#, JS, etc...). So it's been very frustrating that I cannot get something so simple as the example below to work. I want to display content based upon a condition of a custom fields value:
<aura:component controller="MyTestController" implements="flexipage:availableForRecordHome,force:appHostable,lightning:actionOverride,force:hasRecordId">
<aura:attribute name="oppty" type="Opportunity" />
<article class="slds-card">
<aura:if isTrue="{!v.oppty.MyCustomField__c == 'hello'}">
Hello
</aura:if>
<lightning:button label="Click Me" onclick="{!c.handleClick}" class="slds-m-top_medium" />
</article>
</aura:component>
In my JS controller, when I console.log()
my attribute oppty
, I get undefined
:
({
handleClick : function(cmp, event) {
var myOpportunity = cmp.get("v.oppty");
console.log(myOpportunity); // or
console.log(myOpportunity.MyCustomField__c);
}
})
I guess a better question is this: How do I get a value from a field of object Opportunity and bind it to the aura attribute?
Solution 1:[1]
I see you have not included the code for fetching the record and not even having any call to APEX to fetch the data.
There are 2 ways to get the data.
- From lightning component Refer
<aura:attribute name="recordId" type="String" />
<aura:attribute name="opty" type="Opportunity" default="{'sobjectType':
'Opportunity'}" />
<aura:attribute name="recordLoadError" type="String" />
<force:recordData aura:id="recordLoader" recordId="{!v.recordId}"
fields="Name, MyCustomField__c"
targetFields="{!v.opty}" targetError="{!v.recordLoadError}" />
Using APEX call
Create AuraEnabled method in APEX class and consume in component helper.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |