salesforce 在 lightning 组件中获取账户 ID

salesforce get account id in lightning component

我开发了一个 lightning 组件模态弹出窗口以显示在机会页面上。有两个选项是和否。条件是此闪电组件将流传输到一个 visualforce 页面或另一个具有帐户 ID 的页面。如何在 lightning 组件中获取帐户 ID。

<aura:component implements="force:lightningQuickActionWithoutHeader">
  Are you sure you want to proceed?
  <div class="slds-align_absolute-center">
    <lightning:button
      label="No"
      variant="destructive"
      onclick="{!handleNo}"
    ></lightning:button>
    <lightning:button label="Yes" onclick="{!c.handleYes}"></lightning:button>
  </div>
</aura:component>

控制器是

({
  handleNo: function (component, event, helper) {
    var urlEvent = $A.get("e.force:navigateToURL");
    urlEvent.setParams({
      url: "/apex/MyOtherVisualforce",
      isredirect: "true"
    });
    urlEvent.fire();
  },

  handleYes: function (component, event, helper) {
    var urlEvent = $A.get("e.force:navigateToURL");
    urlEvent.setParams({
      url: "/apex/MyVisualforce",
      isredirect: "true"
    });
    urlEvent.fire();
  }
});

为了从Opportunity中获取Account Id,首先需要获取正在执行Quick Action的Opportunity Id。这可以通过实现 force:hasRecordId 接口轻松实现(除了您已经实现的 lightningQuickActionWithoutHeader 接口之外)。通过这样做,您可以访问 recordId 属性,在这种情况下,该属性已经包含商机的记录 ID (https://developer.salesforce.com/docs/component-library/bundle/force:hasRecordId/documentation)。

获得Opportunity Id后,您可以使用不同的方法获取相关Account的id。您可以创建 Apex 控制器,但也可以使用 force:recordData 组件 (https://developer.salesforce.com/docs/component-library/bundle/force:recordData/documentation) 来获取帐户 ID。

<aura:component implements="force:lightningQuickActionWithoutHeader,force:hasRecordId">

  <aura:attribute type="Opportunity" name="opportunity" />

  <force:recordData
    recordId="{!v.recordId}"
    fields="AccountId"
    targetFields="{!v.opportunity}"
  />

  Are you sure you want to proceed?
  <div class="slds-align_absolute-center">
    <lightning:button
      label="No"
      variant="destructive"
      onclick="{!handleNo}"
    ></lightning:button>
    <lightning:button label="Yes" onclick="{!c.handleYes}"></lightning:button>
  </div>
</aura:component>

控制器:

({
  handleNo: function (component, event, helper) {
    var accountId = component.get("v.opportunity.AccountId");
    var urlEvent = $A.get("e.force:navigateToURL");
    urlEvent.setParams({
      url: "/apex/MyOtherVisualforce",
      isredirect: "true"
    });
    urlEvent.fire();
  },

  handleYes: function (component, event, helper) {
    var accountId = component.get("v.opportunity.AccountId");
    var urlEvent = $A.get("e.force:navigateToURL");
    urlEvent.setParams({
      url: "/apex/MyVisualforce",
      isredirect: "true"
    });
    urlEvent.fire();
  }
});