Reputation: 296
I have a custom javascript in Appointment form. There is a button on ribbon. Button should change the state code then save the form. But when I try to save, if there was a field that changed, the form is not save itself. Here is my code below;
Xrm.Page.data.entity.save();
var newStatus = 3;
var json = new JSonObject();
var data = json.SetState(Xrm.Page.data.entity.getId(), Xrm.Page.data.entity.getEntityName(), 1, newStatus);
if (data != true)
alert("Error!");
else {
Xrm.Page.data.entity.save('saveandclose');
//window.parent.location.reload();
}
I tried everything. First save then setState then save again etc. If the code is like this, it causes an error which is "Cannot update Closed or Cancelled Activity". If it saves first then reload then setState then save then reload, the fields which changed were not get last value. It gets previous value which manually saved from Save Button.
Upvotes: 0
Views: 918
Reputation: 2123
function changeRecordStatus(RECORD_ID,stateCode,statusCode) {
// create the SetState request
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>cmic_systemusersalesterritoryassociation</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: Xrm.Page.context.getServerUrl() + "/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) {
Xrm.Page.ui.close();
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
}
I used the above mentioned function to Change the RecordStatus Asynchronously
Create a function name AppointmentOnSave()
like below and call the above function in that:
function AppointmentOnSave(){
var AppointmentGuid = Xrm.Page.data.entity.getId();
var StateCode = 1;
var StatusCode= 3;
Xrm.Page.data.entity.save( "saveandclose");
changeRecordStatus(AppointmentGuid,StateCode,StatusCode); //Mark Completed
}
Here is the Second approach and easy one: try this:
First need to add these three javascript files as webresource in CRM
jquery1.4.1.min.js
json2.js
SDK.JQuery.js
these files are in SDK under Script folder (sdk\samplecode\js\restendpoint\jqueryrestdataoperations\jqueryrestdataoperations\scripts)
then:
function AppointmentOnSave(){
var AppointmentGuid = Xrm.Page.data.entity.getId();
var StateCode = 1;
var StatusCode= 3;
var Appoinment= {};
Appoinment.StateCode=1;
Appoinment.StatusCode=3;
setTimeout(function(){
SDK.JQuery.updateRecord(
AppointmentGuid ,
Appoinment,
"task",
function success (msg) {
alert(msg);
},
function failed (error) {
alert(error);
}
);
}, 500);at
Xrm.Page.data.entity.save( "saveandclose");
}
Upvotes: 1