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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Custom button call logic app flow with JavaScript

(0) ShareShare
ReportReport
Posted on by 2

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.
 
I have the same question (0)
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

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

  • ALAN365 Profile Picture
    2 on at

    Hi Andrew,

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

  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    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
    2 on at

    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.
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    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

  • Verified answer
    Bipin D365 Profile Picture
    28,985 Moderator on at

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

  • ALAN365 Profile Picture
    2 on at

    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
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    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
    2 on at

    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.

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
11manish Profile Picture

11manish 134

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 79 Super User 2026 Season 1

#3
CP04-islander Profile Picture

CP04-islander 57

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans