How to Amend Code to Exclude InvoiceIDs Stored in Exceptions Data Store?

This topic was automatically generated from Slack. You can find the original thread here.

can you amend the following code so it reads an array of InvoiceIDs from a data store called Exceptions and only passed records on that do not contain those records that match?

import { axios } from '@pipedream/platform';
// To use previous step data, pass the `steps` object to the run() function
export default defineComponent({
  props: {
    records: {
      type: "any",
      label: "Records to loop",
      description: "The array of records to send to processing workflow",
    },
    workflow_url: {
      type: "string",
      label: "Processing workflow URL",
      description: "The HTTP endpoint to connect to that's processing single individual records from this workflow"
    }
  },
  async run({ steps, $ }) {
    for await(const record of this.records) {
      await axios($, {
        url: this.workflow_url,
        method: 'POST',
        data: record
      })
    }
  },
})

To filter out records with InvoiceIDs that match the ones in the Exceptions data store, you can use the this.$db.get() method to retrieve the Exceptions data and then filter the records accordingly. Here’s the updated code: import { axios } from "@pipedream/platform"; export default defineComponent({ props: { records: { type: "any", label: "Records to loop", description: "The array of records to send to processing workflow", }, workflow_url: { type: "string", label: "Processing workflow URL", description: "The HTTP endpoint to connect to that's processing single individual records from this workflow", }, }, async run({ steps, $ }) { // Retrieve the Exceptions data from the data store const exceptions = await this.$db.get("Exceptions"); // Filter out records with InvoiceIDs that match the ones in Exceptions const filteredRecords = this.records.filter( (record) => !exceptions.includes(record.InvoiceID) ); // Process the filtered records for await (const record of filteredRecords) { await axios($, { url: this.workflow_url, method: "POST", data: record, }); } }, }); This code retrieves the Exceptions data from the data store and filters out the records with matching InvoiceIDs before processing them. Make sure to replace record.InvoiceID with the correct property name for the InvoiceID in your records.

Can you amend so that the datastore is provided within the props?

Certainly! Here’s the updated code with the Exceptions data store provided within the props: import { axios } from "@pipedream/platform"; export default defineComponent({ props: { records: { type: "any", label: "Records to loop", description: "The array of records to send to processing workflow", }, workflow_url: { type: "string", label: "Processing workflow URL", description: "The HTTP endpoint to connect to that's processing single individual records from this workflow", }, exceptions: { type: "data_store", label: "Exceptions Data Store", description: "The data store containing the array of InvoiceIDs to be excluded", }, }, async run({ steps, $ }) { // Retrieve the Exceptions data from the data store const exceptions = await this.exceptions.get("InvoiceIDs"); // Filter out records with InvoiceIDs that match the ones in Exceptions const filteredRecords = this.records.filter( (record) => !exceptions.includes(record.InvoiceID) ); // Process the filtered records for await (const record of filteredRecords) { await axios($, { url: this.workflow_url, method: "POST", data: record, }); } }, }); This code adds the exceptions prop as a data store and retrieves the InvoiceIDs from it. Make sure to replace record.InvoiceID with the correct property name for the InvoiceID in your records.

can we also print the exceptions to the log?

Sure, you can use console.log() to print the exceptions to the log. Here’s the updated code with the added log statement: ```
import { axios } from “@pipedream/platform”; export default defineComponent({ props: { records: { type: “any”, label: “Records to loop”, description: “The array of records to send to processing workflow”, }, workflow_url: { type: “string”, label: “Processing workflow URL”, description: “The HTTP endpoint to connect to that’s processing single individual records from this workflow”, }, exceptions: { type: “data_store”, label: “Exceptions Data Store”, description: “The data store containing the array of InvoiceIDs to be excluded”, }, }, async run({ steps, $ }) { // Retrieve the Exceptions data from the data store const exceptions = await this.exceptions.get(“InvoiceIDs”); // Print the exceptions to the log console.log(“Exceptions:”, exceptions); // Filter out records with InvoiceIDs that match the ones in Exceptions const filteredRecords = this.records.filter( (record) => !exceptions.includes(record.InvoiceID) ); // Process the filtered records for await (const record of filteredRecords) { await axios($, { url: this.workflow_url, method: “POST”, data: record, }); } }, });