locked
Javascript Changes don't save to form RRS feed

  • Question

  • I have just discovered today that any changes that get made to a form based off of onchange jscript event aren't saved. For example, I have a due date field if it is not null then the estimated close date is required. It works fine. I set a due date and the estimated close date becomes required. When I save the form the estimated close date goes back to being not required... I set up the form to then onload run the same event to see if that might solve the issue. Instead now the estimated close date field is just always required. I'm confused why this is happening. Below is some very simple code I use. It works but then the changes don't save when I hit save on the form.

    Thanks for the help!

    -Trevor

    function closedate()
    {
    if(Xrm.Page.getAttribute("new_due_date").getValue != null)
    {
    Xrm.Page.getAttribute("estimatedclosedate").setRequiredLevel("required");
    }
    else
    {
    Xrm.Page.getAttribute("estimatedclosedate").setRequiredLevel("none");
    }
    }

    Friday, March 1, 2013 6:32 PM

Answers

  • OK  I see. You also need to to add some code to the Form's OnLoad event to handle this. You setting the field to required only effects the form in its current state, once you reload it doesn't remember it needs to be required unless another event fires off.

    Something like this - when the form is a normal update form -(not being created, readonly, or bulk edit) if will check that field again set the requirement level.

    function OnLoad() {
        if (Xrm.Page.ui.getFormType() == 2) {
            if (Xrm.Page.getAttribute("new_due_date").getValue() != null) {
                Xrm.Page.getAttribute("estimatedclosedate").setRequiredLevel("required");
            } else {
                Xrm.Page.getAttribute("estimatedclosedate").setRequiredLevel("none");
            }
        }
    }
    


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Friday, March 1, 2013 7:51 PM
    Moderator
  • It should still work without the form check in this case. I personally just get in the habit of checking which type of form I want the code to run against as a bit of safety net. You don't always necessarily want all code to run every time the form opens. 

    Say for example you have a lookup field on the form that is required and you are going to use the value in a script. Ideally, you would check the value to ensure it isn't null before trying to use it but say you forgot to do that. If the form had already been saved with the field populated the null check isn't truly required. But if that same code runs when you are first creating the record and no fields are populated, you would get an error trying to get the value. In this case, I only want the code to run when the form is in update mode.

    Another example is pre-populating a field. Say I have a date field and I want to default it to today's date. If the code ran every time the the form opened it would overwrite and previous values. But if I limit it to run only when the form is in create mode, it will only get populated when the record is created. 

    Additionally based on the form type - certain code might just not work and throw an error. You might not always be in the habit of testing your code on all the different form types ( :-) I'm not), as a result you may get a user reporting an error on the form. Then when you look at it, you don't see a problem, only to find out later they were looking at an inactive record (read-only form) because they didn't know enough to mention that fact. 

    getFormType



    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Friday, March 1, 2013 10:23 PM
    Moderator

All replies

  • Try:

    function closedate() {
        if (Xrm.Page.getAttribute("new_due_date").getValue() != null) {
            Xrm.Page.getAttribute("estimatedclosedate").setRequiredLevel("required");
        }
        else {
            Xrm.Page.getAttribute("estimatedclosedate").setRequiredLevel("none");
        }
    }
    Might have just been missing the "()" after .getValue


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Friday, March 1, 2013 6:36 PM
    Moderator
  • I am still having the same issue. The code works when the due date changes and the other field becomes required. As soon as I save the form though the field is no longer required.

    It's also interesting I was misisng those () and it still worked. I've been using a Lint service to help catch those small errors and it didn't catch that either.

    Thanks for the help.

    -Trevor

    Friday, March 1, 2013 7:29 PM
  • OK  I see. You also need to to add some code to the Form's OnLoad event to handle this. You setting the field to required only effects the form in its current state, once you reload it doesn't remember it needs to be required unless another event fires off.

    Something like this - when the form is a normal update form -(not being created, readonly, or bulk edit) if will check that field again set the requirement level.

    function OnLoad() {
        if (Xrm.Page.ui.getFormType() == 2) {
            if (Xrm.Page.getAttribute("new_due_date").getValue() != null) {
                Xrm.Page.getAttribute("estimatedclosedate").setRequiredLevel("required");
            } else {
                Xrm.Page.getAttribute("estimatedclosedate").setRequiredLevel("none");
            }
        }
    }
    


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Friday, March 1, 2013 7:51 PM
    Moderator
  • That worked. Thanks for your help again! I have one follow up question if you don't mind. I see what you did and its pretty much the same except that you are checking for the form type. I don't see why you have to check the form type for it to work. Why wouldn't running that code without that if statement onload work? I basically tried that before using the same code (without checking the form type) and setting it at onload but it always set the estimated close date to required upon loading the form no matter what. I'm just trying to better understand this for future reference. I'm trying to teach myself/learn all of this as I go.

    Thanks,

    Trevor

    Friday, March 1, 2013 9:36 PM
  • It should still work without the form check in this case. I personally just get in the habit of checking which type of form I want the code to run against as a bit of safety net. You don't always necessarily want all code to run every time the form opens. 

    Say for example you have a lookup field on the form that is required and you are going to use the value in a script. Ideally, you would check the value to ensure it isn't null before trying to use it but say you forgot to do that. If the form had already been saved with the field populated the null check isn't truly required. But if that same code runs when you are first creating the record and no fields are populated, you would get an error trying to get the value. In this case, I only want the code to run when the form is in update mode.

    Another example is pre-populating a field. Say I have a date field and I want to default it to today's date. If the code ran every time the the form opened it would overwrite and previous values. But if I limit it to run only when the form is in create mode, it will only get populated when the record is created. 

    Additionally based on the form type - certain code might just not work and throw an error. You might not always be in the habit of testing your code on all the different form types ( :-) I'm not), as a result you may get a user reporting an error on the form. Then when you look at it, you don't see a problem, only to find out later they were looking at an inactive record (read-only form) because they didn't know enough to mention that fact. 

    getFormType



    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Friday, March 1, 2013 10:23 PM
    Moderator
  • Makes sense. Thank you for your time and explaining that. I'm going to go back and see if I can't figure out why I was having an error before when trying the onload event. Maybe something to do with I was using the same function (code) event for the onchange field and for the onload event. And I need two separate functions to do that. I appreciate the help and explanation, helps me out tremendously.

    -Trevor

    Friday, March 1, 2013 10:31 PM