1

I have a action button on account which calls a lightning flow. I am creating a lightning component where I would like to pass the (account) recordId from the flow to this lightning aura component. I am not really sure how to pass this recordId into my component, specifically, in the table I have created in my component. What the lightning component will do is find all the related opportunities to the account and display it where a user can select which opportunities they want to send back to the flow for processing (the rest of the flow takes care of this processing). If anyone has a simpler approach then that would be great too. But basically what this component does is allow user to select which opportunities they want to pass into the lightning flow for processing. This is my component so far:

component:

<aura:component controller="NS_SplitRelatedOpps" implements="flexipage:availableForAllPageTypes,force:hasRecordId">
    <aura:attribute name="ListOfOpportunity" type="Opportunity[]" />
    <aura:attribute name="recordId" type="String"/> //here i define the recordId which matches the variable name of the flow
    <aura:handler name="init" value="{!this}" action="{!c.loadOpportunityList}"/>
    <div class="slds-page-header">
      <p class="slds-page-header__title slds-truncate" title="">Opportunity List You Can Select</p>
      <span class="slds-badge">Selected Opportunity:{!v.selectedCount}</span>
      <div class="slds-grid slds-grid--align-end"> 
         <button class="slds-button slds-button--brand" onclick="{!c.passSelected}">Send Selected Opportunities to Close Lost</button>
      </div>
   </div>
    
    <table class="slds-table slds-table--bordered slds-table--cell-buffer"> //where should I pass the record Id here? so that my controllers can return all related opportunities from account id(which is the recordid)
      <thead>
         <tr class="slds-text-title--caps">
            <th style="width:3.25rem;" class="slds-text-align--right">
               <div class="slds-form-element">
                  <div class="slds-form-element__control">
                     <label class="slds-checkbox">
                        <!--header checkbox for select all-->
                        <ui:inputCheckbox aura:id="box3" change="{!c.selectAll}"/>
                        <span class="slds-checkbox--faux"></span>
                        <span class="slds-form-element__label text"></span>
                     </label>
                  </div>
               </div>
            </th>
            <th>
               <span class="slds-truncate" title="Opportunity Id">Opportunity Id</span>      
            </th>
            <th>
               <span class="slds-truncate" title="Opportunity Name">Opportunity Name</span>
            </th>
         </tr>
      </thead>
      <!--table body start, 
         Iterate opportunity list as a <tr>
         -->
      <tbody>
         <aura:iteration items="{!v.ListOfOpportunity}" var="opp">
            <tr>
               <td scope="row" class="slds-text-align--right" style="width:3.25rem;">
                  <div class="slds-form-element">
                     <div class="slds-form-element__control">
                        <label class="slds-checkbox">
                           <ui:inputCheckbox text="{!opp.Id}" aura:id="boxPack" value="" change="{!c.checkboxSelect}"/>
                           <span class="slds-checkbox--faux"></span>
                           <span class="slds-form-element__label text"></span>
                        </label>
                     </div>
                  </div>
               </td>
               <td scope="row">
                  <div class="slds-truncate" title="{!opp.Id}"><a>{!opp.Id}</a></div>
               </td>
               <td scope="row">
                  <div class="slds-truncate" title="{!opp.Name}">{!opp.Name}</div>
               </td>
            </tr>
         </aura:iteration>
      </tbody>
   </table>
</aura:component>

I have added my JS controllers as well, in case its needed: controller:

({
 loadOpportunityList: function(component, event, helper) {
  // call the helper function for fetch contact from apex class 
  helper.onLoad(component, event);
 },
 
 // For count the selected checkboxes. 
 checkboxSelect: function(component, event, helper) {
  // get the selected checkbox value  
  var selectedRec = event.getSource().get("v.value");
  // get the selectedCount attrbute value(default is 0) for add/less numbers. 
  var getSelectedNumber = component.get("v.selectedCount");
  // check, if selected checkbox value is true then increment getSelectedNumber with 1 
  // else Decrement the getSelectedNumber with 1     
  if (selectedRec == true) {
   getSelectedNumber++;
  } else {
   getSelectedNumber--;
  }
  // set the actual value on selectedCount attribute to show on header part. 
  component.set("v.selectedCount", getSelectedNumber);
 },
 
 // For select all Checkboxes 
 selectAll: function(component, event, helper) {
  //get the header checkbox value  
  var selectedHeaderCheck = event.getSource().get("v.value");
  // get all checkbox on table with "boxPack" aura id (all iterate value have same Id)
  // return the List of all checkboxs element 
  var getAllId = component.find("boxPack");
  // If the local ID is unique[in single record case], find() returns the component. not array   
     if(! Array.isArray(getAllId)){
       if(selectedHeaderCheck == true){ 
          component.find("boxPack").set("v.value", true);
          component.set("v.selectedCount", 1);
       }else{
           component.find("boxPack").set("v.value", false);
           component.set("v.selectedCount", 0);
       }
     }else{
       // check if select all (header checkbox) is true then true all checkboxes on table in a for loop  
       // and set the all selected checkbox length in selectedCount attribute.
       // if value is false then make all checkboxes false in else part with play for loop 
       // and select count as 0 
        if (selectedHeaderCheck == true) {
        for (var i = 0; i < getAllId.length; i++) {
          component.find("boxPack")[i].set("v.value", true);
         component.set("v.selectedCount", getAllId.length);
        }
        } else {
          for (var i = 0; i < getAllId.length; i++) {
            component.find("boxPack")[i].set("v.value", false);
             component.set("v.selectedCount", 0);
        }
       } 
     }  
 
 },
 //For Passing selected records 
 passSelected: function(component, event, helper) {
  // create var for store record id's for selected checkboxes  
  var delId = [];
  // get all checkboxes 
  var getAllId = component.find("boxPack");
  // If the local ID is unique[in single record case], find() returns the component. not array
     if(! Array.isArray(getAllId)){
         if (getAllId.get("v.value") == true) {
           delId.push(getAllId.get("v.text"));
         }
     }else{
     // play a for loop and check every checkbox values 
     // if value is checked(true) then add those Id (store in Text attribute on checkbox) in delId var.
     for (var i = 0; i < getAllId.length; i++) {
       if (getAllId[i].get("v.value") == true) {
         delId.push(getAllId[i].get("v.text"));
       }
      }
     } 
   
     // call the helper function and pass all selected record id's.    
      helper.deleteSelectedHelper(component, event, delId);
        
 },
 
})

Helper.js

({
 onLoad: function(component, event) {
     
    console.log('onLoad call');
  //call apex class method
    var action = component.get('c.fetchOpportunities');
    var accIds = component.get('v.recordId');
     action.setParams({
         accIds: accIds
     });
    action.setCallback(this, function(response) {
   //store state of response
    var state = response.getState();
    if (state === "SUCCESS") {
    //set response value in ListOfContact attribute on component.
        component.set('v.ListOfOpportunity', response.getReturnValue());
    // set deafult count and select all checkbox value to false on load 
        component.set("v.selectedCount", 0);
        component.find("box3").set("v.value", false);
   }
  });
  $A.enqueueAction(action);
 },
 
 deleteSelectedHelper: function(component, event, deleteRecordsIds) {
  //call apex class method
  var action = component.get('c.selectedOpportunities');
  // pass the all selected record's Id's to apex method 
  action.setParams({
   "lstRecordId": deleteRecordsIds
  });
  action.setCallback(this, function(response) {
   //store state of response
   var state = response.getState();
   if (state === "SUCCESS") {
    console.log(state);
    if (response.getReturnValue() != '') {
     // if getting any error while delete the records , then display a alert msg/
     alert('The following error has occurred. while passing the Opp Ids record-->' + response.getReturnValue());
    } else {
     console.log('check it--> passing of Opp Ids successful');
    }
    // call the onLoad function for refresh the List view    
    this.onLoad(component, event);
   }
  });
  $A.enqueueAction(action);
 },
})

Apex controller:

public class NS_SplitRelatedOpps {
    @AuraEnabled
    public static List<Id> fetchOpportunities(List<Id> accIds){
        List<String> oppIds = new List<String>();
        List<Id> opportunityIds = new List<Id>();
        List<Opportunity> oppRecordsToUpdate = new List<Opportunity>();
        List<dfsle__Envelope__c> enRecordsToUpdate = new List<dfsle__Envelope__c>();
        List<dsfs__DocuSign_Status__c> statsToUpdate = new List<dsfs__DocuSign_Status__c>();
        List<dfsle__Envelope__c> envelopes = [Select Id, Related_Opportunities__c, dfsle__DocuSignId__c, dfsle__Expires__c from dfsle__Envelope__c where dfsle__SourceId__c = :accIds];
        for(dfsle__Envelope__c en: envelopes){
            if(!String.isBlank(en.Related_Opportunities__c)){
                oppIds.addAll(en.Related_Opportunities__c.deleteWhitespace().split(','));
                List<Id> oppIdsForEnvelope = new List<Id>((List<Id>)new List<String>( oppIds ));
                opportunityIds.addAll(oppIdsForEnvelope);
            }
            
        }
        return opportunityIds;
    }
    
    @AuraEnabled
    public static List<Opportunity> selectedOpportunities(List<String> lstOppsIds){
        List<Opportunity> lstOppsSelected = [SELECT Id FROM Opportunity WHERE Id IN: lstOppsIds];
        return lstOppsSelected;
    }
}

1 Answer 1

2

Add the attribute you want to pass in the design file of the component. Once the design attribute is added, it appears for selection in the flow.

In flow, you can create a String flow variable (recordId) and pass this in the Record Id input.

If you want to use this attribute value later in the flow, click on Manually assign variables (advanced) and use the same recordId flow variable for output value.

enter image description here

enter image description here

8
  • I have already done that part in the flow. I’m struggling with where to call/write the recodid in the code/component Commented Sep 25, 2020 at 13:05
  • Are you getting the value of recordId in the Lightning component? If yes, then what's the issue? Commented Sep 25, 2020 at 13:10
  • Yes I am seeing it in the flow. As you can see I even called out recordid as an attribute in my component. What I was unsure about was should I be passing this record id somewhere in the table i created so that the JS controller knows to pass the recordid to get related oportunities? Commented Sep 25, 2020 at 13:36
  • You don't need to pass the recordId in the table. RecordId is passed as a parameter accIds from the method onLoad present in helper.js. This will make a call to the Apex controller method and get Opportunities related to the passed recordId. Make sure that you are getting response.getReturnValue() in the onLoad' method and ListOfOpportunity` attribute is populated. You can add debugger in js files for checking these values. Also, make sure the SOQL query in Apex class NS_SplitRelatedOpps is returning Opportunities. Commented Sep 25, 2020 at 13:56
  • thank you so much! I think I was overanalyzing for no reason then. Thanks again for the help Commented Sep 25, 2020 at 14:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .