Run a flow/Automate from a button on entity form. A ribbon workbench alternative coming up. A COMMAND BAR DESIGNER FOR MODEL-DRIVEN APPS(Preview)

 

Run a flow/Automate from a button on entity form. A ribbon workbench alternative coming up. A COMMAND BAR DESIGNER FOR MODEL-DRIVEN APPS(Preview)



Remember the days if we have to customise the command bar and we have to poke around ribbon workbench created by Scott Durow which is part of the XRM Toolbox. It has served a greater purpose of customising the command bar. There was not an easy way provided by Microsoft to customise these command buttons. Till now.

Microsoft is finally rolling out a new (preview) feature that is part of the configuration tools in Dynamics 365 which will allows us to configure the command bar/ribbon in model-driven apps. It is still in preview but it will get better from here. Let’s dive in to it.

One of the common requirements from the entity form is to run some actions related to that record. Such as sending an email or getting information from external system or create some unrelated complex recorders etc. Flow is the easiest way to connect to external system or perform low code functions. There are ways to trigger the flows but a button to perform a function provides a better UI for customer.

Let’s dive in to it.

 

Set up a http flow

A Https flow need to be created which will be called from JS.

1.                   Crate a flow using Https request received trigger.



2.                   After creating a trigger, we are defining the input parameters. These are defined using json. Add json in to Request Body Json Schema

a.       For demo purpose I have defined “samtestid”, which is type of string and gets Guid of the SamTest table.

{

    "type""object",

    "properties": {

        "samtestid": {

            "type""string"

        }

    }

}


 


 

3.                   Once you save the trigger, make a note of the HTTP POST URL.  This will be used to run the flow.

 



 

 

4.                   We are using this Id to get the Name column of D365 record.



 

5.                   In the end we are returning the retrieved response.



 

Set up JavaScript

 

For demo purpose we are creating a JavaScript file, which is used to call client-side function “RunFlow” which will run the flow. This will take Guid of the record and once the flow is processed, it will return the name of the record.

 


1.                   From D365 CRM PowerApps navigate to solution and create a web resource of type JavaScript(JS). For demo purpose we are calling it samflow.js



 

2.                   Add following js code and publish the web resource.

 

function Runflow(samtestid) {  

       if(samtestid!-null)

       {

              samtestid=samtestid.replace("{", "").replace("}", "");

       }

      

       var flowUrl = "< FLOW HTTP POST URL >";

 

   /// Request preparation   

    var input = JSON.stringify({

        "samtestid": samtestid

    });

    var req = new XMLHttpRequest();

    req.open("POST", flowUrl, true);

    req.setRequestHeader('Content-Type', 'application/json');

 

////Request processing setup

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);

            }

        }

    }; 

 

////End

 

    req.send(input); /// Request sent

}

 

Set up Command Button on D365 form.

We are creating a Model driven app “Sam Test” which has a table “Business name” added.

 

1.                   Create a new table in solution “Business name” in your D365 instance.

 

2.                   Create a new model driven app “Sam test” and edit app in preview mode.


 

3.                   Click on Add page, select “Table based view and form”. Select “Business name” from the table list and add it in app



 

 

 

4.                   Click on “Edit command bar” for selected Table.




 

 

5.                   For demo we want to add Command button on main form, select “Main form”.

 



 

6.                   Click on “New command” and select the new button.

 



 

 

7.                   On right pan we can select all the properties for the command bat such title, icon and action etc. For demo we renamed button to “Run flow from js”.




 

8.                   As we want to call our JavaScript function. We are choosing

a.       Action – Run JavaScript

b.       Library – Select your web resource you added before in your solution. If it not listing you can add your library.

c.       Name of the function you want to call, for this demo we are calling “Runflow”, which was created before in Javascript step.

d.       We can pass multiple parameters to our function. We added 1 parameter and passed FirstPrimaryItemId. This returns the GUID of the record which the user is viewing or on which the user is running the code. For more information on type of parameters, click here.

 



 

9.                   Click Save and publish to update your changes in model driven app.

 

 

Set up Command Button on D365 form.

After all configurations lets see how it working. A quick rundown on configurations and working example.

 


 

Comments

  1. Hello thanks for you work ! However, I followed each steps as you described but as the end when I click on my button nothing happens. Even a simple console log is not trigger. Any ideas ?

    ReplyDelete
    Replies
    1. Hi Missel,
      I am facing the same issue, however i have provided the right url along with right parameters passed.Did you find what was your issue with the button click or triggering flow? Please let me know.

      Delete

Post a Comment

Popular posts from this blog

Portals support for Microsoft Power Platform CLI - My take on development and deployment