Built something you're proud of? Tell the story. A quick G2 review of App Engine or Build Agent helps other developers see what's possible on ServiceNow. Share your experience.

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

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