Post Views: 571
function createPrescription(formContext)
{
var currentDAID = formContext.data.entity.getId();
currentDAID = currentDAID.replace("{","").replace("}","");
var count = GetCountofExisitingPrescriptions(currentDAID);
alert(count);
if(count<=0)
{
var data =
{
"lifeline_name": "Prescription",
"lifeline_issuedate" : new Date(),
"lifeline_DoctorAppointment@odata.bind": "lifeline_doctorappointments("+currentDAID+")"
}
Xrm.WebApi.createRecord("lifeline_prescription", data).then(
function success(result) {
alert("Prescription created successfully.");
},
function (error) {
console.log(error.message);
// handle error conditions
}
);
}
else{
alert("Aleady prescription crerated.");
}
}
function GetCountofExisitingPrescriptions(docappid)
{
//debugger;
var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>"+
" <entity name='lifeline_prescription'>"+
" <attribute name='lifeline_prescriptionid' />"+
" <attribute name='lifeline_name' />"+
" <attribute name='createdon' />"+
" <order attribute='lifeline_name' descending='false' />"+
" <filter type='and'>"+
" <condition attribute='lifeline_doctorappointment' operator='eq' uitype='lifeline_doctorappointment' value='{"+docappid+"}' />"+
" </filter>"+
" </entity>"+
"</fetch>"
var encodedFetchXML = encodeURI(fetchXml);
var query = "/api/data/v9.2/lifeline_prescriptions?fetchXml="+encodedFetchXML;
var globalContext = Xrm.Utility.getGlobalContext();
var finalpathwithquery = globalContext.getClientUrl() + query;
//use XML HTTP Request for webAPI call
var data = null;
var isAsync = false;
var req = null;
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
req = new ActiveXObject("MSXML2.XMLHTTP.3.0");
}
req.open("GET",finalpathwithquery,isAsync);
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.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var result = JSON.parse(this.response);
data = result;
}
else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();
return data.value.length;
}