function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nehashri320Nehashri320 

Calling flow from Lightning component and passing variable

Hi,
I have written a lightning component and calling a flow from this. It works fine except there is one problem. I am able to pass the record id as a flow variable since that is the flow input variable but there is one more field inside the flow and the flow has a decision node based on that field. If i pass only the Id, the other field inside the flow doenst seem to get the field value even though the field from the same object.
If this is the case how can i pass other field value to the flow along with the id ?
Below are my component and controller: 
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,force:lightningQuickAction" access="global" >
    <aura:attribute name="claim" type="Claim__c"/>
    <aura:attribute name="claimId" type="Id"/>
    <force:recordData aura:id="claimRecordLoader" recordId="{!v.recordId}" fields="Submit_to_TPA__c"
                      targetRecord="{!v.claim}" />
    <aura:handler name="init" value="{!this}" action="{!c.init}" />
    <lightning:flow aura:id="flowData"/>
</aura:component>
 
({
    init : function (component) {
        // Find the component whose aura:id is "flowData"
        var flow = component.find("flowData");
        var inputVariables = [
            {
                name : "ClaimIdVar",
                type : "SObject",
                value : { "Id" : component.get("v.recordId")}
                //value : component.get("v.recordId")
            }
        ];
        // In that component, start your flow. Reference the flow's Unique Name.
        flow.startFlow("SubmitToTPA", inputVariables );
    },
    
})



 
Best Answer chosen by Nehashri320
sfdcMonkey.comsfdcMonkey.com
hi Nehashri320 ,

for do this, you have need to query the record with there fields using currentrecordId in doInit function: 

for reference : 
  create a apex controller :
 
public class flowCtrl{
  @AuraEnabled 
    public static objectApiName__c fetchRecord(string recId){
        return [select id,Name,yourFieldApiName__c from objectApiName__c  WHERE id =: recId] ;
    }
}

update in js controller :
({
    init : function (component) {
         var action = component.get("c.fetchRecord");
          action.setParams({
            'recId': component.get("v.recordId")
          });
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                var storeResponse = response.getReturnValue();
           // Find the component whose aura:id is "flowData"
           var flow = component.find("flowData");
           var inputVariables = [
             {
                name : "ClaimIdVar",
                type : "SObject",
                value : { 
				       "Id" : component.get("v.recordId"),
					   "otherVarFieldName__c" : storeResponse.yourFieldApiName__c
					   }
                //value : component.get("v.recordId")
              }
           ];
           // In that component, start your flow. Reference the flow's Unique Name.
           flow.startFlow("SubmitToTPA", inputVariables );
		
            }
        });
		
    $A.enqueueAction(action);
    },
    
})

update in component : add controller to component

<aura:component controller="flowCtrl" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,force:lightningQuickAction" access="global" >


   Let us know if it helps you, and kindly mark it best answer it this answer helps you so it make proper solution for others in future 
thanks




 

All Answers

sfdcMonkey.comsfdcMonkey.com
hi Nehashri320 ,

for do this, you have need to query the record with there fields using currentrecordId in doInit function: 

for reference : 
  create a apex controller :
 
public class flowCtrl{
  @AuraEnabled 
    public static objectApiName__c fetchRecord(string recId){
        return [select id,Name,yourFieldApiName__c from objectApiName__c  WHERE id =: recId] ;
    }
}

update in js controller :
({
    init : function (component) {
         var action = component.get("c.fetchRecord");
          action.setParams({
            'recId': component.get("v.recordId")
          });
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                var storeResponse = response.getReturnValue();
           // Find the component whose aura:id is "flowData"
           var flow = component.find("flowData");
           var inputVariables = [
             {
                name : "ClaimIdVar",
                type : "SObject",
                value : { 
				       "Id" : component.get("v.recordId"),
					   "otherVarFieldName__c" : storeResponse.yourFieldApiName__c
					   }
                //value : component.get("v.recordId")
              }
           ];
           // In that component, start your flow. Reference the flow's Unique Name.
           flow.startFlow("SubmitToTPA", inputVariables );
		
            }
        });
		
    $A.enqueueAction(action);
    },
    
})

update in component : add controller to component

<aura:component controller="flowCtrl" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,force:lightningQuickAction" access="global" >


   Let us know if it helps you, and kindly mark it best answer it this answer helps you so it make proper solution for others in future 
thanks




 
This was selected as the best answer
Nehashri320Nehashri320
Thank you for your reply. Will definetly be helpful to me. I have solved the issue. Actually, I wasn’t testing correctly. But out of curiosity, Can I use lightning data service to do this instead of Apex class?
sfdcMonkey.comsfdcMonkey.com
yes LDS also a good option for load value without apex, but LDS have own limitations right now so i recommend above method. it's easy and stable and we have create sample post on LDS you can take a reference from there : 
http://www.sfdcmonkey.com/2017/07/31/lightning-data-service/

Thanks, if it helps you, kindly close your query with choosing best answer so it will remove from unanswered queue :)
http://sfdcmonkey.com
Nehashri320Nehashri320
Thank you @piyush_soni
Nehashri320Nehashri320
I would like to add one more question here or Shall I start a new thread? I followed your steps and it works great except for when some field value is null. In the name, type, value pair If a field value is null, it throws an error. How can I check for this? If it is null. Thank you in advance! [cid:image001.png@01D35EF2.B34F1F80]
Sunil Dani 4Sunil Dani 4
Just adding Code Snippet in case you have multiple variables to send to flow

 var inputVariables = [{name:'caseIntId', type:'String', value:component.get("v.caseInteractionId")}, 
                                          {name:'userId', type:'String', value:component.get("v.userId")}];
                    flow.startFlow(flowId, inputVariables);     
 
Jen BryantJen Bryant
Thanks Sunil!