简体   繁体   English

salesforce 在 Lightning 组件中获取帐户 ID

[英]salesforce get account id in lightning component

I have developed a lightning component modal popup to show on the opportunity page.我开发了一个闪电组件模态弹出窗口来显示在机会页面上。 There are two options Yes and No. On condition this lightning component is transferring the flow to one visualforce page or the other with the account id.有两个选项是和否。条件是此闪电组件将流传输到一个visualforce 页面或另一个具有帐户ID 的页面。 How I can get the account id in the lightning component.我如何在闪电组件中获取帐户 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>

and the controller is控制器是

({
  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();
  }
});

In order to obtain the Account Id from the Opportunity, first you need to get the Opportunity Id where the Quick Action is being executed.为了从机会中获取帐户 ID,首先您需要获取正在执行快速操作的机会 ID。 That can be easily achieved by implementing the force:hasRecordId interface (in addition to the lightningQuickActionWithoutHeader one that you are already implementing).这可以通过实现 force:hasRecordId 接口轻松实现(除了您已经实现的 LightningQuickActionWithoutHeader 接口)。 By doing this, you get access to recordId attribute which already contains the record id of the Opportunity in this case ( https://developer.salesforce.com/docs/component-library/bundle/force:hasRecordId/documentation ).通过这样做,您可以访问在这种情况下已经包含机会的记录 ID 的 recordId 属性 ( https://developer.salesforce.com/docs/component-library/bundle/force:hasRecordId/documentation )。

Once you get the Opportunity Id, you can use different methods to obtain the related Account's id.获得机会 ID 后,您可以使用不同的方法来获取相关帐户的 ID。 You can create an Apex Controller but you can also use the force:recordData component ( https://developer.salesforce.com/docs/component-library/bundle/force:recordData/documentation ) to obtain the 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>

Controller:控制器:

({
  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();
  }
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM