Hi All,
I am new in CRM & Javascript as well.
I am trying to add a case(record) on button onclick event using javascript.
Below is my HTML Web Resource which adds button & also creates a record on its onclick event.
<html><head>
<title>Show Case</title>
<script src="../WebResources/new_jquery_1.9.1.min" type="text/javascript"></script>
<script type="text/javascript" src="ClientGlobalContext.js.aspx"></script>
<script type="text/javascript" src="../WebResources/new_SDK_WebApi"></script>
<script type="text/javascript">
function DisplayCase()
{
var caseResult;
var casetitle = window.parent.Xrm.Page.getAttribute("new_casetitle").getValue();
var origin= window.parent.Xrm.Page.getAttribute("new_caseorigincode").getValue();
if(casetitle!=null && origin!=null)
{
var incidents= new Object();
incidents.title=casetitle;
incidents.caseorigincode=origin;
//Create the Case Record
SDK.REST.createRecord(
incidents,
"incidents",
function (result) { caseResult = result },
function (error) { Xrm.Utility.alertDialog(error.message);},
);
}
}
</script>
<style>
h5 {
font-family: Segoe UI,Tahoma,Arial;
font-weight: bold;
font-variant: normal;
color: #000080;
text-decoration: underline;
}
p {
font-family: Segoe UI,Tahoma,Arial;
font-size: 13px;
}
</style>
</head>
<body style='word-wrap: break-word;'>
<table>
<tbody>
<tr>
<td>
<button type="button" onclick="DisplayCase();">
Display Case
</button>
</td>
</tr>
</tbody>
</table>
</body></html>
I am getting error as below:
I am trying to fetch CaseTitle(string), Subject(Lookup), Origin(OptionSet) from contact page & create a case record. I am getting CaseTitle & Origin (but failed to fetch lookup value so omitted initially for getting at least record created with Case Title & Origin)
I tried adding array for a result but still there is a same issue.
Have I missed anything? Please guide.
Thanks in advance.
*This post is locked for comments
Please check Odata URL you have write is correct and also send minimum parameter to works something not all the parameter.
I did all the mentioned changes but while debugging I get the issue at below point in my SDK file where I am getting 2 as my readyState value & at line "req.send(JSON.stringify(object))" it displays as "Failed to load resource: the server responded with a status of 404 (Not Found)"
Hi Mandy,
Kindly use this code, because of the schema name issue. I have checked the below code, it's working fine.
//Get current record information
var contactId = window.parent.Xrm.Page.data.entity.getId();
var entityName = window.parent.Xrm.Page.data.entity.getEntityName();
var contactName = window.parent.Xrm.Page.getAttribute("fullname").getValue();
//alert(contactId + '+++' + entityName + '+++' + contactName);
var incidents= new Object();
incidents.CustomerId={
Id: contactId,
Name: contactName,
LogicalName: entityName
};
//incidents.Title=casetitle;
incidents.Title='Test Case';
//Create the Case Record
SDK.REST.createRecord(
incidents,
"Incident",
function (result) { Xrm.Utility.alertDialog('Case Created'); },
function (error) { Xrm.Utility.alertDialog(error.message);},
);
Hi Mandy ,
From 2016 Dynamics CRM 2016 MS introduce OData V4 or Web API . You can use Web API to create record . Here is the sample code .
Hope this will help you. Please mark as answer it it resolve your issue.
function CreateRecord2016(entity, entityName){
try{
/* Example of entity : note, all fields in lowercase
var CRMObject = new Object();
CRMObject.new_name = "test name";
CRMObject.new_customfieldname = "test";
*/
var req = new XMLHttpRequest()
req.open("POST",encodeURI(clientURL + "/api/data/v8.0/" + entityName + "s"), true);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.onreadystatechange = function () {
if (this.readyState == 4 /* complete */) {
req.onreadystatechange = null;
if (this.status == 204) {
var createdRecordId = this.getResponseHeader("OData-EntityId");
result = createdRecordId;
}
else {
var error = JSON.parse(this.response).error;
//console.log(error.message);
result = error;
}
}
};
req.send(window.JSON.stringify(entity));
//Asynchronous AJAX function to Create a CRM record using OData
return result;
}catch(err){
showError(arguments.callee.toString().match(/function\s+([^\s\(]+)/)[1], err.message);
}
}
function showError(functionName, errorMessage)
{
alert(functionName + ": " + errorMessage);
}
Issue is still the same :-(
Change your code like this,
//Get current record information
var contactId = window.parent.Xrm.Page.data.entity.getId();
var entityName = window.parent.Xrm.Page.data.entity.getEntityName();
var contactName = window.parent.Xrm.Page.getAttribute("fullname").getValue();
var customerid =
{
Id: contactId,
Name: contactName,
LogicalName: entityName
};
if(customerid !=null && casetitle!=null && origin!=null)
{
var incidents= new Object();
incidents.customerid=customerid;
incidents.title=casetitle;
//Create the Case Record
SDK.REST.createRecord(
incidents,
"incidents",
function (result) { caseResult = result },
function (error) { Xrm.Utility.alertDialog(error.message);},
);
}
Sorry by mistake I sent that issue. I did the same thing & find the answer.
Actually I am facing issue with the line of customerid as "Uncaught TypeError: Cannot set property 'customerid' of undefined"
For this, I declared the variable as customerid but it is showing same issue.
Also, I have changed it as below
var customerid =
{
Id: contactId,
Name: contactName,
LogicalName: entityName
};
if(customerid !=null && casetitle!=null && origin!=null)
{
var incidents= new Object();
incidents.contactid=customerid;
incidents.title=casetitle;
incidents.caseorigincode=origin;
//Create the Case Record
SDK.REST.createRecord(
incidents,
"incidents",
function (result) { caseResult = result },
function (error) { Xrm.Utility.alertDialog(error.message);},
);
}
but now it is giving me "Error : 400: Bad Request: undefined" popup.
Hi Mandy,
Kindly find the below details for your reference.
In contact entity, the primary field is fullname. So, we need to alter the contactName from new_name to fullname. Try this and let me know if any clarifications/updates are required.
//Get current record information
var contactId = window.parent.Xrm.Page.data.entity.getId();
var entityName = window.parent.Xrm.Page.data.entity.getEntityName();
var contactName = window.parent.Xrm.Page.getAttribute("fullname").getValue();
incidents.customerid =
{
Id: contactId,
Name: contactName,
LogicalName: entityName
};
Hi Marimuthu,
Thanks for your instant reply.
Hi Mandy,
The case creation needs a value for customerid lookup field (i.e., from contact/account). Without customerid lookup field value, we are not able to create the case record. In your case, you need to assign the contact values as array object for case customerid field. kindly find the below code for your reference. You need to add those lines in your code.
ex:
//Get current record information
var contactId = window.parent.Xrm.Page.data.entity.getId();
var entityName = window.parent.Xrm.Page.data.entity.getEntityName();
var contactName = window.parent.Xrm.Page.getAttribute("new_name").getValue();
incidents.customerid =
{
Id: contactId,
Name: contactName,
LogicalName: entityName
};
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,240 Super User 2024 Season 2
Martin Dráb 230,149 Most Valuable Professional
nmaenpaa 101,156