Trigger Power Automate Flow using JavaScript – Bi-Directional
Hi All,
This blog will be a continuation of my previous blog – Trigger Power Automate Flow using JavaScript – Uni Directional
Now, feedback is essential when sending a request to determine whether it was successfully performed or failed somewhere.
You can accomplish this by forwarding a response back from where the flow was invoked.
I’ll use the same situation as in my previous blog, where I send a notification by greeting a person’s name if exists, or else I will greet a friend.
Check out my previous blog to learn how to build your Flow and JavaScript.
Steps to pass the response back within Flow​
Step 1: Add a response that will be sent back from where the Flow was invoked.

Quick Tip: I am checking if ‘Name’ is present in my dynamic content. If yes, then greet the person else greet a Friend
Formula: if(contains(triggerBody()?[‘DynamicData’], ‘Name’), triggerBody()?[‘DynamicData’][‘Name’], ‘Friend’)
Steps to add into the JavaScript
Step 1: Initially we created JS to trigger the flow, now we will add the code snippet to accept the response from Flow.
Add the following Code:
var TriggerFlow =
{
    Main: () =>
    {
        var formContext = executionContext.getFormContext();
        //Strict Defined Parameters
        var RecordID = formContext.data.entity.getId().replace("{", "").replace("}", "");
        var EntityName = formContext.data.entity.getEntityName();
        //Dynamic Parameter - Sample Data
        var DynamicValues = {Name: "Ethan", Operation: "One Way Power Automate trigger"};
        //Find Plural Logical Name of the Entity and Call Flow
        Xrm.Utility.getEntityMetadata(EntityName, "").then(
            function (result)
            {
                TriggerFlow.CallFlow({logicalEntityName: result.EntitySetName, CurrentRecord: RecordID, DynamicData: DynamicValues});
            },
            function (error)
            {
                console.log(error);
            }
        );
    },
    CallFlow: ({logicalEntityName, CurrentRecord, DynamicData}) =>
    {
        var flowUrl = "https://prod-133.westus.logic.azure.com:443/workflows/796d8796a139409e844e8f1001493567/triggers/manual/paths/invoke?api-version=2016-06-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=XtYrETcQW07SX5EIuxtQDv7lQ8mPNyDpDZ9O7tGmqc8";
        var inputdata = JSON.stringify({
            "EntityName": logicalEntityName,
            "RecordID": CurrentRecord,
            "DynamicData": DynamicData
        });
        var req = new XMLHttpRequest();
        req.open("POST", flowUrl, true);
        req.setRequestHeader('Content-Type', 'application/json');
        req.send(inputdata);
        ////Response
        req.onreadystatechange = function () {
            if (this.readyState === 4) {
                req.onreadystatechange = null;
                if (this.status === 200)
                {
                    var result = this.response;
                    alert("" + result);
                }
                else if(this.status === 400)
                {
                    alert(this.statusText);
                    var result = this.response;
                    alert("Error" + result);
                }
            }
        };  
    }
}
Step 2: Trigger the JS and watch the output I get as Alert (I have used the console page to trigger my JS for example purposes)


Hope this helps in achieving a response from the Power Automate Flow!
