Restrict a date field value in record producer.

Prasnajeet1
Giga Guru

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.

1 ACCEPTED SOLUTION

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

HarishKM_0-1691138184624.png

 

HarishKM_1-1691138200597.pngHarishKM_2-1691138213272.png

 

Regards
Harish

View solution in original post

5 REPLIES 5

thomaskennedy
Tera Guru

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'));

 

 

 

 

 

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

}

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

HarishKM_0-1691138184624.png

 

HarishKM_1-1691138200597.pngHarishKM_2-1691138213272.png

 

Regards
Harish

That was awesome. It is working as per my requirement.

Thank you very much.