web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Check if the flow was ...
Power Apps
Unanswered

Check if the flow was successfully triggered from Java Script on Custom Button of Model Driven App

(0) ShareShare
ReportReport
Posted on by 1,115

I have a Java Script that is being executed at the click of a custom button on entity Main grid page. I am running a Power Automate flow with the trigger "When an HTTP Request is received". The flow is running fine on click of my custom button. I am attaching the code below. I want to show a dialog box with a message for the user whether the flow was triggered or failed to trigger. How do I get the response like Status code 200 or any other means to check if the flow was successfully called or if the HTTP Request was successfully made? 

 

RameshMukka_0-1705769483220.png

Below is the code

function ExportWithLogos(gridContext){
 var grid = gridContext.getGrid();
 //console.log(gridContext);
 var fetchXMLStr = gridContext.getFetchXml();
 console.log(fetchXMLStr);
 var fetchXMLWithEscapedQuotes = fetchXMLStr.replace(/"/, '\\"');
 console.log(fetchXMLWithEscapedQuotes);
 var raw = JSON.stringify({"FetchXmlQuery": fetchXMLStr});
 var myHeaders = new Headers();
 myHeaders.append("Content-Type","application/json")
 var requestOptions = {
 method: 'POST',
 headers: myHeaders,
 body: raw
 };
 console.log(requestOptions);

 Xrm.WebApi.retrieveMultipleRecords("environmentvariabledefinition", "?$select=defaultvalue&" + "$filter=schemaname eq 'tms_HTTPURLforExport'").then(
 function success(results) {

 console.log(results);
 for (var i=0; i <results.entities.length;i++) {

 var result = results.entities[i];

 var flowURL = result["defaultvalue"];
 console.log('This is flowURL ',flowURL);
 fetch(flowURL, requestOptions)
 .then(response => response.text())
 .then(result => console.log(result))
 .catch(error => console.log('error', error));

 }
 },
 function (error)
 {
 console.log(error.message);
 }
)
}
 

Thanks and Regards,

Ramesh Mukka

I have the same question (0)
  • Ethan_009 Profile Picture
    4,838 Moderator on at

    Hi @RameshMukka ,

     

    You can follow this blog, describes how to trigger flow from JS and get response back from Power Automate Flow in Model Driven Apps.

    Blog Link

     

    Hope this helps

  • Neeraj523 Profile Picture
    82 on at

    you can simply show a notification to the user from success function about flow execution. Or you need something different.

  • rameshmukka Profile Picture
    1,115 on at

    @Ethan_R 

    Thanks for the blog, I noticed that the XMLHttpRequest() was used to trigger the flow. In my case, I am using the fetch() function to trigger the flow. How do I get the response in this case? Please bare with me am pretty much a noob in javascript. 

     

    Thanks,

    Ramesh

  • rameshmukka Profile Picture
    1,115 on at

    @NeerajGargi I have updated the code like below to show an Alert dialog on success. But doesn't show up. I am sure, something is wrong here.

     

    function ExportWithLogos(gridContext){
     var grid = gridContext.getGrid();
     //console.log(gridContext);
     var fetchXMLStr = gridContext.getFetchXml();
     console.log(fetchXMLStr);
     var fetchXMLWithEscapedQuotes = fetchXMLStr.replace(/"/, '\\"');
     console.log(fetchXMLWithEscapedQuotes);
     var raw = JSON.stringify({"FetchXmlQuery": fetchXMLStr});
     var myHeaders = new Headers();
     myHeaders.append("Content-Type","application/json")
     var requestOptions = {
     method: 'POST',
     headers: myHeaders,
     body: raw
     };
     console.log(requestOptions);
    
     Xrm.WebApi.retrieveMultipleRecords("environmentvariabledefinition", "?$select=defaultvalue&" + "$filter=schemaname eq 'tms_HTTPURLforExport'").then(
     function success(results) {
    
     console.log(results);
     for (var i=0; i <results.entities.length;i++) {
    
     var result = results.entities[i];
    
     var flowURL = result["defaultvalue"];
     console.log('This is flowURL ',flowURL);
     fetch(flowURL, requestOptions)
     .then(response => response.text())
     .then(result => console.log(result))
     .catch(error => console.log('error', error));
    
     };
     },
     function(success)
     {
    
     Xrm.Navigation.openAlertDialog({ confirmButtonLabel: "OK", text: "You will receive the exported trademarks in an Excel file shortly.", title: "Export Confirmation" }).then(
     function (success) {
     console.log("Alert dialog closed");
     },
     function (error) {
     console.log(error.message);
     }
     )
     },
     function (error)
     {
     console.log(error.message);
     }
    )
    }
     
  • ivan_apps Profile Picture
    2,187 Moderator on at

    fetch() is an ES6 promise so if you’re not comfortable with JavaScript or the concept of await promises, I would recommend you implement an XMLHttpRequest instead. There’s more examples for how to execute those requests while you’re learning.

     

    If you want to continue with fetch(), you’ll probably want to not shorthand the function for response or result and put in a function with a debug statement. Step through your code until you find the response HTTP number so you know if it executed or not.

     

    caveat: I’m not sure es6 is supported in all browsers or by model-driven apps. Might be worth ensuring es6 can run on your target browsers.

  • Fubar Profile Picture
    8,338 Super User 2025 Season 2 on at

    the fetch part would be structured something like the following

    fetch(flowurl, {method: "POST",
    		headers: {
    			"OData-MaxVersion": "4.0",
    			"OData-Version": "4.0",
    			"Accept": "application/json",						 
    			"Content-Type": "application/json; charset=utf-8", 					
    		},
    		body:	JSON.stringify( { key: "value" } )
    	})
    	.then(function(res){					
    		return res.json(); 					
    	})
    	.then(function(data){ 					
    		if(data.error !== undefined){
    			// an error occured
    			if(data.error.code === "ResponseTimeout"){
    				// Javascript did not get a response from flow
    				// note: Flow could stil be successfully running just the 
    				// Javascript has gien up waiting for a response
    			}else{
    			 // unexpected error occurred	
     // can get error code etc from the data.error object			
    			}
    		}else{
    			// success, so process data results
    												 
     }
    					
    		})
    		.catch(error => {
    			// An error occured when trying to make call)						
    		});

     

    I usually start a client side .showProgressIndicator, before calling the fetch, and stop it inside the error and success places.  

    Because it uses JS Promises, you can have fun getting objects in scope e.g. inside the fetch the Xrm object is out of scope, and so if needing various objects you may need to work around it by creating other functions or variables with more scope than what you may usually.

     

    Also if you are not returning a properly formatted JSON object in your HTTP Response node in your Flow res.json() will error (even though the Flow ran successfully)

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 796 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 327 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard