Hi,
We have an entity(client) related to the opportunity entity, the opportunity entity has 4 stages. what I am trying to achieve is, when the opportunity is moved from stage 1 to any other stage client entity should not be updated.
I thought i achieved this by disabling the client gird available in the opportunity form using java script, but later found that though the grid is disabled users are still able to double click on it and it brings up the client form and they are able to edit it or create new using Save & new.
So i went about writing another jscript in the client form (on save) using odata where it will check what stage the opportunity is and will not allow to save, save and close or save and new. The odata works fine but i am not able to restrict the save event i am using executionObj.getEventArgs().preventDefault(); but not sure where to have it in the code.
I am using it as shown below which works fine and does not allow anyone to save in stage 2,3 or 4 but it works even for stage1 so client cannot be saved at all. i tried using executionObj.getEventArgs().preventDefault(); in many places in the code but all went in vain.
Please help on this request, Thanks in advance.
// JScript source code
function odata(executionObj) {
var lookupid = Xrm.Page.getAttribute("davy_clientid").getValue();
if (lookupid != null) {
var serverUrl = document.location.protocol + "//" + document.location.host + "/";
var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";
var retrieveReq = new XMLHttpRequest();
var Odata = ODataPath + "/OpportunitySet?Select=davy_OpportunityStage&$filter=OpportunityId eq guid'" + lookupid[0].id + "'";
//alert(Odata);
retrieveReq.open("GET", Odata, true);
retrieveReq.setRequestHeader("Accept", "application/json");
retrieveReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
retrieveReq.onreadystatechange = function () { retrieveReqCallBack(this); };
executionObj.getEventArgs().preventDefault();
retrieveReq.send();
}
}
function retrieveReqCallBack(retrieveReq) {
if (retrieveReq.readyState == 4 /* complete */) {
if (retrieveReq.status == 200) {
var retrieved = JSON.parse(retrieveReq.responseText).d;
//alert(retrieved);
//Get result of oData and assign to variable.
var stage = retrieved.results[0].davy_OpportunityStage.Value;
//alert(stage);
if (stage == '2' || stage == '3' || stage == '4' || stage == '5') {
var message = "Client can be defined only in Stage One"
alert(message);
}
}
}
}