Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

I am getting "TypeError: parent.Xrm is undefined" while trying to create a record on click of HTML button

Posted on by Microsoft Employee

I  am getting "TypeError: parent.Xrm is undefined" while trying to create a record on click of submit button on html webresource. I am trying to create using WEBAPI call , also have tried to create using xrmservicetoolkit and also tried using xrm.webapi https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/clientapi/reference/xrm-webapi.

I was using below code.

<html><head>
    <meta>
    <meta>
    <meta>
    <meta>
    <meta>
<meta></head>
<body style="word-wrap: break-word;">
    <!--<script src="gaayam.crm8.dynamics.com/.../new_xrmservicetoolkit"></script>
    <script src="gaayam.crm8.dynamics.com/.../new_Jquerytoolkit"></script>
    <script src="gaayam.crm8.dynamics.com/.../new_json2toolkit"></script>-->
    <p>FirstName: <textarea cols="20" id="myTextBox" rows="1"></textarea></p>
    <p>
        LastName: <textarea cols="20" id="myTextBox1" rows="1"></textarea>
    </p>
    <button onclick="MyFunction()">Click Me</button>
    <script>
        function MyFunction() {
            debugger;
            var c = document.getElementById("myTextBox").value;
            alert(c);
            var entity = {};
            entity.name = c;
            //var xrm = window.parent.Xrm;
            

            var req = new XMLHttpRequest();
            req.open("POST", parent.Xrm.Page.context.getClientUrl() + "/api/data/v8.0/accounts", true);
            req.setRequestHeader("OData-MaxVersion", "4.0");
            req.setRequestHeader("OData-Version", "4.0");
            req.setRequestHeader("Accept", "application/json");
            req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
            req.onreadystatechange = function () {
                if (this.readyState === 4) {
                    req.onreadystatechange = null;
                    if (this.status === 204) {
                        var uri = this.getResponseHeader("OData-EntityId");
                        var regExp = /\(([^)]+)\)/;
                        var matches = regExp.exec(uri);
                        var newEntityId = matches[1];
                    } else {
                        Xrm.Utility.alertDialog(this.statusText);
                    }
                }
            };
            req.send(JSON.stringify(entity));
        }

    </script>


</body></html>

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: I am getting "TypeError: parent.Xrm is undefined" while trying to create a record on click of HTML button

    Thanks Jagannath,

    it worked for me after putting

    <script src="ClientGlobalContext.js.aspx" type="text/javascript"></script>

  • Suggested answer
    Nithya Gopinath Profile Picture
    Nithya Gopinath 17,074 on at
    RE: I am getting "TypeError: parent.Xrm is undefined" while trying to create a record on click of HTML button

    Hi Chaithu,

    It is good to include the script tags in the head tag of HTML web resource. Please try to do the same and also try using

    window.parent.Xrm.Page.context.getClientUrl()

    Hope this helps.

  • Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: I am getting "TypeError: parent.Xrm is undefined" while trying to create a record on click of HTML button

    What version of Dynamics 365 are you on? 8.2/9.0?

    Xrm.WebApi is only supported on v9.

    For 8.x documentation refer to:

    msdn.microsoft.com/.../mt593051.aspx

    Hope this helps.

  • Verified answer
    Jagannath Profile Picture
    Jagannath 545 on at
    RE: I am getting "TypeError: parent.Xrm is undefined" while trying to create a record on click of HTML button

    Hi Chaitu,

    Just add these two line in your code.

    <script src="ClientGlobalContext.js.aspx" type="text/javascript"></script>

    <script src="../WebResources/msdyn_SDK.REST.js" type="text/javascript"></script>

    and generate a query from rest builder.

    and with the help of the query  you can create a record from html button.

    go through this blog once.Dont use xrm in html resource.It will not support.

    arunpotti.wordpress.com/.../rest-create-example

    Thanks,

    Jagannath

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,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans