web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Prevent saving a form after validating

(0) ShareShare
ReportReport
Posted on by 1,299

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);
}
}
}
}

*This post is locked for comments

I have the same question (0)
  • Davyjones Profile Picture
    1,299 on at

    Hi,

    Can anyone please help.

    Thanks

  • Rajkumar Rajaraman Profile Picture
    on at

    I think the other script loads immediately and blocks this.

    Try  to use debbuging,

    function odata(executionObj){

    debugger;

    ---put all your code---

    }

  • Davyjones Profile Picture
    1,299 on at

    Hi Guido,

    I am calling the function odata on the save function, and it works but not according to my logic as mentioned above, please feel free to ask questions. Thanks for your time.

  • Davyjones Profile Picture
    1,299 on at

    Hi Raj,

    The abouve script is the only script written for onsave event

  • Verified answer
    Community Member Profile Picture
    on at

    Hi Davyjones,

    You need to have execution context o prevent saving the form. You need to return to the main function.

    Using OData you can not have the execution context as you land up to the callback function.

    Instead of using OData, you can use the fetchxml to retrieve the opportunity details. The fetchxml query will return object in the main function itself (containing the opportunity details you need to retrieve). Hence you can check the condition using the object details and hence, can prevent saving the form.  

  • Davyjones Profile Picture
    1,299 on at

    Hi Purva,

    Thanks for your time, this seems to be a good plan, thought i have a doubt, can fetch xml be called on a onsave event and also if there is any links for examples.

    Thanks again.

  • Verified answer
    Community Member Profile Picture
    on at

    Yes, JavaScript can be written on "OnSave" event having fetchxml. Please find the following useful links -

    -dynamicscrmgirl.wordpress.com/.../crm-2013-javascript-retrievemultiple-records-using-fetchxml-query-and-soap-endpoint

    -www.crmsoftwareblog.com/.../using-the-fetchxml-crm-2011-service-within-a-javascript-web-resource

    -mscrmkb.blogspot.in/.../crm-2011-execute-fetchxml-from.html

    [If you find any helpful answer(s) of your question, please mark the answer(s) as verified answer(s).]

  • Davyjones Profile Picture
    1,299 on at

    Hi Purva,

    Thank you so much for your help, i almost spent 2 weeks on doing this and finally achieved this using fetchxml. Your links were usefull but for a beginner like me i found the below one usefull too.

    arunpotti.wordpress.com/.../retrieve-records-using-fetch-xml-java-script-in-crm-201113

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans