Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

SN_Learn
Kilo Patron
Kilo Patron

Hi @Community Alums ,

 

You can achieve this in the client side only. No need to call the script include. Please take a look at the below sample script:

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 daysInHrs = selectedDay * 60; //Converting days into hours

    alert("Selected Days: " + selectedDay + " \nhours: " + selectedHour + "\nDayInHrs: " + daysInHrs);

}

 

Now, you can simply compare them and alert the message as per the requirement.

 

Mark this as Helpful / Accept the Solution if this helps

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

Community Alums
Not applicable

Hi @SN_Learn ,

 

Thank you for the solution.

It works only for if giving 13 hours .if i am giving 12 hours 1 mint it not works

 

I need count mints also .if select more the 720 need get alert msg.

 

Thank you,

Siva

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.

Community Alums
Not applicable

Thank you so much @SN_Learn  It works