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.