How to stop saving a record while using o data query in javascript RRS feed

  • Question

  • Hi All,

    I am having a problem using odata Query.

    I am having a Custom entity with name "abc". Here i have two fields named CustomerId and Admission date. CustomerId is a lookup field from some other entity. Now in the "abc" entity i would like to add a new record, but before saving that record i would like to check whether any other record present with same combination of ClientId and Admission date. Here i used odata query for condition and SDK.Rest service for retrieving records. Below is the code:

    var FieldObject = Xrm.Page.getAttribute("new_clientid");
        var FieldValue = FieldObject.getValue();
        var recordId = FieldValue[0].id;
        if (recordId != null) {
            var options = "select=new_name&$filter=new_ClientId/Id eq guid'" + recordId + "' and new_Admissiondate eq datetime'"+Admissiondate+''";
            SDK.REST.retrieveMultipleRecords('new_admission', options, successCallbackResults, function (error) { alert(error.message); }, function () { }, true)

    I am implementing this functionality on ONSave event. But the problem is i am unable to prevent the saving of record even if there is record present in the "abc". What functionality do i need to implement in successCallbackresults.

    Any help is Appreciable


    Tuesday, November 19, 2013 6:11 AM

All replies

  • Use javascript statement event.returnValue = false; in OnSave event to prevent save
    • Proposed as answer by SravaniRN459 Tuesday, November 19, 2013 6:28 AM
    • Edited by Yatin Babaria Tuesday, November 19, 2013 6:28 AM
    Tuesday, November 19, 2013 6:27 AM
  • You can use the context.getEventArgs().preventDefault() function to cancel the form save. This is the updated code for CRM2011 and should work for 2013 too (but I haven't tested it) In case you are wondering what is this 'context' object, its a default parameter passed to your method(which you are calling on OnSave) automatically by CRM. You need to enable the OnSave event handler(its a checkbox in the form customization properties where you select library and put method name) to pass the execution context as the first parameter to all custom methos called on OnSave.

    For example:

    function abc_OnSave(context) {
    if (//some condition is not met) {
    alert('Something is required!');

    More details available below:



    - Arvind (My posts represent my own views and not of Accenture's)

    Tuesday, November 19, 2013 6:51 AM
  • Thanks Arvind for your suggestion.

    But i tried in that manner also. It is not preventing the save.

    I tried in this manner also;

        if(SDK.REST.retrieveMultipleRecords('new_admission', options, successCallbackResults, function (error) { alert(error.message); }, function () { }, true)!=null)  


    but it's not working

    Tuesday, November 19, 2013 7:21 AM
  • Hi,

    That will not work because this is asynchronous call to get multiple records.

    So, you need to write synchronous call to get the data and then you should be able to prevent the save operation.

    Hope this helps. If you get answer of your question, please mark the response as an answer and vote as helpful !
    Vikram !

    Tuesday, November 19, 2013 7:24 AM