Salesforce Aura 组件 - 获取 Flow 变量的当前 v.recordId

Salesforce Aura Component - getting current v.recordId for Flow variable

我对 Aura Component 还很陌生,Javascript 所以如果您觉得我的措辞不准确,我深表歉意。

在我之前的 question 中,我能够知道如何获取当前的 recordID 并将其作为组件控制器中的变量传递给 Flow。我的目标是获取页面的当前 recordID,它可以是机会帐户和潜在客户,甚至可以将其设置为 null,我的流程可以处理所有 ID。

这是我的代码:

组件:

    <aura:component implements="flexipage:availableForAllPageTypes,force:hasRecordId,lightning:isUrlAddressable" access="global" >
    <aura:handler name="change" value="{!v.pageReference}" action="{!c.reInit}" event="force:refreshView" />
     <aura:handler name="change" value="{!v.recordId}" action="{!c.onRecordIdChange}"/>
     <lightning:utilityBarAPI aura:id="utilitybar" />
    <br/>
      Account Id is {!v.recordId} // This updating properly everytime I switch from home screen to recordID its updating real time.
    <lightning:formattedText value="{!v.recordId}" aura:id="ThisRecord" />
    <lightning:flow aura:id="flowData" onstatuschange="{!c.minimizeUtility}" />

</aura:component>

JS组件控制器:

   ({
   minimizeUtility : function(component, event, helper) {
        if(event.getParam("status") === "FINISHED") {
            
        var utilityAPI = component.find("utilitybar");
        utilityAPI.minimizeUtility();
        }
    },
    
    onRecordIdChange : function(component, event, helper) {
        
       $A.get("e.force:refreshView").fire();
        
        var newRecordId = component.get("v.recordId");
        console.log(newRecordId);
          // Find the component whose aura:id is "flowData"
        var flow = component.find("flowData");
      
      const recordId = component.find("ThisRecord").get("v.value"); const inputs = []; if(recordId) inputs.push({name:"recordId",type:"String",value:recordId});

        // In that component, start your flow. Reference the flow's API Name.
        flow.startFlow("MyFlowHere",
                     inputs );
        
    
        
    },
    
       reInit : function(component, event, helper) {
       alert("hello there!"); 
    },
    
   
   getInput : function(cmp, event) { 
      alert("hello there!"); 
   }
 
})

现在我已经设置了组件,它正在按预期发送当前的 recordID,但我遇到了一个小问题。你看看我是否在主屏幕上并切换到一个帐户 v.RecordID 没有得到更新。即使它设置为每次 v.RecordID 更改时重新启动流程,但事实并非如此,所以当主屏幕有 v.Record = null 时,即使我跳入帐户页面它也不会更新正在传递的 ID 的 recordID 不正确。

每次页面更改或 v.RecordID 更改以重新呈现 const recordId = component.find("ThisRecord").get("v.value"); 时,我需要一种刷新或重新启动流功能的方法,以便它始终获取更新的 ID。每次切换记录时,我都可以看到 aura 组件文本正在更新,但是获取 ID 的函数不会在那里刷新,因为总是有旧 ID。

我曾尝试更改我组织的会话设置,但这对我阅读相关内容没有帮助 here 但这没有帮助。

如果您知道任何帮助我的方法,请告诉我!谢谢!

好的,我想通了!

我发布这篇文章是为了帮助任何需要更简单解决方案的人。我看到了一些类似的问题,答案很长而且不清楚。

为了在刷新时获得最新的 RecordId,您将需要重新启动流程,但没有默认的方法来执行此操作,因此我有一个处理程序可以在记录更改时触发操作。它只是销毁了旧的 Flow 组件并从头开始动态创建一个并再次调用该流。

组件:

<aura:component implements="flexipage:availableForAllPageTypes,force:hasRecordId" access="global" >
<aura:handler name="change" value="{!v.recordId}" action="{!c.onRecordIdChange}"/> // This will fire function OnRecordIdChange
  Account Id is {!v.recordId}  . 
<lightning:flow aura:id="flowData" />
      {!v.body}
</aura:component>

控制器 JS:

  ({
 
    onRecordIdChange : function(component, event, helper) {
           
   // We are going to destroy our Flow Element and dynamically re-create it every time the recordID changes. It goes it checks if there is a flow with same name, Destroy it and dynamically re-create it, once you are re-calling the flow it will take the updated ID's
        
               var flowfinal = component.find("flowData");

        if ($A.util.isUndefinedOrNull(flowfinal)) {
           
        }
        else
        {
             flowfinal.destroy();
        }
        $A.createComponent(
            "lightning:flow",
            {
                "aura:id": "flowData"
            },
            function(NewFlow, status, errorMessage){
                //Add the new button to the body array
                if (status === "SUCCESS") {
                    var body = component.get("v.body");
                    body.push(NewFlow);
                    component.set("v.body", body);
                     console.log(NewFlow)
                }
                else if (status === "INCOMPLETE") {
                    console.log("No response from server or client is offline.")
                    // Show offline error
                }
                else if (status === "ERROR") {
                    console.log("Error: " + errorMessage);
                    // Show error message
                }
            }
        );
        
        
        
          // Find the component whose aura:id is "flowData"
        var flow = component.find("flowData");
      
     var recordId = component.get("v.recordId");
        const inputs = [];
        console.log(recordId);
        if(recordId) inputs.push({name:"recordId",type:"String",value:recordId});
        console.log("In puts here");
         console.log(recordId);
        // In that component, start your flow. Reference the flow's API Name.
        flow.startFlow("One_Click_Request_Flow_1",
                     inputs );
        
    
        
    }
  
 
})