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
Thanks Jagannath,
it worked for me after putting
<script src="ClientGlobalContext.js.aspx" type="text/javascript"></script>
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.
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.
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
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,253 Super User 2024 Season 2
Martin Dráb 230,188 Most Valuable Professional
nmaenpaa 101,156