简体   繁体   English

Salesforce:如何在 aura 属性中获取字段值并在组件中使用 if 语句

[英]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...).我是 Salesforce 开发的新手,但对 web 开发(Angular、React、C#、JS 等)并不陌生。 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 :在我的 JS controller 中,当我console.log()我的属性oppty时,我得到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?我想一个更好的问题是:如何从 object Opportunity 的字段中获取值并将其绑定到 aura 属性?

I see you have not included the code for fetching the record and not even having any call to APEX to fetch the data.我看到您没有包含用于获取记录的代码,甚至没有调用 APEX 来获取数据。

There are 2 ways to get the data.有两种获取数据的方法。

  1. From lightning component Refer来自闪电组件参考

 <aura:attribute name="recordId" type="String" /> <lightning:recordForm recordId="{.v,recordId}" objectApiName="Opportunity" mode="readonly" fields="Name, MyCustomField__c" />

  1. Using APEX call使用 APEX 调用

    Create AuraEnabled method in APEX class and consume in component helper.在 APEX class 中创建 AuraEnabled 方法并在组件助手中使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Salesforce Aura 组件 - 获取 Flow 变量的当前 v.recordId - Salesforce Aura Component - getting current v.recordId for Flow variable salesforce 在 Lightning 组件中获取帐户 ID - salesforce get account id in lightning component 具有值的角度属性分量 - Angular attribute component with value 如何在Salesforce中创建自定义的登录组件? - How to create customized login component in Salesforce? 如何使用OmniFaces组件工具在XHTML页面上获取组件的HTML / CSS属性值? - How can I use OmniFaces Components util to get HTML/CSS property value of a component on XHTML page? 如何定位此组件的HTML属性,是否有更简单的方法来获取此元素? - How to target this component HTML attribute, is there a easier way to get this element? JSF自定义组件:如何获取属性<f:ajax /> - JSF Custom Component: How to get attribute of <f:ajax /> 如何将API调用的响应从触发器传递到Salesforce中的侧边栏组件? - how to pass response of api call from trigger to sidebar component in salesforce? 角度2:如何将字段的默认值从父组件设置回子组件? - Angular 2: How to set default value of field from parent component back to child component? 根据布尔值有条件地切换组件属性的值 - Conditionally toggle value of component attribute based on a boolean
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM