salesforcesalesforce-lightninglwc

How to pass selected records i.e, list of records which isn't related to any sObject and Custom Object from LWC to Flow


I'm having a list of records which isn't related to any custom or standard object I need to pass it to Flow. how to pass the list of records from LWC to flow using in js-meta.xml and also help me what kind of flow resource I need to create so that I can bind the selected records to flow resource variable.

This what I have created in js-meta.xml:

<propertyType extends="SObject" name="T" label="Select SObject"/>
<property name="records2" type="{T[]}" label="Records U" />

and in flow I've created 'records2' resource as a collection variable.

I need to pass selected records from LWC to Flow but am stuck how to pass it.


Solution

  • To pass a list of records from LWC to Flow, you can use the following steps:

    1. Create a property in your LWC component that is of type SObject[].In your LWC component, add an event listener for the change event on the property.

    2. In the event listener, call the setFlowVariable method to set the Flow variable with the list of records.

    3. In your Flow, create a collection variable of the same type as the property in your LWC component.

    4. In your Flow, bind the collection variable to the Flow variable that was set by the LWC component.

    Here is an example of how to do this:

    LWC component HTML

        <template>
      <c-my-component records2="{! v.records2 }"></c-my-component>
    </template>
    
    <script>
    import MyComponent from './my-component';
    
    export default {
      components: {
        MyComponent,
      },
      data() {
        return {
          records2: [],
        };
      },
      methods: {
        onRecords2Change(event) {
          this.setFlowVariable('records2', event.target.value);
        },
      },
    };
    </script>
    

    JS-meta.xml

    <aura:component implements="flexipage:availableForAllPageTypes,force:lightningQuickAction">
      <aura:attribute name="records2" type="SObject[]" default="[]" />
    </aura:component>
    

    Flow

    Set Variable Variable Name: records2 Type: SObject[] Value: {! v.records2 }