Task-5 _ Call the Powerautomate on click on the approve button and Send an Email

 

Call the Powerautomate on click on the approve button and Send an Email

Call the Powerautomate on click on the approve button and send mail to parents.

User Story: -

When we click on the Approve button in the student Entity record then a confirmation email should be sent to the respective student, do this operation using powerautomate.

Sudo code: -

  • Create an Approve button for custom entity using Ribbon work bench in XRM Toolbox. 
    • Create a solution for custom entity ribbon workbench. 
    • Add the required entity meta data to the solution. 
    • Use this solution in Ribbon workbench to create buttons and publish it in the entity.
  • Create a Power automate for that particular entity, with triggering point as a HTTP Request.
  • Retrieve the email to a variable from HTTP Request body.
  • Check the null condition for email.
  • If email is present then send email.
  • If email is not present then stop the power automate.
  • After saving the power automate we will get the URL form triggering the power automate.
  • Now create a JavaScript file, with a function to trigger the power automate.
    • In that JavaScript code, create XML HTTP request.
    • open the connection with POST method with the given power automate URL.
    • retrieve the email and name from the formContext.
    • Create a body object with name and email and convert into JSON object.
    • Send the HTTP request.
  • Create a web resource with JavaScript code.
  • Use this JavaScript web resource to Action function for the Approve button using Ribbon workbench in XRM Toolbox.

JavaScript code: -

function CallPowerAutomate(primaryControl) {
    var formContext = primaryControl;
    var id = formContext.data.entity.getId().replace("{", "").replace("}", "");

    Xrm.WebApi.online.retrieveRecord("naree_student", id, "?$select=naree_name&$expand=naree_Parent($select=emailaddress1,fullname)").then(
        function success(result) {
            var naree_name = result["naree_name"];
            if (result.hasOwnProperty("naree_Parent")) {
                var naree_Parent_emailaddress1 = result["naree_Parent"]["emailaddress1"];
                var naree_Parent_fullname = result["naree_Parent"]["fullname"];

                if (naree_Parent_emailaddress1 != null) {
                    var Url = "Power automate generated URL";
                    var httpReq = new XMLHttpRequest();
                    httpReq.open("POST", Url);
                    httpReq.setRequestHeader("Content-Type", "application/json; charset=UTF-8");

                    var body = JSON.stringify({ "name": naree_name, "email": naree_Parent_emailaddress1, "Parent Name": naree_Parent_fullname })
                    httpReq.send(body);
                }
            }
        },
        function (error) {
            Xrm.Utility.alertDialog(error.message);


        }
    );
   
}







Comments

Popular posts from this blog

How to create an Azure AD(Active Directory) and create an application to connect Dynamics 365 from console application

Task -6 JavaScript , Regular expressions

Task_1 JavaScript