- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-03-2023 09:53 AM
HI All
I have requirement to restrict user to enter past date in the Delivery date field in a record producer. For this I have already a script include and that triggered from a BR. Now I want to call this script include using client script. Please help on the scripting part to call this script include using client script.
Script Include:
var TransferOrderDateTimeAjax = Class.create();
TransferOrderDateTimeAjax.prototype = Object.extendsObject(AbstractAjaxProcessor , {
/*
* Compares two dates coming both from the client's timezone
*/
compareDatesAjax: function() {
var startDate = new GlideDateTime();
startDate.setDisplayValue(this.getParameter('sysparm_startDate'));
var endDate = new GlideDateTime();
endDate.setDisplayValue(this.getParameter('sysparm_endDate'));
return this.compareDates(endDate, startDate);
},
/*
* Compares two dates from the same timezone
*/
compareDates: function(/*GlideDateTime*/ date1, /*GlideDateTime*/ date2) {
var diff = gs.dateDiff(date1, date2, true);
return (diff <= 0);
},
/*
* Compares a date in the server's timezone to now in the server timezone too
*/
isDateBeforeNow: function(/*GlideDateTime*/ date) {
var now = new GlideDateTime();
now.setDisplayValue((new Date()).toString());
return this.compareDates(now, date);
},
type: 'TransferOrderDateTimeAjax'
});
BR which call this script include:
var timeComp = new TransferOrderDateTimeAjax();
if (timeComp.isDateBeforeNow(current.delivery_by_date)) {
gs.addErrorMessage(gs.getMessage('Choose a delivery date in the future'));
current.setAbortAction(true);
}
I want same error message should populate, when a user enter past date in the delivery date field and that script include should call from client script. Please help in the scripting part please.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-04-2023 01:37 AM
is the delivery date a field or variable on the record producer form? You can create a UI Policy for this no complex script required example below
Harish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-03-2023 01:09 PM
Your client script will be something like this:
var ga = new GlideAjax('TransferOrderDateTimeAjax');
ga.addParam('sysparm_name', 'compareDatesAjax');
ga.addParam('sysparm_startDate, <date>);
ga.addParam('sysparm_endDate, <date>);
ga.getXML(yourCallback);
function yourCallback(response) {
var res = JSON.parse(response.responseXML.documentElement.getAttribute("answer"));
// g__form methods here et cet
}
If you will be calling it through ajax, consider modifying your include's methods to return an object instead of a value. Something like this:
{
"result":-1,
"msg":"too low!"
}
You might find this more intuitive to deal with in your callback, and your code easier to understand. Stringify the object before returning - I don't think this is done for you.
Some of my includes have an explicit conversion to String on the params, like this, I don't remember why.
var selectedValue = String(this.getParameter('sysparm_subtopic'));
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-04-2023 01:20 AM
Hi Tomas
I tried with your script but not getting the expected output. In the record producer when I am selecting the past date, it is not throwing any error message. When clicking on the submit button, I am also able to submit the request.
OnChange Client script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('TransferOrderDateTimeAjax');
ga.addParam('sysparm_name', 'compareDatesAjax');
ga.addParam('sysparm_startDate', 'date');
ga.addParam('sysparm_endDate', 'date');
ga.getXML(CallBackFunction);
function CallBackFunction(response) {
var res = JSON.parse(response.responseXML.documentElement.getAttribute("answer"));
g_form.addErrorMessage('Choose a delivery date in the future');
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-04-2023 01:37 AM
is the delivery date a field or variable on the record producer form? You can create a UI Policy for this no complex script required example below
Harish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-04-2023 01:48 AM
That was awesome. It is working as per my requirement.
Thank you very much.