• Instructions to Ask a Question

    Click on the "Ask a Question" button and select the application for which you would like to ask questions.

    We have 5 different products namely - Pabbly Connect, Pabbly Subscription Billing, Pabbly Email Marketing, Pabbly Form Builder, Pabbly Email Verification.

    The turnaround time is 24 hrs (Business Hours - 10.00 AM to 6.00 PM IST, Except Saturday and Sunday). So your kind patience will be highly appreciated!

    🚀🚀Exclusive Discount Offer

    Just in case you're looking for any ongoing offers on Pabbly, you can check the one-time offers listed below. You just need to pay once and use the application forever -
     

    🔥 Pabbly Connect One Time Plan for $249 (🏆Lifetime Access) -  View offer 

    🔥 Pabbly Subscription Billing One Time Plan for $149 (🏆Lifetime Access) - View offer

  • Important: Any reported problems and issues with your integration will be reported to you and we will encourage the app developers help to resolve those integration issues.

Guide to configure actions using custom JavaScript code

Status
Not open for further replies.
Introduction:
This guide provides detailed instructions for configuring actions within your application using JavaScript code. By following this guide thoroughly, you can easily set up actions using custom JavaScript code.

Steps to Follow:

1. Create an action inside your application that utilizes JavaScript and navigate to "Action Event API Configuration".
  • Example 1: I have created an action to "convert the paragraph to UC (Upper case) and extract sentences out of it."
    1698922190275.png

  • To configure the action event, select the method as POST and enter the following in the API endpoint field: . Ensure you select "Set Body/Query/Path Parameters" and "Request Body (Raw JSON)."
    1698919254596.png

  • Now enter your custom javascript code in JSON (Key-value pair) format.
    JSON:
    {
    "code" : "your code comes here"
    }
  • Here is the sample JavaScript code to convert the paragraph in Uppercase and extract sentences out of it:
  • JavaScript:
    var para = "Code by Pabbly is a freedom to process data in my own way. It allows me to perform multiple tasks in a single step. It supports JavaScript and Python."
    
    var modification1 = para.toUpperCase();
    var modification2 = modification1.replaceAll('. ', '.*');
    var sentences = modification2.split('*');
    
    return sentences;
  • Refer to the provided example in JSON format for better understanding. The JavaScript code provided converts the given sentence to uppercase and separates it.
    JSON:
    {
    "code": "var para = \"{{paragraph}}\";var modification1 = para.toUpperCase();var modification2 = modification1.replaceAll('. ', '.*');var sentences = modification2.split('*');return sentences;"
    }

  • Additionally, You can check your JSON code validation on this website:
    1699015332386.png


  • (NOTE: If your JS code contains double quotes or backward slashes, escape them using a backward slash. For example: If your JS code is "My JS Code," write \"My JS Code\").

  • Now we will enter the same code mentioned above in the "Request Body (Raw JSON) and mention the parameter.
    (NOTE: Parameters are utilized for capturing dynamic values entered by users. The dynamic value is defined by {{}}. For e.g. {{paragraph}})
    1698920367131.png

  • Run this action in your workflow. As you can see, the JavaScript code has been executed successfully.
    1698920661705.png

2. Example 2: Let's consider another scenario where we need to find the date of the upcoming Monday from today.
  • Follow the above-mentioned steps to configure the action using the custom JavaScript API endpoint.
  • Here is the sample JavaScript code to get the date of the upcoming monday:
    JavaScript:
    function getNextMondayDate() {  var today = new Date();
      var day = today.getDay();
      var daysUntilMonday = 1 + (7 - day) % 7;
      var nextMonday = new Date(today.getTime() + daysUntilMonday * 24 * 60 * 60 * 1000);
    
      // Formatting the date as YYYY-MM-DD
      var year = nextMonday.getFullYear();
      var month = String(nextMonday.getMonth() + 1).padStart(2, '0');
      var day = String(nextMonday.getDate()).padStart(2, '0');
    
      var formattedDate = year + '-' + month + '-' + day;
    
      return formattedDate;
    }
    
    var nextMondayDate = getNextMondayDate();
    return  "Next upcoming Monday is on: " + nextMondayDate;
  • Enter the below javascript code in JSON format in the "Request Body (Raw JSON)" section.
    JSON:
    {
        "code": "function getNextMondayDate() {var today = new Date();var day = today.getDay(); var daysUntilMonday = 1 + (7 - day) % 7; var nextMonday = new Date(today.getTime() + daysUntilMonday * 24 * 60 * 60 * 1000); var year = nextMonday.getFullYear(); var month = String(nextMonday.getMonth() + 1).padStart(2, '0'); var day = String(nextMonday.getDate()).padStart(2, '0'); var formattedDate = year + '-' + month + '-' + day; return formattedDate;} var nextMondayDate = getNextMondayDate(); return  \"Next upcoming Monday is on: \" + nextMondayDate;"
    }

  • Additionally, You can check your JSON code validation on this website:
    1699015409219.png


  • (NOTE: Here we do not need parameters as we do not need any dynamic value that needs to be entered by the user).
    1698921189489.png

  • Now view the response in your action step within the workflow.
    1698921326494.png

Successful Setup:
By following the above steps, you can successfully configure your action which contains custom javascript code.


Another Tutorial on JavaScript Actions:
https://forum.pabbly.com/threads/how-we-built-javascript-based-action.17797/#post-84563
 
Last edited by a moderator:
Status
Not open for further replies.
Top