Set field value to yyyymm of date field in form

Forums Dynamic Forms for SharePoint Set field value to yyyymm of date field in form

Viewing 2 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 1 week, 5 days 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

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