I have issues with changing the state, status and additional data for Entity Record (Dynamics CRM 2015). . I am trying to update the data in just created Entity record. But all the time I get Bad Request. Please help me!?
this is my code.
- Entity name: "Send Email Campaign"
- Entity Schema Name: "concep_emailcampaign"
- Record test name: 19/05/17-2
function CreateInactiveEmailCampaign(data) {
var emailcampaignId, EmailCampaignName;
var context = Xrm.Page.context;
var serverUrl = Xrm.Page.context.getClientUrl();
var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
var CRMObject = new Object();
/////////////////////////////////////////////////////////////
// Specify the ODATA entity collection
var ODATA_EntityCollection = "/concep_emailcampaignSet";
/////////////////////////////////////////////////////////////
CRMObject.concep_name = data.Name;
EmailCampaignName = data.Name;
CRMObject.concep_Subject = data.Subject;
CRMObject.concep_SentDateTime = data.sentdatetime;
CRMObject.concep_Recipients = data.recipientsCount;
CRMObject.concep_CampaignID = data.CampaignID;
CRMObject.concep_CampaignCode = data.CampaignCode;
//Parse the entity object into JSON
var jsonEntity = window.JSON.stringify(CRMObject);
//Asynchronous AJAX function to Create a CRM record using OData
$.ajax({ type: "POST",
contentType: "application/json; charset=utf-8",
datatype: "json",
url: serverUrl + ODATA_ENDPOINT + ODATA_EntityCollection,
data: jsonEntity,
beforeSend: function (XMLHttpRequest) {
//Specifying this header ensures that the results will be returned as JSON.
XMLHttpRequest.setRequestHeader("Accept", "application/json");
},
success: function (data, textStatus, XmlHttpRequest) {
//alert("success");
debugger;
var NewCRMRecordCreated = data["d"];
ChangeRecordStatus(NewCRMRecordCreated, 1, 2, serverUrl);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("failure");
}
});
}
function ChangeRecordStatus(data, stateCode, statusCode, serverUrl) {
// I need to update the record with
//CRMObject.statecode = {}
//CRMObject.statecode.Value = 1; //Inactive
//CRMObject.statuscode = {Value:2}
//CRMObject.statuscode.Value = 2; //Inactive
//CRMObject.concep_Campaign = {}
//CRMObject.concep_Campaign.Id = data.Campaign;
//CRMObject.concep_SendAccount = {}
//CRMObject.concep_SendAccount.Id = data.SendAccount;
//CRMObject.concep_SendAccount.LogicalName = data.SendAccount;
//CRMObject.concep_SendAccount.Name = data.SendAccount;
//CRMObject.concep_ResponsesTotal = ''
//CRMObject.concep_ResponsesToRecipientsRate = ''
//CRMObject.concep_concep_emailcampaign_list = ''
//CRMObject.concep_emailcampaign_campaign. = ''
//CRMObject.concep_emailcampaign_sendaccount. = ''
//CRMObject.concep_surveyresponse_emailcampaign. = ''
//CRMObject.user_concep_emailcampaign. = ''
var serverUrl = serverUrl;
var RECORD_ID = data.concep_emailcampaignId
var EntityLogicalName = data.concep_name;
var request = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">";
request += "<s:Body>";
request += "<Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";
request += "<request i:type=\"b:SetStateRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\" xmlns:b=\"http://schemas.microsoft.com/crm/2011/Contracts\">";
request += "<a:Parameters xmlns:c=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">";
request += "<a:KeyValuePairOfstringanyType>";
request += "<c:key>EntityMoniker</c:key>";
request += "<c:value i:type=\"a:EntityReference\">";
request += "<a:Id>" + RECORD_ID + "</a:Id>";
//request += "<a:LogicalName>" + EntityLogicalName + "</a:LogicalName>";
request += "<a:Name i:nil=\"true\" />";
request += "</c:value>";
request += "</a:KeyValuePairOfstringanyType>";
request += "<a:KeyValuePairOfstringanyType>";
request += "<c:key>State</c:key>";
request += "<c:value i:type=\"a:OptionSetValue\">";
request += "<a:Value>" + stateCode + "</a:Value>";
request += "</c:value>";
request += "</a:KeyValuePairOfstringanyType>";
request += "<a:KeyValuePairOfstringanyType>";
request += "<c:key>Status</c:key>";
request += "<c:value i:type=\"a:OptionSetValue\">";
request += "<a:Value>" + statusCode + "</a:Value>";
request += "</c:value>";
request += "</a:KeyValuePairOfstringanyType>";
request += "</a:Parameters>";
request += "<a:RequestId i:nil=\"true\" />";
request += "<a:RequestName>SetState</a:RequestName>";
request += "</request>";
request += "</Execute>";
request += "</s:Body>";
request += "</s:Envelope>";
//send set state request
$.ajax({
type: "POST",
contentType: "text/xml; charset=utf-8",
datatype: "xml",
url: serverUrl + "/XRMServices/2011/Organization.svc/web",
data: request,
beforeSend: function (XMLHttpRequest) {
XMLHttpRequest.setRequestHeader("Accept", "application/xml, text/xml, */*");
XMLHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
},
success: function (data, textStatus, XmlHttpRequest) {
alert("Success");
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest);
alert(errorThrown);
}
});
}
This may help: Active/Deactive Record using JS in CRM