Follow my blog for more interesting topics on Dynamics 365, Portals and Power Platform. For training and consulting, write to us at info@xrmforyou.com
Flows are the new path on which every Dynamics 365 consultant is treading now and while you move along, you discover many new stuffs on your journey.
And one of them is when customer walked up to me and asked – “Flows are not realtime and they need a trigger. Can I call them through some means on demand and get the results of the flow back?”.
Well, I never did this since this requirement never popped up but I was aware that it can be done and hence said “Yes” and went back to my holy grail – “Visual studio”. Can’t put the exact requirement here but for this blog I create a simple flow and the same concepts can be applied no matter how much complex your flow is.
So here is the requirement for this blog – When an account is created, I call a flow with the created account GUID and also pass in the number of child contacts to be created. The flow takes those parameters in and creates as many contacts as defined by the input parameter to the flow and returns the created contact guid’s to the invoker.
Please understand that the purpose of this blog is not to show what u can do with flow and when you should use them or when not, but to call them programmatically on demand.
So I started creating the flow. Before I proceed let’s understand the requirement.
My flow will be invoked programmatically with some input parameters and then the flow will execute all its defined paths and send the response back to the invoker.
So basically the flow will receive a HttpRequest call with body provided by the invoker and that will be my first flow trigger. So I started.
Once you select the Request trigger, you should be seeing the below screenshot.
As you can see, if you wish to pass some data to the Request, you need to define the Request Body JSON schema so that flow understands your input parameters and their data types.
This is very easy now. Just paste your JSON body and use the option – “Use sample payload to generate JSON schema” as highlighted above.
For this example, I would be passing two parameters in the body – the account id and the number of child records to create. Below is the JSON I intend to pass.
{“parentRecordId”: “A16B3F4B-1BE7-E611-8101-E0071B6AF231″,”numberOfRecords”: 5}
Once I put this and use the option to generate the schema, I get the schema.
below is my entire flow
Just to summarize, I take the accountid and number of records to create input parameters. I initialize two variables one for record counter and one for storing the created contact’s id.
The important step is the last one – “Response” step.
There in the response body I set the array variable named Responses. This will be the response which will be received by the invoker.
To invoke this I create a plugin on POST create of the account and then from there I invoke the workflow
var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
var outputRecordId = (Guid)context.OutputParameters[“id”];
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(“https://prod-47.westus.logic.azure.com:443/workflows/25a5b8c4aapi-version=v=1.0&sig=ysWzgmjyNCSrUxy0KPWZlq-OvsVmVUqGalWaEnBuryY“);
webRequest.Method = “POST”;
webRequest.ContentType = “application/json”;
var data = “{\”parentRecordId\” : \”” + outputRecordId.ToString().Trim(‘{‘, ‘}’) + “\”, \”numberOfRecords\”: 5}”;
var dataContent = Encoding.UTF8.GetBytes(data);
using(var stream = webRequest.GetRequestStream())
{
stream.Write(dataContent, 0, dataContent.Length);
}
Stream response = ((HttpWebResponse)webRequest.GetResponse()).GetResponseStream();
using(var sr = new StreamReader(response))
{
// this will be a JSON array containing the list of newly created contact GUID’s
var result = sr.ReadToEnd();
}
The other part of the code are pretty simple. However the important part is authenticating with the flow. And this is achieved by taking the URL of the HTTP Request trigger from the flow.
Below is the screenshot from where you need to take this. And then create the HTTP Web Request object with this URL.
In the same way you can call any flow from anywhere be it client side script or server side. Your flow can be sending notifications and stuff which you can trigger programmatically using this procedure.
Hope this helps!
Debajit Dutta
(Dynamics MVP)
For consultation/ corporate training visit www.xrmforyou.com or reach out to us at info@xrmforyou.com
Our product offerings:
Role based views for Dynamics 365 (http://www.xrmforyou.com/role-based-views.html)
CRM-Sharepoint Attachment uploader and metadata manager (http://www.xrmforyou.com/sharepoint-integrator.html)
Record Cloner for Dynamics 365 (http://www.xrmforyou.com/record-cloner.html)
Multiselect picklist for Dynamics 365 (http://www.xrmforyou.com/multi-select-picklist.html)
Discover more from Debajit's Power Apps & Dynamics 365 Blog
Subscribe to get the latest posts sent to your email.
Hello Debajit, an interesting article… however you seem to have attempted to redact part of the URL, including the sig parameter, but you have simply changed the backgound colour and the parameters are readable just through highlighting the text!
Hey Dominic,
Thanks for reading. Missed the forecolor while working on the background. Thanks for tip 🙂
Trial environment. So less of a risk.
-Debajit
This trigger no longer appears to be available?
Hi Steve
Thanks for letting me know. I already implemented in project but customer didn’t complain still :). Wondering why 🙂
Let me check if this is still there. Thanks for letting me know.
Cheers!
Debajit