converting a duration to hours

NewBiee
Tera Contributor

Greetings,

i have a requirement where --> when an RTO (reference field) selected is less than 24 Hrs, display the message “Default RTO is 24 Hrs. Are you sure you want to proceed with selection”?".

i have written a script include and called it from client script.

script include:

var BCMClientUtilAppl = Class.create();
BCMClientUtilAppl.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

    hoursValidation: function() {
        
        var timeframeGlide = new GlideRecord('sn_bcm_timeframe');
        var value = this.getParameter('sysparm_timeframeSysId');
        gs.info("BCMClientUtilAppl new sysid value: " + value);
        timeframeGlide.query('sys_id', value);
        timeframeGlide.query();

        if (timeframeGlide.next()) {

            var startsAtValue= timeframeGlide.getDisplayValue('starts_at');
            gs.info("BCMClientUtilAppl starts at get display value: " + startsAtValue);

            var valueDateTime = new GlideDateTime(startsAtValue);
            valueDateTime = valueDateTime.getNumericValue() / 1000;
            gs.info("BCMClientUtilAppl converting startsAtValue to gilde date time: " + valueDateTime);

            var valueDuration = new GlideDuration(startsAtValue);
            valueDuration = valueDuration.getNumericValue() / 1000;
            gs.info("BCMClientUtilAppl converting startsAtValue to glide duration: " + valueDuration);

        }
        return true;
    },

    type: 'BCMClientUtilAppl'
});

client Script

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    var rtoValue= g_form.getValue('recovery_time_objective');
    var rtoDisplayValue = g_form.getDisplayValue('recovery_time_objective');
    var nameValue = g_form.getValue('name');
    //     alert("rto: " + rtoDisplayValue); // sysid
    //     alert("rtoo: " + rtoo); //bia name
    // 	alert("name value: " +nameValue); //bia name

    var ga = new GlideAjax('BCMClientUtilAppl');
    ga.addParam('sysparm_name', 'hoursValidation');
    ga.addParam('sysparm_timeframeSysId', rtoValue);
    ga.getXML(callback);
}

function callback(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    alert("answer: ", +answer);
    //     g_form.setValue('applies_to_table', answer);
}

 

i have added necessary details in the form

the working of the code should be

when any value selected find_real_file.png

starts at value from this table is taken find_real_file.png

and then validated.

 

the results i'm getting right now is this

find_real_file.png

right now i'm not getting the value in hours or seconds, and also the return true value is not being passed from script include to client script.

expected result: 

if selected 2 days it should return 48 hours

and alert message should be populated

 

Can anyone help me with this code??

thank you.

1 ACCEPTED SOLUTION

@NewBiee My bad there is a typo in above script.

var dur = startAtValue.toString();  Should be  var dur = startsAtValue.toString();

var hour = 0; Should be declared before if statement.

 

Try the below Script Include

var BCMClientUtilAppl = Class.create();
BCMClientUtilAppl.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

    hoursValidation: function() {
        
        var timeframeGlide = new GlideRecord('sn_bcm_timeframe');
        var value = this.getParameter('sysparm_timeframeSysId');
        gs.info("BCMClientUtilAppl new sysid value: " + value);
        timeframeGlide.addQuery('sys_id', value);
        timeframeGlide.query();

        var hour = 0;

        if (timeframeGlide.next()) {

            var startsAtValue= timeframeGlide.getDisplayValue('starts_at');
            var dur = startsAtValue.toString();

            var arr = dur.split(' ');  // Array will look like ['Days', '2', 'Hours', '1', 'Minutes', '2']
            var arr2 = [];  // Array to store only values like ['2', '1', '2']
            var arr3 = [24, 1, 1/60, 1/3600] // Will be used to convert duration to hours

            // Pushing only numbers/values to arr2
            for (var i=0; i<arr.length; i++){
                if(!isNaN(arr[i])){
                    arr2.push(arr[i]);
                } 
            }

           // Converting duration to hours
           var temp;
           for (var i=0; i<arr2.length; i++){
               temp = arr2[i] * arr3[i];
               hour += temp;
           }

        }
        return hour;
    },

    type: 'BCMClientUtilAppl'
});

View solution in original post

4 REPLIES 4

Muhammad Khan
Mega Sage
Mega Sage

Hi NewBie,

 

  • Remove comma (,) from alert("answer: ", +answer);
    • Use it like alert("answer: " + answer);
  • Use timeframeGlide.query('sys_id', value)
    • Like timeframeGlide.addQuery('sys_id', value);
var startsAtValue= timeframeGlide.getDisplayValue('starts_at');
var dur = startAtValue.toString();

var arr = dur.split(' ');  // Array will look like ['Days', '2', 'Hours', '1', 'Minutes', '2']
var arr2 = [];  // Array to store only values like ['2', '1', '2']
var arr3 = [24, 1, 1/60, 1/3600] // Will be used to convert duration to hours

// Pushing only numbers/values to arr2
for (var i=0; i<arr.length; i++){
  if(!isNaN(arr[i])){
    arr2.push(arr[i]);
  }
}

// Converting duration to hours
var temp;
var hour = 0;
for (var i=0; i<arr2.length; i++){
  temp = arr2[i] * arr3[i];
  hour += temp;
}

return hour;

 

Hopefully, this will help you.

@NewBiee Overall Scripts might look like below

Script Include

var BCMClientUtilAppl = Class.create();
BCMClientUtilAppl.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

    hoursValidation: function() {
        
        var timeframeGlide = new GlideRecord('sn_bcm_timeframe');
        var value = this.getParameter('sysparm_timeframeSysId');
        gs.info("BCMClientUtilAppl new sysid value: " + value);
        timeframeGlide.addQuery('sys_id', value);
        timeframeGlide.query();

        if (timeframeGlide.next()) {

            var startsAtValue= timeframeGlide.getDisplayValue('starts_at');
            var dur = startAtValue.toString();

            var arr = dur.split(' ');  // Array will look like ['Days', '2', 'Hours', '1', 'Minutes', '2']
            var arr2 = [];  // Array to store only values like ['2', '1', '2']
            var arr3 = [24, 1, 1/60, 1/3600] // Will be used to convert duration to hours

            // Pushing only numbers/values to arr2
            for (var i=0; i<arr.length; i++){
                if(!isNaN(arr[i])){
                    arr2.push(arr[i]);
                } 
            }

           // Converting duration to hours
           var temp;
           var hour = 0;
           for (var i=0; i<arr2.length; i++){
               temp = arr2[i] * arr3[i];
               hour += temp;
           }

        }
        return hour;
    },

    type: 'BCMClientUtilAppl'
});

Client Script

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    var rtoValue= g_form.getValue('recovery_time_objective');
    var rtoDisplayValue = g_form.getDisplayValue('recovery_time_objective');
    var nameValue = g_form.getValue('name');
    //     alert("rto: " + rtoDisplayValue); // sysid
    //     alert("rtoo: " + rtoo); //bia name
    // 	alert("name value: " +nameValue); //bia name

    var ga = new GlideAjax('BCMClientUtilAppl');
    ga.addParam('sysparm_name', 'hoursValidation');
    ga.addParam('sysparm_timeframeSysId', rtoValue);
    ga.getXML(callback);
}

function callback(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    if (answer < 24)
        alert("Default RTO is 24h. Are you sure you want to proceed ?");
    //     g_form.setValue('applies_to_table', answer);
}

helllo @Muhammad Khan

i tried the code

1. the alert message is popping up for every selection regardless of the condition being true or false ( the answer returning is null)

2. i don't know if the conversion is working or not. i tried gs.info. 

gs.info("BCMClientUtilAppl hello if");

            var startsAtValue = timeframeGlide.getDisplayValue('starts_at');
            var dur = startAtValue.toString();
			gs.info("BCMClientUtilAppl starts at value: " +startsAtValue);

the first gs.info worked, but the second one is not working

@NewBiee My bad there is a typo in above script.

var dur = startAtValue.toString();  Should be  var dur = startsAtValue.toString();

var hour = 0; Should be declared before if statement.

 

Try the below Script Include

var BCMClientUtilAppl = Class.create();
BCMClientUtilAppl.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

    hoursValidation: function() {
        
        var timeframeGlide = new GlideRecord('sn_bcm_timeframe');
        var value = this.getParameter('sysparm_timeframeSysId');
        gs.info("BCMClientUtilAppl new sysid value: " + value);
        timeframeGlide.addQuery('sys_id', value);
        timeframeGlide.query();

        var hour = 0;

        if (timeframeGlide.next()) {

            var startsAtValue= timeframeGlide.getDisplayValue('starts_at');
            var dur = startsAtValue.toString();

            var arr = dur.split(' ');  // Array will look like ['Days', '2', 'Hours', '1', 'Minutes', '2']
            var arr2 = [];  // Array to store only values like ['2', '1', '2']
            var arr3 = [24, 1, 1/60, 1/3600] // Will be used to convert duration to hours

            // Pushing only numbers/values to arr2
            for (var i=0; i<arr.length; i++){
                if(!isNaN(arr[i])){
                    arr2.push(arr[i]);
                } 
            }

           // Converting duration to hours
           var temp;
           for (var i=0; i<arr2.length; i++){
               temp = arr2[i] * arr3[i];
               hour += temp;
           }

        }
        return hour;
    },

    type: 'BCMClientUtilAppl'
});