Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Custom button call logic app flow with JavaScript

Posted on by

Hi expert,

Now the requirement: create a custom button with ribbon workbench. Ribbon command use JavaScript webresource library call a external logic app flow. The error message:

8176.JPG

and

6874.JPG

My code as below

var createFile =

    {

        clickChooseTemplatefunction(primaryControl)

        {

            debugger;

            try {

            var formContext = primaryControl;

            var contactId = formContext.data.entity.getId();

            var fullName = formContext.getAttribute("fullname").getValue();

            var templateName = "Product Instruction";

            var params = {
                "contact_GUID": contactId,
                "contact_fullname": fullName,
                "template_name": templateName
            }
                var url = "">prod-xx.canadaeast.logic.azure.com:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
                var req = new XMLHttpRequest();
                req.open("POST"urltrue);
                req.setRequestHeader('Content-Type''application/json');
                console.log ("Sending Request");
                req.send(JSON.stringify(params));

                Xrm.Utility.alertDialog("Flow initiated. The document will be generated soon.");

            } catch (e) {
                Xrm.Navigation.openAlertDialog(e.message);
            }

        }

    }
Cannot figure out the reason causing the error. Could you please help to check and fix it? Thanks.
 
  • ALAN365 Profile Picture
    ALAN365 on at
    RE: Custom button call logic app flow with JavaScript

    Sorry for making confuse. Actually I have remove the event handler from form, no record is opening/refreshing to trigger flow any more. Here is the complete code:

    function clickChooseTemplate(primaryControl) {

        debugger;

        var formContext = null;

        if (primaryControl !== null) {

            if (typeof primaryControl.getAttribute === 'function') {

                formContext = primaryControl//called from the ribbon.

            } else if (typeof primaryControl.getFormContext === 'function'

                &&
                typeof(primaryControl.getFormContext()).getAttribute === 'function') {

                formContext = primaryControl.getFormContext(); // most likely called from the form via a handler
            }
        }

        var contactId = formContext.data.entity.getId().replace('{''').replace('}''');

        var firstName = formContext.getAttribute("firstname").getValue();

        var lastName = formContext.getAttribute("lastname").getValue();

        var fullName = firstName + " " + lastName;

        var templateName = "PRODUCT INSTRUCTION";

        var params = {

            "contact_GUID": contactId,

            "contact_fullname": fullName,

            "template_name": templateName
        }
        var url = "">prod-xx.canadaeast.logic.azure.com:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

        var req = new XMLHttpRequest();

        req.open("POST"urltrue);

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

        console.log("Sending Request");

        req.send(JSON.stringify(params));

        Xrm.Utility.alertDialog("Flow initiated. The document will be generated soon.");

    }
    Thank you guys again. Next step I will create a dynamic flyout menu list buttons, will open another question if it need to help from you.
  • Verified answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Custom button call logic app flow with JavaScript

    I still don't understand why the initial version of the code didn't work. Also, the code placed on a hander of the ribbon button click is not executed when the record is opened/refreshed. If it is get executed - either it's written the wrong way or it's registered the wrong way.

    Can you please provide the full code (including namespaces and so on) not the truncated version?

  • ALAN365 Profile Picture
    ALAN365 on at
    RE: Custom button call logic app flow with JavaScript

    Hi Andrew, Bipin,

    Thank you so much for your help. 

    I have tried to use the code Var formContext=primaryControl.getFormContext(), it still not works. The error message: 

    executionContext.getFormContext is not a function.

    Referring the blog - Error : getFormContext is not a function in UCI , Ribbon Button (http://microsoftdynamics.in/2020/08/22/error-getformcontext-is-not-a-function-in-uci-ribbon-button/), I rewrite the code (beginning part) as below:

    function clickChooseTemplate(primaryControl) {

        debugger;

        var formContext = null;

        if (primaryControl !== null) {

            if (typeof primaryControl.getAttribute === 'function') {

                formContext = primaryControl//called from the ribbon.

            } else if (typeof primaryControl.getFormContext === 'function'

                &&
                typeof(primaryControl.getFormContext()).getAttribute === 'function') {

                formContext = primaryControl.getFormContext(); // most likely called from the form via a handler
            }
        }

        var contactId = formContext.data.entity.getId().replace('{''').replace('}''');

        var firstName = formContext.getAttribute("firstname").getValue();

        var lastName = formContext.getAttribute("lastname").getValue();

        var fullName = firstName + " " + lastName;

      // continuedly

    Now it's work!

    But the new question is how to block the webresource to trigger the logic app when I loading/refreshing the Contact record, I only want to trigger the flow through click the custom button.

    Any idea? Thanks.

  • Verified answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: Custom button call logic app flow with JavaScript

    Hi,

    Can you move fullname code above and add alert(fullName);

    To see if you are able to get full name value .

    If you still get an error then try getting formContext as below

    Var formContext=primaryControl.getFormContext();

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Custom button call logic app flow with JavaScript

    Getting contactid that way is legit - learn.microsoft.com/.../getid

    According to the error you provided (cannot read property of undefined, reading entity) I believe your code fails on the line

    var contactId = formContext.data.entity.getId();

    All this looks like indirect troubleshooting and doesn't make a lot of sense, to be honest. Check my video on how you can troubleshoot your code - www.youtube.com/watch

  • ALAN365 Profile Picture
    ALAN365 on at
    RE: Custom button call logic app flow with JavaScript

    Thank you!

    Here is the screen:

    7838.JPG

    I'm thinking, for getting Contact record GUID, I need change the code line

    FROM 

    var contactId = formContext.data.entity.getId();
    TO
    var contactId = formContext.getAttribute("contactid");
    What do you think? Thank you so much.
  • Verified answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Custom button call logic app flow with JavaScript

    Gotcha. Your code looks good. Can you please provide the screenshot of your command from ribbon workbench? It seems that you do not pass the context inside - it should be CRM type, PrimaryControl.

  • ALAN365 Profile Picture
    ALAN365 on at
    RE: Custom button call logic app flow with JavaScript

    Hi Andrew,

    Thank you for your response and question. The custom button is on the ribbon of the form.

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Custom button call logic app flow with JavaScript

    Hello,

    Is it a ribbon of the grid or a ribbon of the form?

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans