Date/Time Field Restriction Record Producer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-12-2017 08:18 AM
Hi All,
I have a date/time field on a record producer asking when the incident first occurred.
I have seen that the only way yo restrict this so people cannot add a date in the future is to have a script which validates this between the current date and the date selected when the field changes and then have a pop up message to warn the user.
I'm just not sure on how to script this?
Any help is greatly appreciated.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-13-2017 12:58 AM
Hi,
You can achieve this with the help of an simple Script Include and an On Change Catalog Client Script as mentioned below:
1) Create a Script Include with script as mentioned below:
var DateValidation = Class.create();
DateValidation.prototype = Object.extendsObject(AbstractAjaxProcessor, {
validateDate3: function() {
var ActualDate = this.getParameter('sysparm_date');
return gs.dateDiff(gs.now(),ActualDate, true)/86400;
},
type: 'DateValidation'
});
2) Once the Script Include has been created then create a On Change Catalog Client Script on your Record Producer as mentioned below:
Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('DateValidation');
ga.addParam('sysparm_name','validateDate3');
ga.addParam('sysparm_date',g_form.getValue('date')); //Replace your Variable Name here in place of "date"
ga.getXML(ProcessResult);
function ProcessResult(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer > 0)
{
g_form.clearValue('date'); //Replace your Variable Name here in place of "date"
alert('Date should not be in the Future.');
}
}
//Type appropriate comment here, and begin script below
}
In the above screen shot "Date(date)" is the Variable Name for me. Replace with your Variable Name as mentioned above:
Result:
And it also clears out the Value of the Date Variable after the popup.
Hope this helps.Mark the answer as correct/helpful based on impact.
Regards,
Shloke
Regards,
Shloke
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-15-2017 03:10 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-15-2017 03:15 AM
My Bad. Forgot to mention that you need to check the "Client Callable" checkbox as true on the script include form as shown below:
Please marked the above checkbox as true and try the same script again.
Hope this helps.Mark the answer as correct/helpful based on impact.
Regards,
Shloke
Regards,
Shloke
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-15-2017 03:34 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-15-2017 04:15 AM
That's strange. It's working fine for me on my instance. Can you try the below steps by debugging our script to check what is coming in the logs:
1) In you script Include can you pass a Log statement and check the value as mentioned below:
Script:
var DateValidation = Class.create();
DateValidation.prototype = Object.extendsObject(AbstractAjaxProcessor, {
validateDate3: function() {
var ActualEndDate = this.getParameter('sysparm_date');
var res = gs.dateDiff(gs.now(),ActualEndDate, true)/86400;
gs.log('Value of res is' + res);
return res;
},
type: 'DateValidation'
});
.
2) Please update your client script as below and also pass the alert statement to check for the value getting rendered on the client side:
Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('DateValidation');
ga.addParam('sysparm_name','validateDate3');
ga.addParam('sysparm_date',g_form.getValue('date'));
ga.getXML(ProcessResult);
//Type appropriate comment here, and begin script below
}
function ProcessResult(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer > 0)
{
alert(answer);
g_form.clearValue('date');
alert('Date should not be in the Future.');
}
}
Please check and let me know the outcomes of the Log statement and alert Statement.
Regards,
Shloke
Regards,
Shloke
