Date validation in import API onbefore Transform script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2023 11:49 PM
Hi,
I have a import API which sends in the date&time as string and gets mapped to the date&time field. The date sent in the payload is getting mapped to the target field correctly.
I want to include a validation as in the payload they are able to send the past date, the validation should be like it should only allow the future date.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2023 09:26 PM
Hi @Jake Adams ,
if (action == 'insert') {
ignore = true;
} else {
var sourceFieldName = source.u_date;
if (sourceFieldName == '') {
error = true;
error_message = 'The field is blank';
} else {
var sourceDate = source.u_date;
if (sourceDate) {
var gdt = new GlideDateTime(sourceDate);
var currentDate = new GlideDateTime();
if (gdt < currentDate) {
error= true;
error_message = 'Past date has been sent in, kindly send a future date.';
} else
target.datefield_toupdatetargettable = sourceDate;
}
} else {
// If no date is provided, update other fields without date validation
target.<otherFieldToUpdate> = source.<otherFieldToUpdate>;
// Add other fields as needed
}
}
}
Mark it helpful and solution proposed if it serves your purpose.
Thanks,
Anand