【问题标题】:Salesforce Aura Component - getting current v.recordId for Flow variableSalesforce Aura 组件 - 获取 Flow 变量的当前 v.recordId
【发布时间】:2022-01-07 08:36:11
【问题描述】:

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

在我之前的question 中,我能够知道如何获取当前的recordID 并将其作为组件控制器中的变量传递给Flow。我的目标是获取页面的当前 recordID,它可能是 Opportunity Account 和 Lead,甚至是 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!"); 
   }
 
})

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

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

我试图更改我的组织上的会话设置,但这并没有帮助我了解它here 但这没有帮助。

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

【问题讨论】:

    标签: javascript components salesforce-lightning salesforce-developer


    【解决方案1】:

    好的,我想通了!

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

    为了在刷新时获得最新的 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 );
            
        
            
        }
      
     
    })
    

    【讨论】:

      猜你喜欢
      • 2018-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-21
      • 2016-02-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多