Saving Contents of Read-Only Fields

Beantwortet Saving Contents of Read-Only Fields

  • Freitag, 11. Mai 2012 23:51
     
     

    I have written a JScript routine that does calculations based on the contents and real-time changes in a number of source fields and posts the results to a target field.  Ideally, this target field should be set as read-only.  Because I want to use the value in the target field latter in a roll-up to it's parent, I want the calculated value saved to the DB.  Through trial-and-error, I learned that the values in read-only fileds are not saved to the databas when the form's Save, Save+Close, Save+New, etc. buttons are clicked.

    Does someone have a solution where I can save the contents of these calculated fields while keeping them read-only?

    Thanks for any help.

Alle Antworten

  • Samstag, 12. Mai 2012 00:53
     
     Beantwortet Enthält Code

    Assuming you're using CRM 2011, you must call the read-only fields' setSubmitMode method:

    Xrm.Page.data.entity.attributes.get("new_readonlyfield").setValue(28.3);
    Xrm.Page.data.entity.attributes.get("new_readonlyfield").setSubmitMode("always");
    

    In CRM 4.0 the equivalent is:

    crmForm.all.new_readonlyfield.DataValue = 28.3;
    crmForm.all.new_readonlyfield.ForceSubmit = true;
    



    --pogo (pat) @ pogo69.wordpress.com

    • Als Antwort markiert Kahuna2000 Samstag, 12. Mai 2012 01:03
    •  
  • Samstag, 12. Mai 2012 00:57
     
     

    Thanks Pat.  So can I do this on the Form Load event and have it persist for the session or do I have to do it as a part of the calc function itself?

  • Samstag, 12. Mai 2012 01:02
     
     
    Whenever you set a field's value, you force its submission; in that order, as per my sample code.  I haven't seen your code, but it sounds like you will do it as part of the calculation.

    --pogo (pat) @ pogo69.wordpress.com

  • Samstag, 12. Mai 2012 01:03
     
     
    Got it.  Thanks so much.
  • Donnerstag, 19. Juli 2012 14:49
     
     
    And If is a Read-Only Required field, how can i set it? I dont wanna to submit a null value to DB.

    Js

  • Donnerstag, 19. Juli 2012 15:00
     
     

    Js,

    I'm not sure what you mean.  It does not seem to make much sense to have a required field be read-only as you would never be able to save the record.  I guess you could set the read-only/required field in code if that makes sense in your situation.  It would be done as illustrted by pogo earlier:

       Xrm.Page.data.entity.attributes.get("new_readonlyfield").setValue(<some value here>);
       Xrm.Page.data.entity.attributes.get("new_readonlyfield").setSubmitMode("always");

    I hope this helps.

  • Donnerstag, 19. Juli 2012 15:11
     
     

    Hi Kahuna,

    I get the value of this field from an event, depending on the values of Options Set

    I would like be read-only for the user don't modify it manually.


    Js

  • Donnerstag, 19. Juli 2012 17:43
     
     

    Js,

    Ok then, you should be able to insert your value in the above code at the point marked "<some value here>".

    K

  • Freitag, 20. Juli 2012 08:38
     
     

    Hi Kahuna,

    thank you for your answer. I think the best way for me is:

    Uncheck as Only-Read field. Disable the field in Javascript and check if the value != null in OnSave method.


    Js