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

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

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

(0) ShareShare
ReportReport
Posted on by

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

I have the same question (0)
  • Verified answer
    Jagannath Profile Picture
    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

  • Aric Levin - MVP Profile Picture
    30,190 Moderator 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.

  • Suggested answer
    Nithya Gopinath Profile Picture
    17,078 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.

  • Community Member Profile Picture
    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>

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#1
UllrSki Profile Picture

UllrSki 2

#3
ED-30091530-0 Profile Picture

ED-30091530-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans