How do I check to see if a field is date / datetime in Journey JS.
I found a solution but curious if there is a better solution.
assuming I have a model called opportunity
var testField = "action_date";
var logValue;
var theField = view.opportunity.type.attributes[testField];
var fieldIsDay = theField.type.isDay;
if (fieldIsDay) {
logValue = getDateFormat(view.opportunity[testField]);
} else {
logValue = view.opportunity[testField].toString();
}
Your solution will work for fields that are defined as type="date"
on the data model. The code view.opportunity.type.attributes
is essentially looking at the data model definition for that object's fields and fields that are defined as type="date"
have a special type accessor called isDay
.
However, you can also evaluate the type of any of the fields for an object using a bit of a workaround (hack), as follows:
var fieldType = JSON.parse(JSON.stringify(DB.model.first().type.attributes[field])).type; // fieldType will be a string that maps to one of the supported data types you can define in your data model, ie 'text', 'single-choice', 'date', 'boolean', etc.
The reason you need to first stringify
the properties and then parse
it again is allow you to evaluate the type
of the field
as a string, not as a Type
object.
1 Like