create an alert message in the catalog based on the duration variable being more than 12

Community Alums
Not applicable

Hi Team,

 

I need alret message if select more then 12 hours in duration variable

 

I tried below client script and script include but it works 

 

Client script 

 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
   var duration = g_form.getValue('how_long_do_you_need_access');
 
   var ga = new GlideAjax('setlimitduration');
    ga.addParam('sysparm_name', 'setduration');    
    ga.addParam('sysparm_Duration', duration);
   
    ga.getXML(CheckDateValidation);
    }
 
function CheckDateValidation(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
   
    alert(answer);
 
}
 
script inculde:
 
var UtilsAjax = Class.create();
UtilsAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
   
    setduration: function() {
       
 
  var duration = new GlideDuration(this.getParameter('sysparm_Duration'));
       
        if (duration > 12) {
            return ('Alert: Duration selected is more than 12 hours!');
        }
       // return duration.getDisplayValue();

        //return this.getParameter('sysparm_Duration');
    },
 
    type: 'setlimitduration'
});
 
Can you please help on the issue 
 
Thank you
Siva
   

 

1 ACCEPTED SOLUTION

Hi @Community Alums ,

 

Please try the below:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var duration = g_form.getValue('how_long_do_you_need_access');

    var parts = duration.split(" ");
    var selectedDay = (parts.length > 1) ? Number(parts[0]) : 0;
    var selectedHour = Number(parts[parts.length - 1].split(":")[0]);
    var selectedMin = Number(parts[parts.length - 1].split(":")[1]);
    var selectedSeconds = Number(parts[parts.length - 1].split(":")[2]);

    var daysInHrs = selectedDay * 60; //Converting days into hours
    var minInHrs = selectedMin / 60; //converting mins in hrs
    var secInHrs = selectedSeconds / 3600; //converting secs in hrs

    var finDur = daysInHrs + selectedHour + minInHrs + secInHrs;
    //alert("Selected Days: " + selectedDay + " \nhours: " + selectedHour + " \nMinutes: " + selectedMin + " \nSeconds: " + selectedSeconds + " \nFinalDur: "+ finDur);
    if (finDur > 12) {
        alert('More than 12hrs detected');
    }
}

 

 

SN_Learn_0-1721810335034.png

 

More than 12 hrs selected:

SN_Learn_1-1721810370166.png

 

 

 

Mark this as Helpful / Accept the Solution if this helps

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.

View solution in original post

9 REPLIES 9

Addy22
Tera Guru

Hi @Community Alums 

What is variable type for 'how_long_do_you_need_access' ?

 

If it is date or date/time - In script include you can use GlideDateTime() 

 

Similar use case for your reference

 

Community Alums
Not applicable

Hi @Addy22,

 

Thank you for  your quick response

 It a Duration type variable.

 

Thank you

siva

Addy22
Tera Guru

You can write in this way:

setduration: function() {
       
 
  var duration = new GlideDuration(this.getParameter('sysparm_Duration'));
   var roundedDuration =duration.getRoundedDayPart());
        if (duration >= 1) {
            return ('Alert: Duration selected is more than 12 hours!');
        }
       
    }
 
 
It returns the rounded number of days. If the time part is more than 12 hours, the return value is rounded up. Otherwise, it is rounded down.

Community Alums
Not applicable

Hi @Addy22 ,

 

I tried above script it not working . It s get null

 

SivanandaReddy_0-1721802319522.png

Thank you

siva