Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Uncaught Reference Error: Xrm is not defined.

Posted on by Microsoft Employee

Hi,

I am using CRM Rest Builder to create a record for an entity using Javascript, which accepts values from the user dynamically. It is being done within a HTML file. I have a submit button, which will create a new record and show up in the CRM database (a record in the entity). It does not submit the record in the database, and when debugging, it shows I have entered the values as input dynamically by the user. However, when debugging through Chrome's console, I get the following error:

Uncaught ReferenceError: Xrm is not defined
at createRecord (new_crmhtmltest?preview=1:49)
at HTMLButtonElement.onclick (new_crmhtmltest?preview=1:212)

-----------------------------------------------------------------------------------------

My whole HTML code as follows:

<html>

<head>
<title>My Payment Information</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<style>
* {
box-sizing: border-box;
}

/* Create two equal columns that floats next to each other */
.column {
float: left;
width: 50%;
padding: 10px;
/* Should be removed. Only for demonstration */
}

/* Clear floats after the columns */
.row:after {
content: "";
display: table;
clear: both;
}
</style>

<script src="../ClientGlobalContext.js.aspx" type="text/javascript" ></script> --> CAUSING THE ERROR

<script>
function createRecord(){
var entity = {};
entity.new_name = document.getElementById('new_name');
entity.new_email = document.getElementById('new_email');
entity.new_address = document.getElementById('new_address');
entity.new_city = document.getElementById('new_city');
entity.new_state = document.getElementById('new_state');
entity.new_zippostal = document.getElementById('new_zippostal');

var req = new XMLHttpRequest();
req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/new_entitytest", true);  //Causing the error
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>


</head>

<body>

<h2>Creating a record</h2>

<div class="row">
<div class="column">
<h2>Address Information:</h2>
Name: <input type="text" name="Name" id="new_name"><br>
Email: <input type="text" name="Email" id="new_email"><br>
Address: <input type="text" name="Address" id="new_address"><br>
City: <input type="text" name="City" id="new_city"><br>
State: <select id="new_state">
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
<option value="CA">California</option>
<option value="CO">Colorado</option>
<option value="CT">Connecticut</option>
<option value="DE">Delaware</option>
<option value="DC">District Of Columbia</option>
<option value="FL">Florida</option>
<option value="GA">Georgia</option>
<option value="HI">Hawaii</option>
<option value="ID">Idaho</option>
<option value="IL">Illinois</option>
<option value="IN">Indiana</option>
<option value="IA">Iowa</option>
<option value="KS">Kansas</option>
<option value="KY">Kentucky</option>
<option value="LA">Louisiana</option>
<option value="ME">Maine</option>
<option value="MD">Maryland</option>
<option value="MA">Massachusetts</option>
<option value="MI">Michigan</option>
<option value="MN">Minnesota</option>
<option value="MS">Mississippi</option>
<option value="MO">Missouri</option>
<option value="MT">Montana</option>
<option value="NE">Nebraska</option>
<option value="NV">Nevada</option>
<option value="NH">New Hampshire</option>
<option value="NJ">New Jersey</option>
<option value="NM">New Mexico</option>
<option value="NY">New York</option>
<option value="NC">North Carolina</option>
<option value="ND">North Dakota</option>
<option value="OH">Ohio</option>
<option value="OK">Oklahoma</option>
<option value="OR">Oregon</option>
<option value="PA">Pennsylvania</option>
<option value="RI">Rhode Island</option>
<option value="SC">South Carolina</option>
<option value="SD">South Dakota</option>
<option value="TN">Tennessee</option>
<option value="TX">Texas</option>
<option value="UT">Utah</option>
<option value="VT">Vermont</option>
<option value="VA">Virginia</option>
<option value="WA">Washington</option>
<option value="WV">West Virginia</option>
<option value="WI">Wisconsin</option>
<option value="WY">Wyoming</option>
</select><br>
Zip/Postal: <input type="text" name="Zip/Postal" id="new_zippostal"><br>
</div>

</div>

</body>

<button type="button" onclick="createRecord()">Submit</button>

</html>

----------------------------------------------------------------------------------------------------

In the above code, I've noted the lines where the errors are coming from. What I've tried doing is adding the script tag, but it seems to have made the problem worse. Any help would be appreciated. 

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Uncaught Reference Error: Xrm is not defined.

    Update: I've fixed my form by debugging line by line. I likely must have forgotten the document.getElementById('new_name').value. That fixed my problem. The other two posters have helped me immensely. Thanks everyone!!

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Uncaught Reference Error: Xrm is not defined.

    All I'm doing is filling out information dynamically and clicking the submit button and it still won't create a record in the database.

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Uncaught Reference Error: Xrm is not defined.

    Can you please tell us when you are actually getting this error , I mean what action?

    Looks like something is undefined getting in the code , try to debug the code hope you will  get the line where its getting undefined.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Uncaught Reference Error: Xrm is not defined.

    That seemed to fix a part of the problem. However, I get the following error on my console:

    CrashReport:

    FileName:/_common/global.ashx

    LineNumber:6823

    Function:

    ErrorReport:<CrmScriptErrorReport>

     <ReportVersion>1.0</ReportVersion>

      <OriginContext></OriginContext>

      <OriginCorrelationId>undefined</OriginCorrelationId>

     <ScriptErrorDetails>

      <Message>Uncaught Error: parameter is not passed or parameter is nNull or undefined</Message>

      <Line>6823</Line>

      <URL>/_common/global.ashx?ver=0</URL>

      <PageURL>//WebResources/new_crmhtmltest?preview=1</PageURL>

      <Function></Function>

      <FunctionRaw></FunctionRaw>

      <SourceScriptName></SourceScriptName>

      <CallStack>

    Error: parameter is not passed or parameter is nNull or undefined    at Function.Error.create (https://dynamics2practice.crm.dynamics.com/_static/_common/scripts/MicrosoftAjax.js?ver=897745810:214:13)    at Mscrm.XrmUtility.alertDialog (https://dynamics2practice.crm.dynamics.com/_common/global.ashx?ver=897745810:6823:141452)    at Function.Xrm.Utility.alertDialog (https://dynamics2practice.crm.dynamics.com/_common/global.ashx?ver=897745810:4078:20)    at XMLHttpRequest.req.onreadystatechange (https://dynamics2practice.crm.dynamics.com//WebResources/new_crmhtmltest?preview=1:85:27)   </CallStack>

     </ScriptErrorDetails>

     <ClientInformation>

      <BrowserUserAgent>Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36</BrowserUserAgent>

      <BrowserLanguage>undefined</BrowserLanguage>

      <SystemLanguage>undefined</SystemLanguage>

      <UserLanguage>undefined</UserLanguage>

      <ScreenResolution>1440x900</ScreenResolution>

      <ClientName>Web</ClientName>

      <ClienState>Online</ClienState>

      <ClientTime>2018-08-20T23:45:34</ClientTime>

     </ClientInformation>

    </CrmScriptErrorReport>

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Uncaught Reference Error: Xrm is not defined.

    In addition also you can try to replace below line   -

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

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Uncaught Reference Error: Xrm is not defined.

    Remove two dots before the CLientGLobal:

    <script src="/ClientGlobalContext.js.aspx" type="text/javascript" ></script> --> CAUSING THE ERROR

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Uncaught Reference Error: Xrm is not defined.

    Hi Ramakrishnan,

    How you are opening the HTML page in Dynamics CRM? Replace below code -

    Try with this

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

    window.parent.Xrm.Utility.alertDialog(this.statusText);

    OR

    -------------------------

    If you are opening from Ribbon you can also try with this -

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

    window.parent.opener.Xrm.Utility.alertDialog(this.statusText);

  • Suggested answer
    Mahendar Pal Profile Picture
    Mahendar Pal 45,095 on at
    RE: Uncaught Reference Error: Xrm is not defined.

    Hi,

    You can't directly access Xrm.Page in html, try to change it to parent.Xrm or window.parent.Xrm before Xrm.Page like below

    parent.Xrm.Page.context.getClientUrl()

    or

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

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!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans