locked
CRM 2011 synchronous jscript ajax -still can't cancel save event RRS feed

  • Question

  • This was what I thought was a nice example of using REST to query a related entity to extract a couple of field values and compare them to values in the calling record and do field validation based on that (ie don't save record until validation satisfied).  The validation works fine but the preventing the save event doesn't work.

    I thought my question was answered in the thread below - I set the ajax code block to run synchronously - but still no joy. 

    http://social.microsoft.com/Forums/en-IE/crmdevelopment/thread/518f4e40-7510-435c-99b6-987939c2ee8d

    See that after closing out of the ajax code block this.event.returnValue = undefined

    Hope someone can help since I'm now stuck on this one - how do I know if the ajax call  is *actually* running synchronously?

    NB I'm aware of the new ExecutionObj.getEventArgs().preventDefault(); method but I don't think this is the issue here

    http://community.dynamics.com/product/crm/f/117/p/58773/106151.aspx

    Thanks

    David

     

    function ValidateSubContract() {
    
    
            // Get the CRM URL
    
            var serverUrl = Xrm.Page.context.getServerUrl(); 
    
            // Cater for URL differences between on premise and online
            if (serverUrl.match(/\/$/)) {
                serverUrl = serverUrl.substring(0, serverUrl.length - 1);
            } 
    
        var guidvalue = Xrm.Page.data.entity.getId();
            // Specify the ODATA end point (this is the same for all CRM 2011 implementations)
            var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc"; 
    
            // Specify the ODATA entity collection (this needs to be specific to your entity)
            var ODATA_EntityCollection = "/new_subcontractSet"; 
    
            // Specify the ODATA filter
            var ODATA_Query = "?$select=new_project_new_subcontract/new_EndDate,new_project_new_subcontract/new_StartDate&$filter=new_subcontractId eq (guid'" + guidvalue + "')&$expand=new_project_new_subcontract"; 
            // Build the URL
            var ODATA_Final_url = serverUrl + ODATA_ENDPOINT + ODATA_EntityCollection + ODATA_Query; 
    
            //Calls the REST endpoint
     // need to set to synchronous in order to control form save but it doesn't work
            $.ajax({
                type: "GET",
                async: false,
                contentType: "application/json; charset=utf-8",
                datatype: "json",
                url: ODATA_Final_url,
                beforeSend: function (XMLHttpRequest) {
                    //Specifying this header ensures that the results will be returned as JSON.
                    XMLHttpRequest.setRequestHeader("Accept", "application/json");
                },
                success: function (data, textStatus, XmlHttpRequest) {
    
                    //This function will trigger synchronously if the Retrieve was successful
    debugger;
    // this.event is not exposed here
    SubContractDateChecks(data.d);
    
                },
                error: function (XmlHttpRequest, textStatus, errorThrown) {
                    //This function will trigger synchronously if the Retrieve returned an error
                    alert("ajax call failed");
                }
            });
         //in debugger this.event.returnValue = undefined
    
    }
    
    
    function SubContractDateChecks(OneEntity) {
    // debugger;
        var startdate = Xrm.Page.data.entity.attributes.get("new_contract_start_date").getValue();
        var enddate = Xrm.Page.data.entity.attributes.get("new_contract_end_date").getValue();
        var oneEntity=OneEntity.results[0];
        var projectstartdatefieldvalue = oneEntity.new_project_new_subcontract.new_StartDate;
    var projectstartdate= new Date(parseInt(projectstartdatefieldvalue.replace("/Date(", "").replace(")/", ""), 10));
    var projectenddatefieldvalue = oneEntity.new_project_new_subcontract.new_EndDate;
    var projectenddate= new Date(parseInt(projectenddatefieldvalue.replace("/Date(", "").replace(")/", ""), 10));
    
         
        if (projectenddate < enddate) {
            alert("Subcontract End date can not be beyond Project end date");
    
            event.returnValue = false;
         //in debugger this.event.returnValue = false
        //    return false
       }
    }
    

    Wednesday, November 30, 2011 10:02 PM

Answers

  • Which function is called on the onsave form's event ?

    I think you must prevent the saving event inside the function that CRM called before saving the record, not in the onSuccess $.ajax callback.

    Try to set a global variable and set the validation result into it and condition this in the original function.

    ie:

    var validationResult = true;

    function yourFunctionCRMCallOnSaveEvent(executionContext)

    {

    // your block code

     $.ajax(...., //async parameter must be FALSE ! this must be a synchronous call

    success: function (data, textStatus, XmlHttpRequest) { validationResult = false;  }

    );

    if (!validationResult) // or event.returnValue = validationResult....

    {

     executionObj.getEventArgs().preventDefault(); // I assume you're using CRM2011 if you used REST endpoint

    // I encountered some problem with preventDefault in some scenarios, but the event.returnValue works well

    }

    }

     

    }

    (I wrote this js in notepad and didnt test anything)

    Did this solved your problem?

     


    -- Christophe Trevisani Chavey www.notafiscaldeservicos.com.br www.virtualgroup.com.br
    • Marked as answer by David b111 Thursday, December 1, 2011 1:05 PM
    Wednesday, November 30, 2011 11:04 PM