Prevent Submission of Catalog Item With a Date in the Past

jmiskey
Kilo Sage

We have many Catalog Items that have a date field like "Effective Date", where the date entered cannot be in the past.  We have always handled it with a On Change Catalog Client Script, right on that date Variable, to do a GlideAjax call to a bunch of date functions to check/verify the date is not in the past, and this has always worked well.

 

However, with the most recent versions of ServiceNow, it now has/allows a "Save as Draft" option for Catalog Items.  So now people could enter an Effective Date of today, and then save the Catalog Item as a draft.  They could then re-open that draft tomorrow and submit it.  We do not want to allow that to happen, as tomorrow, the date they entered in the Effective Date field will be in the past, and therefore should not be allowed.  So I think we need to add similar logic that we have in the On Change Catalog Client Script to an On Submit Catalog Client Script.

 

When I tried doing that, it did not work.  It ran, but did not prevent the record from being submitted like it should.  From research, it appears that you cannot use GlideAjax calls in On Submit Catalog Client Scripts.  So what is the best way of handling this?

 

Here is the I attempted, which uses the same logic/GlideAjax call as my On Change Script:

function onSubmit() {
   //Needed due to the ability to "Save as Draft"

	var dndt = g_form.getValue('effective_date'); //First Date/Time field  
    var dttype = 'day'; //this can be day, hour, minute, second. By default it will return seconds.  

    var ajax = new GlideAjax('global.ClientDateTimeUtils');  
    ajax.addParam('sysparm_name','getNowDateTimeDiff');  
    ajax.addParam('sysparm_fdt', dndt);  
    ajax.addParam('sysparm_difftype', dttype);  
    ajax.getXML(doSomething);  

    function doSomething(response){  
        var answer = response.responseXML.documentElement.getAttribute("answer");  
        //check to see if request for old date (by seeing if difference between now and request date < -1)  
        alert(answer);
        if (answer<=-1) {
            alert("Effective Date cannot be a date in the past!");
            g_form.setValue('effective_date','');
			return false;  //disable submission
        }else{
			return true;
		}
    }  
   
}

Thanks

1 ACCEPTED SOLUTION

Zach Koch
Giga Sage
Giga Sage

Unless you need more data from the server, you can actually just prevent past dates through UI policy where it will clear out the value if it is in the past, which will prevent people from being able to save a draft like you mentioned, with the date in the past. Take a look at this post.

No Code date validations through (Catalog) UI Policies 

If this information helped resolve your issue, please remember to mark response correct and thumbs up to help future community members on this information, thanks!

View solution in original post

18 REPLIES 18

Did you try below:

function onSubmit() {
    /*if (g_scratchpad.isFormValid)
        return true;*/
    //Needed due to the ability to "Save as Draft"

    //var dndt = g_form.getValue('effective_date'); //First Date/Time field  
    //var dttype = 'day'; //this can be day, hour, minute, second. By default it will return seconds.  
alert('called')
    var ajax = new GlideAjax('global.UserQuery');
    ajax.addParam('sysparm_name', 'getUserIbnformation');
    ajax.addParam('sysparmUserID', g_user.userID);
    //ajax.addParam('sysparm_difftype', dttype);  
    ajax.getXMLAnswer(doSomething);
    return false;

    function doSomething(response) {
		alert('response');

        
        //check to see if request for old date (by seeing if difference between now and request date < -1)  
        alert(response);
        if (response == '') {
            alert("Effective Date cannot be a date in the past!");
            //g_form.setValue('effective_date','');
            return false; //disable submission
        } else {
			 //var actionName = g_form.getActionName();
        //g_scratchpad.isFormValid = true;
        //g_form.submit(actionName);
            return true;
        }
    }

}

No, I don't see how to retrofit my code into there.  So much is different, I cannot tell where I need to make changes.

amaradiswamy
Kilo Sage

Okay, hopefully below will work 

function onSubmit() {
   //Needed due to the ability to "Save as Draft"

	var dndt = g_form.getValue('effective_date'); //First Date/Time field  
    var dttype = 'day'; //this can be day, hour, minute, second. By default it will return seconds.  

    var ajax = new GlideAjax('global.ClientDateTimeUtils');  
    ajax.addParam('sysparm_name','getNowDateTimeDiff');  
    ajax.addParam('sysparm_fdt', dndt);  
    ajax.addParam('sysparm_difftype', dttype);  
    ajax.getXMLAnswer(doSomething);  
    return false;
    function doSomething(answer){  
       
        //check to see if request for old date (by seeing if difference between now and request date < -1)  
        alert(answer);
        if (answer<=-1) {
            alert("Effective Date cannot be a date in the past!");
            g_form.setValue('effective_date','');
			return false;  //disable submission
        }else{
			return true;
		}
    }  
   
}

Unfortunately not.  It always returns "FALSE".  If I enter in a future date, I do not get the Message Box, but it never submits the form.  It seems that the "return false" line above the function trumps all, and it can never return true.