Requirement to create onsubmit client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-18-2022 09:07 AM
I have a requirement to create a onsubmit client script
we are setting get-date using client script.If user changes the date to past then the form submission should be aborted.
eg:populated date is 01/06/2022----(today date+61 days)
if user changes date to eg:01/05/2022 then the form should not be submitted.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-18-2022 09:23 AM
you have two options.
1. You make the date field as mandatory and if user changes a date to past clear it, that way you can prevent user from submitting a past date.
2. You can also do a before BR that checks the date and aborts action if date is in the past.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-18-2022 09:34 AM
Hi Vishali,
It would be easier to recommend a solution if you could include more information on the situation and where exactly you wish to apply this.
Would this for example be in the Service Portal on a catalog item, or are you submitting a record within the backend and want to specify this check here?
It can of course be scripted, but if you are using a date/time field maybe you can use the following low-code solution;
- Create a UI Policy or a Catalog UI Policy in case of a cat item;
- In the condition specify '<your date/time field or variable> - before - Today'
-> Then Run Script
Clear the value of the field and inform the user.
For example in case of a catalog ui policy;
function onCondition(){
g_form.setValue('your variable', ''); //empty the field
g_form.showErrorBox('your variable' , "Invalid date, you cannot use a date in the past", true);
}
Hope this helps you with ideas to proceed.
Kind regards,
Fernando
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-18-2022 11:32 AM
As per my understanding, the ask is if user set a past date, then it won't let the user submit the form.
In this case, two scripts are required. One is script include getting the current date and comparing it with the date set by the user and the second script is a client script to restrict the user on form submission.
Script Include::
var dateTimeUtils = Class.create();
dateTimeUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
compareDate: function(){
var currentDate = new GlideDate();
var clientDate = this.getParameter('sysparm_date');
var dateDiff = parseInt(gs.dateDiff(currentDate,clientDate,true));
return dateDiff;
}
});
Client Script:
function onSubmit() {
var ajax = new GlideAjax('dateTimeUtils');
ajax.addParam('sysparm_name', 'compareDate');
ajax.addParam('sysparm_date', g_form.getValue("u_test_date"));
ajax.getXML(test);
function test(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.addInfoMessage("answer:: "+answer+" @@ "+typeof answer);
if (parseInt(answer) < 0) {
g_form.clearValue('u_test_date');
return false;
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-23-2022 04:11 AM
function onSubmit() {
var oldValue = g_form.getValue('cmdb_review_date');
var newValue = g_form.getValue('cmdb_review_date_new');
if (newValue != oldValue) {
var review_date = g_form.getValue('cmdb_review_date_new');
var ga = new GlideAjax('ValidateDate');
ga.addParam('sysparm_name', 'subtractDaysFromDate');
ga.addParam('sysparm_days', 61);
ga.addParam('sysparm_date', review_date);
ga.addParam('sysparm_old_date', oldValue);
ga.addParam('sysparm_new_date', newValue);
ga.getXML(checkDates);
function checkDates(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer == 'false')
{
alert('New CMDB Review Date must be at least 61 days from today and cannot be further in the future than the currently documented one');
g_form.submitted = false;
return false;
}
if(g_form.getValue('cmdb_review_date_new') == "")
{
g_form.setValue('cmdb_review_date_new', oldValue);
alert( 'New CMDB Review Date must be at least 61 days from today and cannot be further in the future than the currently documented one.');
return false;
}
else
{
return true;
}
}
}
}
on the catalog form am getting the alert but form is getting submitted.