Is There a Way to Skip a Workflow Step if a Condition Isn't Met While Adding PDF Pages to a Google Doc?

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

Hey is there any way to skip a step if a condition isn’t met? I don’t want to stop the workflow. I want it to keep going if the condition isn’t met.

ie: I’m creating a PDF earlier in the workflow. Sometimes it’s 2 pages, sometimes its 3.
Later in the workflow, I’m adding those PDF pages to a Google Doc. Each instance of adding a PDF to the doc has its own step. But I’m getting an error if the PDF created only has two pages, but I have 3 steps.

So is there a way to check whether that PDF URL is present, and if it isn’t, just skip that step? (Hope that made sense)

Yes, you can conditionally skip a step in a Pipedream workflow by using a code step to check whether the PDF URL is present and then using $.flow.exit() to exit the step early if the condition isn’t met. This way, the workflow will continue to the next step without stopping. Here’s an example of how you can achieve this: 1. Check for the PDF URL in a code step: ```
export default defineComponent({ async run({ steps, $ }) { const pdfUrl = steps.previous_step_name.pdfUrl; // Replace with the actual step name and variable if (!pdfUrl) { // If the PDF URL is not present, exit the step return $.flow.exit(“PDF URL not found, skipping this step.”); } // If the PDF URL is present, continue with the rest of the code // Add your logic to handle the PDF URL here }, });

That didn’t work - it stopped the whole workflow the first time a PDF wasn’t present

I’m sorry to hear that you’re having trouble with your workflow. It sounds like the absence of a PDF is causing the entire workflow to stop. To handle this situation more gracefully, you can add a conditional check to see if the PDF is present before proceeding with the rest of the workflow. Here’s an example of how you can modify your workflow to check for the presence of a PDF and handle the situation accordingly: export default defineComponent({ async run({ steps, $ }) { const pdf = steps.trigger.event.pdf; // Adjust this to the correct path to your PDF if (!pdf) { // If the PDF is not present, exit the workflow gracefully $.flow.exit('PDF not present, exiting workflow.'); } // Continue with the rest of your workflow if the PDF is present // Your existing code here } }); In this example, the workflow checks if the pdf variable is present. If it’s not, the workflow exits gracefully with a message. If the PDF is present, the workflow continues with the rest of your code. If you need further assistance, please visit Support - Pipedream. Would you like more specific help with your workflow? If so, could you provide more details about how your workflow is structured and where the PDF is expected to be found?

Can you help me fix this script?

import { axios } from "@pipedream/platform";

export default defineComponent({
  props: {
    googleDocs: {
      type: "app",
      app: "google_docs",
    },
  },
  async run({ steps, $ }) {
    // Check if the condition is met
    const condition = steps.matyx_report_1_filter.$return_value;

    // Log the condition for debugging
    console.log(`Condition value: ${condition}`);

    if (condition.toLowerCase() !== "true") {
      // If the condition is not met, exit the step
      return $.flow.exit("Condition not met, skipping this step.");
    }

    // Get the PDF URL
    const pdfUrl = steps.pdf_to_anything_converter_Matyx.$return_value.urls[0];

    // Log the PDF URL for debugging
    console.log(`PDF URL: ${pdfUrl}`);

    // Check if the PDF URL exists
    if (!pdfUrl) {
      return $.flow.exit("PDF URL not found, skipping this step.");
    }

    // Get the Google Doc ID
    const documentId = steps.Create_Report.$return_value.documentId;

    // Log the document ID for debugging
    console.log(`Document ID: ${documentId}`);

    // Define the Google Docs API endpoint
    const endpoint = `https://docs.googleapis.com/v1/documents/${documentId}:batchUpdate`;

    // Define the request body for appending the image
    const requestBody = {
      requests: [
        {
          insertInlineImage: {
            uri: pdfUrl, // Use the original URL without encoding
            location: {
              index: 1, // Change this index to the correct location in the document
            },
            objectSize: {
              height: {
                magnitude: 200,
                unit: 'PT'
              },
              width: {
                magnitude: 200,
                unit: 'PT'
              }
            }
          },
        },
      ],
    };

    // Log the request body for debugging
    console.log(`Request body: ${JSON.stringify(requestBody)}`);

    try {
      // Make the API request to append the image
      const response = await axios(this, {
        method: "POST",
        url: endpoint,
        headers: {
          Authorization: `Bearer ${this.googleDocs.$auth.oauth_access_token}`, // Use the OAuth token from the connected account
          "Content-Type": "application/json",
        },
        data: requestBody,
      });

      // Log the response for debugging
      console.log(`Response: ${JSON.stringify(response.data)}`);

      // Check the response
      if (response.status === 200) {
        return "Image appended successfully.";
      } else {
        throw new Error(`Failed to append image: ${response.statusText}`);
      }
    } catch (error) {
      // Log full error details
      console.error(`Error appending image: ${error.message}`);
      if (error.response) {
        console.error(`Error details: ${JSON.stringify(error.response.data)}`);
      }
      throw error;
    }
  },
});

Sure, I can help you fix the script. Here are a few issues and improvements: 1. The endpoint URL should not be enclosed in angle brackets (< >). 2. The axios call should use $ as the first argument, not this. 3. The googleDocs prop should be accessed as this.google_docs (with an underscore) to match the prop definition. 4. Ensure the Content-Type header is set correctly. 5. The response data should be accessed directly, not through response.data. Here is the corrected script: ``` import { axios } from “@pipedream/platform”; export default defineComponent({ props: { google_docs: { type: “app”, app: “google_docs”, }, }, async run({ steps, $ }) { // Check if the condition is met const condition = steps.matyx_report_1_filter.$return_value; // Log the condition for debugging console.log(Condition value: ${condition}); if (condition.toLowerCase() !== “true”) { // If the condition is not met, exit the step return $.flow.exit(“Condition not met, skipping this step.”); } // Get the PDF URL const pdfUrl = steps.pdf_to_anything_converter_Matyx.$return_value.urls[0]; // Log the PDF URL for debugging console.log(PDF URL: ${pdfUrl}); // Check if the PDF URL exists if (!pdfUrl) { return $.flow.exit(“PDF URL not found, skipping this step.”); } // Get the Google Doc ID const documentId = steps.Create_Report.$return_value.documentId; // Log the document ID for debugging console.log(Document ID: ${documentId}); // Define the Google Docs API endpoint const endpoint = https://docs.googleapis.com/v1/documents/${documentId}:batchUpdate; // Define the request body for appending the image const requestBody = { requests: [ { insertInlineImage: { uri: pdfUrl, // Use the original URL without encoding location: { index: 1, // Change this index to the correct location in the document }, objectSize: { height: { magnitude: 200, unit: 'PT

I got these errors:

6/3/2024, 8:51:41 PM

Condition value: True

6/3/2024, 8:51:41 PM

PDF URL: https://pdf-temp-files.s3.us-west-2.amazonaws.com/EXUTWI20CQZGU3Y7ZXNHLVKSJUGYUZX5/htmltopdf.jpg?X-Amz-Expires=3600&X-Amz-Security-Token=FwoGZXIvYXdzECQaDMhDTuIWA7few%2FdR2yKCATuk8uh64MXbgiqTJ%2FJheKnpUHC8z4j%2Bux9kAQcX5TC8bDA8JBkrRNUys%2Fpk5cNLyKiRsb1TszSNshM%2Bw3xBXzA%2Bqrbch3UuIaz%2BKSYSwxqXpchnae49fLIo5zdblJr%2Bz3DgcofwQjpKC%2BK1LFsD4eSJWiVo1Q9nfvG1i50nJLzZNO8ohfL5sgYyKNk%2BwxHmDQXleNPPbKeOHG1i79ZBx71ezLC5Q0YUyM0v2lergnrQdgk%3D&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=ASIA4NRRSZPHPFWAITR3/20240604/us-west-2/s3/aws4_request&X-Amz-Date=20240604T022812Z&X-Amz-SignedHeaders=host&X-Amz-Signature=6e5a41da69ee8e1de428b160c5a5362016405ea6030b0c72c2908ed61448dd38

6/3/2024, 8:51:41 PM

Document ID: 1JMtjcYz2mKd_v1srpZSyqpGSinMRnVPRWAvJrS9kdz0
6/3/2024, 8:51:41 PM

Request body: {“requests”:[{“insertInlineImage”:{“uri”:”
https://pdf-temp-files.s3.us-west-2.amazonaws.com/EXUTWI20CQZGU3Y7ZXNHLVKSJUGYUZX5/htmltopdf.jpg?X-Amz-Expires=3600&X-Amz-Security-Token=FwoGZXIvYXdzECQaDMhDTuIWA7few%2FdR2yKCATuk8uh64MXbgiqTJ%2FJheKnpUHC8z4j%2Bux9kAQcX5TC8bDA8JBkrRNUys%2Fpk5cNLyKiRsb1TszSNshM%2Bw3xBXzA%2Bqrbch3UuIaz%2BKSYSwxqXpchnae49fLIo5zdblJr%2Bz3DgcofwQjpKC%2BK1LFsD4eSJWiVo1Q9nfvG1i50nJLzZNO8ohfL5sgYyKNk%2BwxHmDQXleNPPbKeOHG1i79ZBx71ezLC5Q0YUyM0v2lergnrQdgk%3D&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=ASIA4NRRSZPHPFWAITR3/20240604/us-west-2/s3/aws4_request&X-Amz-Date=20240604T022812Z&X-Amz-SignedHeaders=host&X-Amz-Signature=6e5a41da69ee8e1de428b160c5a5362016405ea6030b0c72c2908ed61448dd38”,“location”:{“index”:1},“objectSize”:{“height”:{“magnitude”:200,“unit”:“PT”},“width”:{“magnitude”:200,“unit”:“PT”}}}}]}

6/3/2024, 8:51:43 PM

Response: {“replies”:[{“insertInlineImage”:{“objectId”:“kix.dp3n4hhhvks1”}}],“writeControl”:{“requiredRevisionId”:“ALBJ4Ls1-22_BBZSy1zjDzscl42SeZZh95L5_HpB8GX6NKEEg5z5wtvUWPgrYz7z47NT4EkAQymvG4DNGWOWHg”},“documentId”:“1JMtjcYz2mKd_v1srpZSyqpGSinMRnVPRWAvJrS9kdz0"}
6/3/2024, 8:51:43 PM

Error appending image: Failed to append image: undefined

To help you troubleshoot the error you’re encountering, I need a bit more information about your workflow and the specific error message you’re seeing. Here are a few questions that will help me understand the issue better: 1. Can you provide the exact error message or stack trace that you’re seeing? 2. Are you using a specific Pipedream component or custom code to insert the image into a document? 3. Could you share the relevant portion of your workflow code or configuration? 4. Are there any specific conditions or steps that lead to this error? With this information, I can provide more targeted assistance.