Valid to field should display message when is set to more then 1 year

jean-pauldehaas
Tera Guru

Hi,

i have a requirements as follows:

1: User will be allowed to set the date 1 year from the date of submission. Date cannot less or exceed the 1 year timeline.

2: Users with Knowledge manager roles can modify the "Valid to" variable dates and can select any future date.

 

for the first one we have a script already in place but its not doing anything:

-------------------------------------------------------------------------------------------------------------

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
  
    var cdt = g_form.getValue('valid_to'); //First Date/Time field
var dttype = 'day'; //this can be day, hour, minute, second. By default it will return seconds.

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

function doSomething(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
//alert(answer);
if(answer > 365 )
{
g_form.showFieldMsg('valid_to','The date needs to be within the next year.','error');
}
}

    }

-------------------------------------------------------------------------------------------------------------------------------

 

please help

1 ACCEPTED SOLUTION

SumanthDosapati
Mega Sage
Mega Sage

Hi,

To validate that, without client script you can just do it with UI policy as below

Condition as below:

find_real_file.png

 

In Script tab :

find_real_file.png

 

Mark as correct and helpful if it solved your query.

Regards,
Sumanth

 

View solution in original post

10 REPLIES 10

Aman Kumar S
Kilo Patron

Are you getting alert in your client script?

Try changing your script tp:

if(Number(answer) > 365 )
{
g_form.showFieldMsg('valid_to','The date needs to be within the next year.','error');
}

Also, share your script include, so it can be debugged

Best Regards
Aman Kumar

this is the script include:

 

var ClientDateTimeUtils = Class.create();
ClientDateTimeUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
 
    
    checkDateEnteredPast: function() {
            
        // Check date to validate it is in the past
        
        var answer = "";
        
        // Date selected by User, date/time value, and only date value
        var selectedDateTime = new GlideDateTime(this.getParameter('sysparm_date'));
        var selectedDate = selectedDateTime.getDate();                   
        
        // Current date/time, date/time value, and only date value
        var nowDateTime = new GlideDateTime(gs.nowDateTime());
        var nowDate = nowDateTime.getDate();                             
        
        // if date entered is after the current date, is the current date, or is empty, return false
        if ((selectedDate >= nowDate) || (selectedDate == null)) {
            answer = false;
        } else {
            answer = true;
        }
        return answer;
    },
    
    type: 'ClientDateTimeUtils'
});

So you are returning true or false from your script, how are you comparing with 365 in client script?

Best Regards
Aman Kumar

how do you mean ?