none
Unable to Cancel Save Event in MSCRM 2011 Case Entity Form

    Question

  • Hi,

    I am  using context.getEventArgs().preventDefault(); to cancel save event in mscrm 2011 but still i am able to create record

    and save is not getting cancelled . I am passing context as first parameter  in the java script , why it's not working i am unable to figure out. Any help would be appreciated.


    Abhishek

    Thursday, July 11, 2013 7:40 AM

All replies

  • Provided that your function prototype is someOnSaveFunction(context), this this code is correct.

    If it's not working there could be a script error that is being raised and so stopping the preventDefault from being called. Use the F12 script debugger to step through the onsave event to see if it's working as expected.

    hth


    Scott Durow
    Blog www.develop1.net    Follow Me
    Rockstar365
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    Thursday, July 11, 2013 8:00 AM
  • Hi,

    I am using  javascript as descibed below . there is no syntax error

    function errorHandler(error) {
     writeMessage(error.message);
    }
    
     var   create ;
    function getFirstContactToBePrimaryContact(context) {
    
    debugger;
    
    var CustomerGUID = Xrm.Page.data.entity.attributes.get("customerid").getValue()[0].id; 
    
    
     SDK.REST.retrieveMultipleRecords(
    
         "Incident",
         "$select=CreatedOn &$filter=CustomerId/Id eq (guid'" + encodeURIComponent(CustomerGUID) + "')&$orderby=CreatedOn desc &top=1" ,
         test,      
         errorHandler,
         function () { 
         //OnComplete handler
    
    if(create =="false")
    {
    debugger;
    context.getEventArgs().preventDefault();
    //event.returnValue = false;
    
     //return false;
    
    //context.getEventArgs().isDefaultPrevented()
    
    
    }
          }
       );
    }
    
     function test (results) {
    debugger;
          var date= results[0].CreatedOn;
    
           var today = new Date(); 
       var today_date = today.getDate();   
      var today_month = today.getMonth();
      today_month++;
       var today_year = today.getFullYear();
    
     var dateinput_date = date.getDate();   
     var dateinput_month = date.getMonth();
     dateinput_month++;
       var dateinput_year = date.getFullYear();
    
    
    
    if (date!= null && dateinput_year == today_year && dateinput_month == today_month && dateinput_date == today_date) 
    {
    
    alert("duplicate");
    
    create ="false";
    
    
    }
    
    
         }


    Abhishek

    Thursday, July 11, 2013 9:19 AM
  • I see the issue - you have got an async call back in your REST request - the problem is that your call to preventDefault will happen *after* CRM has started to save the form since the on save handler will exit before the create has completed.

    You'll need to make a call synchronously  - the REST library in the SDK doesn't support this I think - you'll need to use request.send or use the jQuery example with async:false - http://msdn.microsoft.com/en-us/library/gg309549.aspx

    hth



    Scott Durow
    Blog www.develop1.net    Follow Me
    Rockstar365
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    Thursday, July 11, 2013 11:30 AM
  • I tried using soap request as descibed in http://mileyja.blogspot.in/2011/07/using-jscript-to-access-soap-web.html  blog but i am not getting any result. I have generated this script from soap logger there i was getting two records as output , but through javascript no result. Below is the code.
    if (typeof (SDK) == "undefined")
       { SDK = { __namespace: true }; }
           //This will establish a more unique namespace for functions in this library. This will reduce the
           // potential for functions to be overwritten due to a duplicate name when the library is loaded.
           SDK.test54 = {
               _getServerUrl: function () {
                   ///<summary>
                   /// Returns the URL for the SOAP endpoint using the context information available in the form
                   /// or HTML Web resource.
                   ///</summary>
                   var ServicePath = "/XRMServices/2011/Organization.svc/web";
                   var serverUrl = "";
                   if (typeof GetGlobalContext == "function") {
                       var context = GetGlobalContext();
                       serverUrl = context.getServerUrl();
                   }
                   else {
                       if (typeof Xrm.Page.context == "object") {
                             serverUrl = Xrm.Page.context.getServerUrl();
                       }
                       else
                       { throw new Error("Unable to access the server URL"); }
                       }
                      if (serverUrl.match(/\/$/)) {
                           serverUrl = serverUrl.substring(0, serverUrl.length - 1);
                       }
                       return serverUrl + ServicePath;
                   },
               caseDupRequest: function () {
                   var requestMain = ""
                   requestMain += "Content-Type: text/xml; charset=utf-8";
                   requestMain += "SOAPAction: http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/RetrieveMultiple";
                   requestMain += "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">";
                   requestMain += "  <s:Body>";
                   requestMain += "    <RetrieveMultiple xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";
                   requestMain += "      <query i:type=\"a:QueryExpression\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\">";
                   requestMain += "        <a:ColumnSet>";
                   requestMain += "          <a:AllColumns>false</a:AllColumns>";
                   requestMain += "          <a:Columns xmlns:b=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">";
                   requestMain += "            <b:string>incidentid</b:string>";
                   requestMain += "            <b:string>createdon</b:string>";
                   requestMain += "            <b:string>title</b:string>";
                   requestMain += "            <b:string>customerid</b:string>";
                   requestMain += "          </a:Columns>";
                   requestMain += "        </a:ColumnSet>";
                   requestMain += "        <a:Criteria>";
                   requestMain += "          <a:Conditions>";
                   requestMain += "            <a:ConditionExpression>";
                   requestMain += "              <a:AttributeName>customerid</a:AttributeName>";
                   requestMain += "              <a:Operator>Equal</a:Operator>";
                   requestMain += "              <a:Values xmlns:b=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">";
                   requestMain += "                <b:anyType i:type=\"c:guid\" xmlns:c=\"86000117-63dd-e211-9cba-080027cac729http://schemas.microsoft.com/2003/10/Serialization/\">86000117-63dd-e211-9cba-080027cac729</b:anyType>";
                   requestMain += "              </a:Values>";
                   requestMain += "            </a:ConditionExpression>";
                   requestMain += "            <a:ConditionExpression>";
                   requestMain += "              <a:AttributeName>createdon</a:AttributeName>";
                   requestMain += "              <a:Operator>Today</a:Operator>";
                   requestMain += "              <a:Values xmlns:b=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\" />";
                   requestMain += "            </a:ConditionExpression>";
                   requestMain += "          </a:Conditions>";
                   requestMain += "          <a:FilterOperator>And</a:FilterOperator>";
                   requestMain += "          <a:Filters />";
                   requestMain += "        </a:Criteria>";
                   requestMain += "        <a:Distinct>false</a:Distinct>";
                   requestMain += "        <a:EntityName>incident</a:EntityName>";
                   requestMain += "        <a:LinkEntities />";
                   requestMain += "        <a:Orders />";
                   requestMain += "        <a:PageInfo>";
                   requestMain += "          <a:Count>0</a:Count>";
                   requestMain += "          <a:PageNumber>0</a:PageNumber>";
                   requestMain += "          <a:PagingCookie i:nil=\"true\" />";
                   requestMain += "          <a:ReturnTotalRecordCount>false</a:ReturnTotalRecordCount>";
                   requestMain += "        </a:PageInfo>";
                   requestMain += "        <a:NoLock>false</a:NoLock>";
                   requestMain += "      </query>";
                   requestMain += "    </RetrieveMultiple>";
                   requestMain += "  </s:Body>";
                   requestMain += "</s:Envelope>";
                   var req = new XMLHttpRequest();
                   req.open("POST", SDK.test54._getServerUrl(), false)
                   req.setRequestHeader("Accept", "application/xml, text/xml, */*");
                   req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
                   req.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
                   req.send(requestMain);
                   //work with the response here
                  var strResponse = req.responseXML.xml;
                   alert(strResponse.toString());
               },
     __namespace: true
    };

    Abhishek



    • Edited by Abhisinha17 Saturday, July 13, 2013 1:36 PM update
    Saturday, July 13, 2013 1:27 PM
  • function AbortSave() {
        event.returnValue = false;
        return false;
    }

    Call above JavaScript function to cancel Save on the form.
    Monday, July 15, 2013 3:41 PM
  • With the code context.getEventArgs().preventDefault();

    When the user clicks cancel, is there a way for more code to be executed?

    Thanks, Shaun


    S.Harrison

    Thursday, June 05, 2014 3:15 PM
  • Make your own prompt window

    Regards Faisal

    Friday, June 06, 2014 12:48 PM