Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Date validation in import API onbefore Transform script

Jake Adams
Tera Contributor

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.

5 REPLIES 5

Anand Kumar P
Giga Patron

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;
}

AnandKumarP_0-1701763816174.png


Please mark it as helpful and solution proposed if it serves your purpose.

Thanks,

Anand

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;
}

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;
}

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:

 

{
"u_number":"INCXXXX",
"u_description":"updating description"
}