locked
Data is not saving if the field has on change event ?!! RRS feed

  • Question

  • Hello guys ,

         We are using MS CRM 2011 online, In opportunity , one change "projected order value" field a java script will be triggered to convert the currency into dollars . it works fine if the user tabs out after entering  the value. on  saving, data will be saved properly.  Problem occurs if the user don't' tab out and directly save the record. !!  it wont' save the value in dollar field.. 

    After  multiple checking it seems that, it is happening in multiple fields which has on-change event.. data is not saving if they  user directly opens the record and enter the data only in that filed which has on change event and saves without tabbing out.. 

    I put debugger on-change event.. surprisingly on-change event scripts triggers when you directly click on save button without tabbing out from the field.  

    May be i'm missing out something very basic.. can you guys please guide me ??

    Monday, August 26, 2013 5:52 AM

All replies

  • I cannot reproduce this... maybe you have some JavaScript setting the field submit mode to never? Or some onsave JavaScript disabling fields? Try disabling all JavaScript if you're not sure.

    Anyway, you can add an onsave event after any existing onsave events which fires this code:

    document.activeElement.blur();

    That will ensure the active element is 'blurred' or loses focus before the form is saved, which should trigger the onchange and save the value.

    Hope that helps

    Paul


    If my response helped you find your answer please show your thanks by taking the time to "Mark As Answer" and "Vote As Helpful".

    Twitter LinkedIn Facebook Blog Magnetism

    Monday, August 26, 2013 6:44 AM
  • I cannot reproduce this... maybe you have some JavaScript setting the field submit mode to never? Or some onsave JavaScript disabling fields? Try disabling all JavaScript if you're not sure.

    Anyway, you can add an onsave event after any existing onsave events which fires this code:

    document.activeElement.blur();

    That will ensure the active element is 'blurred' or loses focus before the form is saved, which should trigger the onchange and save the value.

    Hope that helps

    Paul


    If my response helped you find your answer please show your thanks by taking the time to "Mark As Answer" and "Vote As Helpful".

    Twitter LinkedIn Facebook Blog Magnetism

    Yes.. I'm trying to save the read-only filed. I've added setsubmit mode("always").. but still not saving.. There are some other script on the same on-change event to doing some calculation.. 

    If the user follow the rules and tab out of the "Projected order value" and then click on save button .. it works just fine.. both the filed value will be saved..



    • Edited by Pradeep47 Monday, August 26, 2013 7:38 AM
    Monday, August 26, 2013 7:33 AM
  • Hi,

    When are you setting the Submit Mode of the Projected Order Value field? On load or on change of the Projected Order Value field? If you are doing it on change, try moving the setSubmitMode call to on load and see if you get the same behaviour.


    Michael Palmer
    xRMPalmer     @MJFPalmer     Rockstar365

    Monday, August 26, 2013 7:45 AM
  • Hi,

    When are you setting the Submit Mode of the Projected Order Value field? On load or on change of the Projected Order Value field? If you are doing it on change, try moving the setSubmitMode call to on load and see if you get the same behaviour.


    Michael Palmer
    xRMPalmer     @MJFPalmer     Rockstar365

    I had it on-save.. i moved setSumbit mode to onchange and then to on-load.. still the same issue :(

    This is getting crazy.. 

    on saving the record, for a split second i see the updated value gets displayed in read-only field.. but after refreshing it keeps the old value.. new value is not getting saved..

    Nothing is been updating on-load event.. 


    • Edited by Pradeep47 Monday, August 26, 2013 9:15 AM
    Monday, August 26, 2013 9:12 AM
  • Hi,

    Can you please try temporarily setting the field to not be read-only and seeing if the value now save correctly. This will at least confirm that the issue is something to do with the read-only field (and not something in a plugin or something).


    Michael Palmer
    xRMPalmer     @MJFPalmer     Rockstar365

    Monday, August 26, 2013 9:16 AM
  • It's a bad idea to set submit mode to always, as it means the value will be submitted to the database with every update to the form, even if the value hasn't changed. And in this case, it appears the submitMode is not being set correctly, and so the readonly field is not being submitted at all.

    A better way to do it is run the setSubmitMode onsave only, and only set it to 'always' if the field value has changed. You can do this on a field-by-field bases, or just run the following code which will do everything:

    function setSubmitAll() {
     Xrm.Page.data.entity.attributes.forEach(function (attribute, index) {
      if (attribute.getIsDirty()) {
       attribute.setSubmitMode("always");
      }
     });
    }

    Hope that helps

    Paul


    If my response helped you find your answer please show your thanks by taking the time to "Mark As Answer" and "Vote As Helpful".

    Twitter LinkedIn Facebook Blog Magnetism

    Monday, August 26, 2013 9:35 AM
  • Hi,

    Can you please try temporarily setting the field to not be read-only and seeing if the value now save correctly. This will at least confirm that the issue is something to do with the read-only field (and not something in a plugin or something).


    Michael Palmer
    xRMPalmer     @MJFPalmer     Rockstar365

    Thanks that was a good call..which will ensure this is nothing to do with read only field..nonetheless, Filed is not getting saved even after removing re-only field property . 




    • Edited by Pradeep47 Monday, August 26, 2013 9:38 AM
    Monday, August 26, 2013 9:36 AM
  • It's a bad idea to set submit mode to always, as it means the value will be submitted to the database with every update to the form, even if the value hasn't changed. And in this case, it appears the submitMode is not being set correctly, and so the readonly field is not being submitted at all.

    A better way to do it is run the setSubmitMode onsave only, and only set it to 'always' if the field value has changed. You can do this on a field-by-field bases, or just run the following code which will do everything:

    function setSubmitAll() {
     Xrm.Page.data.entity.attributes.forEach(function (attribute, index) {
      if (attribute.getIsDirty()) {
       attribute.setSubmitMode("always");
      }
     });
    }

    Hope that helps

    Paul


    If my response helped you find your answer please show your thanks by taking the time to "Mark As Answer" and "Vote As Helpful".

    Twitter LinkedIn Facebook Blog Magnetism

    Thanks paul, now I see that this issue is nothing to do with read-only field, I removed read-only option for a while and tested it.. and again.. data is not saving in the "projected order value in($)" field.

    • Edited by Pradeep47 Monday, August 26, 2013 9:43 AM
    Monday, August 26, 2013 9:42 AM
  • Hi,

    Ok, do you have any plugins running against this entity or any other code that could be incorrectly setting the value of the field?


    Michael Palmer
    xRMPalmer     @MJFPalmer     Rockstar365

    Monday, August 26, 2013 9:44 AM
  • You said it's happening in multiple fields... is this still the case?

    Now that the field is not read only, can you try editing the field manually, and see if that saves? That will rule out any server side code.

    Then once you've identified it's a client side problem, you can check your function that's setting the value to make sure it's setting the correct data type etc.

    If you're not sure, post your code.

    Paul


    If my response helped you find your answer please show your thanks by taking the time to "Mark As Answer" and "Vote As Helpful".

    Twitter LinkedIn Facebook Blog Magnetism

    Monday, August 26, 2013 10:07 AM
  • Hi,

    Ok, do you have any plugins running against this entity or any other code that could be incorrectly setting the value of the field?


    Michael Palmer
    xRMPalmer     @MJFPalmer     Rockstar365

    Checked all plugin on opportunity entity nothing is updating this field...

    Monday, August 26, 2013 10:11 AM
  • Hi,

    It would be good if you can provide the script. Till then you can write a function that will be triggered whenever user clicks on Save button. this function will do all conversion what you want.

    Let me know if it helps you.


    Monday, August 26, 2013 11:22 AM
  •        

     Okay , here is my part of code where i'm setting the value ...  

             var projectedordervaluecurrencyname = "new_projectedordervalue"; // currency field
            var projectedordervaluecurrency = Xrm.Page.getControl(projectedordervaluecurrencyname);
            var projectedordervaluecurrencyattribute = projectedordervaluecurrency.getAttribute();

            TransactionCurrencyValueInUSD = (Math.round((TransactionCurrencyValueInRupees / USDValueInRupees) * Math.pow(10, 3)) / Math.pow(10, 3)) * projectedordervaluecurrencyattribute.getValue(); //  calculation logic

            var setProjectedOrderValueCurrencyName = "new_projectedordervalueindollar"; // Decimal filed
            var setProjectedOrderValueCurrency = Xrm.Page.getControl(setProjectedOrderValueCurrencyName);
            var setProjectedOrderValueCurrencyAttribute = setProjectedOrderValueCurrency.getAttribute();

            setProjectedOrderValueCurrencyAttribute.setValue(TransactionCurrencyValueInUSD); // here i'm setting the filed value

    For some reason i was not able to set he value , like normal i do for any other field... so I had to do this way.. 


    but the thing is, code is not braking if i tab out of the filed and then save.. 
    only if i directly save without tabbing out.. then it wont's save..

    Thanks..

    @Paul yes, I can edit and save the filed value manually after removing read-only property of the field..I've done all the checks, disabled all the scripts on-load, on save, on change except this.. and then tried to save, same issue.. so.. now i guess. if there is a problem it's only with this code... 




    • Edited by Pradeep47 Monday, August 26, 2013 11:39 AM
    Monday, August 26, 2013 11:33 AM
  • Try adding this to your onsave event:

    document.activeElement.blur();
    Xrm.Page.getAttribute("new_projectedordervalue").fireOnChange();

    If that works then you can tidy it up.

    Paul


    If my response helped you find your answer please show your thanks by taking the time to "Mark As Answer" and "Vote As Helpful".

    Twitter LinkedIn Facebook Blog Magnetism

    Monday, August 26, 2013 11:43 AM
  • Try adding this to your onsave event:

    document.activeElement.blur();
    Xrm.Page.getAttribute("new_projectedordervalue").fireOnChange();

    If that works then you can tidy it up.

    Paul


    If my response helped you find your answer please show your thanks by taking the time to "Mark As Answer" and "Vote As Helpful".

    Twitter LinkedIn Facebook Blog Magnetism

    No paul, after adding this code , problem still persisting... 


    • Edited by Pradeep47 Monday, August 26, 2013 11:54 AM
    Monday, August 26, 2013 11:53 AM
  • Hi,

    Just on save event add this function

    function SetProjectOrderValue()

    {

    Xrm.Page.getAttribute("new_projectedordervalue").fireOnChange();

    }

    See if it helps you. Call this function whenever user hits Save button.

    Let me know if it helps.

    Tuesday, August 27, 2013 7:20 AM