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 12:10 AM
Hi @Jake Adams ,
Use below script
var payloadDateString = source.field_name;
var payloadDate = new GlideDateTime(payloadDateString);
var currentDate = new GlideDateTime();
if (payloadDate<currentDate) {
ignore = true;
}else{
ignore = false;
}
Please mark it as helpful and solution proposed if it serves your purpose.
Thanks,
Anand
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2023 12:40 AM - edited 12-05-2023 12:43 AM
It's not working for me, even if I send the future date it's ignoring.
Also, I am adding the validation only for update and not insert, so even if I send the other payload without the date it's considering the date value in blank/past in the payload and ignoring it.
var sourceDate = source.u_date;
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 {
ignore = false;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2023 01:43 AM
Hi @Jake Adams ,
var sourceDate = source.u_date;
var gdt = new GlideDateTime(sourceDate);
var currentDate = new GlideDateTime();
if (gdt < currentDate) {
ignore=true;
error_message = "past date has been sent in, kindly send a future date";
} else {
target.datefield_toupdatetarget table=sourceDate;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2023 07:02 PM
Hi,
This is working now, if I provide the past date it's ignoring and throwing the error and if I give the future date the target field in getting set correctly
but again as I mentioned before this validation is only for update operation and not insert, so even if I send the other payload while updating without the date it's considering the date value as blank/past in the payload and ignoring the payload and throwing the same error:
eg.) If I send the below payload for updating the description, then it's ignoring the payload and throwing the date error: