Set field value to yyyymm of date field in form

Home Forums Classic DFFS Set field value to yyyymm of date field in form

Viewing 4 reply threads
  • Author
    Posts
    • #33720
      Paul Heffner
      Participant

        Hi! I want to set a rule that sets a field value based off a date field. I want the field value to be yyyymm (202105) based off the date a user enters.

        I see you can do somwthing like {Timestamp[yyyy]} but that doesnt work for an internal field name

        • This topic was modified 3 years, 5 months ago by Paul Heffner.
      • #33730
        Alexander Bautz
        Keymaster

          Hi,
          There is unfortunately no such formatting option built in, but you can do it with some custom js.

          Add this to your Custom JS:

          function setCustomDateStr(){
              var d = spjs.utility.getDateFieldAsDateObject("YOUR_DATE_FIELD");
              var dStr = d.getFullYear() + String(d.getMonth() + 1).padStart(2,"0");
              setFieldValue("YOUR_TEXT_FIELD", dStr);
          }

          Now set up a rule to trigger on change on your date field, and in the field “Run these functions / evaluate these rules” add the function name setCustomDateStr.

          Change YOUR_DATE_FIELD and YOUR_TEXT_FIELD to match your fields.

          Alexander

        • #33754
          Paul Heffner
          Participant

            I am getting the error the objext doesnt support padStart

            • #33756
              Alexander Bautz
              Keymaster

                I forgot padStart isn’t available in IE – try it like this instead:

                function setCustomDateStr(){
                    var d = spjs.utility.getDateFieldAsDateObject("YOUR_DATE_FIELD");
                    var month = d.getMonth() + 1;
                    if(month < 10){
                        month = "0" + month;
                    }
                    var dStr = d.getFullYear() + month;
                    setFieldValue("YOUR_TEXT_FIELD", dStr);
                }

                Alexander

            • #35405
              Brett Ross
              Participant

                I’m trying to use this but, it is giving me an error. Here’s the code I’m using:

                function setCustomDateStr(){
                var d = spjs.utility.getDateFieldAsDateObject(“DateServicesProvided”);
                var dStr = d.getFullYear() + String(d.getMonth() + 1).padStart(2,”0″);
                setFieldValue(“YearMonth”, dStr);
                }

                When the date changes, I get an error that says the “TypeError: d.getFullYear is not a function”. If I click OK, the function works and the correct number is filled in. But how do I get rid of that error?

              • #35407
                Alexander Bautz
                Keymaster

                  Try adding an alert (or console.log) to see what the value for d is.

                  It might be that you need a little timeout to let the picker “finish” – try it like this:

                  function setCustomDateStr(){
                      setTimeout(function(){
                          var d = spjs.utility.getDateFieldAsDateObject("DateServicesProvided");
                          var dStr = d.getFullYear() + String(d.getMonth() + 1).padStart(2,"0");
                          setFieldValue("YearMonth", dStr);
                      },100);
                  }

                  Alexander

              Viewing 4 reply threads
              • You must be logged in to reply to this topic.