javascriptyoutrack

YouTrack Workflows test if Custom Field is a Date


How do you check if a Custom Field in a YouTrack Workflows script is a Date?

If I access issue.fields['my-field'], it turns out to be a simple timestamp as a number both for a Date and a DateTime. How do I test if my-field is a Date and not a number?

I tried accessing its fields like dateTimeType and dateType, but since it's just a number, I got undefined for both. Simply guessing if it's a date based on the number's value is also not gonna work in my case, because I need to be 100% sure.

I need a fully dynamic solution that would work without specifying requirements. All I have is basically this:

function example(ctx) {
  const { issue } = ctx;
  const { fields } = issue;
  Object.entries(fields).forEach(f => /* is f a Date? */)
}

Solution

  • Assuming that you need to find a date-typed field named "Date":

    const dateField = issue.project.fields.find(function(field) {
      const fieldName = field.name;
      const fieldTypeName = field.typeName;
      return fieldName === "Date" && fieldTypeName === "date";
    });
    

    The trick here is to look for issue.project.fields instead of issue.fields. This will return a set of ProjectCustomFields, and the find method can then be used to look for a match based on your conditions.