Add the duration variable to date and time variable and populat the end date value in string variab.

Community Alums
Not applicable

Hi Team,

 

I have created variable's in catalogs

 

1 start date - Date and time type variable

2 required time - Duration type variable

3 End time -  String type.

 

I need to add the duration and date and time variables. And populate the end time in string 

 

Can you please help me requriment.

 

Thank you

Siva

2 ACCEPTED SOLUTIONS

Rupanjani
Giga Guru

Hi @Community Alums,

 

Check the below link, it may be of any help for you.

https://www.servicenow.com/community/now-platform-forum/add-duration-to-date-time/m-p/1153814

 

Try below script and check:

onChange Client Script (start date), make sure the duration is first selected and then the start date.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var gaChng = new GlideAjax('Test'); //script include name
    gaChng.addParam('sysparm_name', 'getEndDate'); //second parameter - function name in the SI
    gaChng.addParam('sysparm_date', newValue);
    gaChng.addParam('sysparm_duration', g_form.getValue('u_duration'));
    gaChng.getXML(getData2);

    function getData2(response) {
        var answer2 = response.responseXML.documentElement.getAttribute('answer');
        if (answer2) {
            g_form.setValue('end_date', answer2);
        }
    }
}

 

 

Script Include (client callable checkbox - true):

 

var Test = Class.create();
Test.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getEndDate: function() {
        try {
            var gdt = new GlideDateTime(this.getParameter('sysparm_date'));
            var dur = new GlideDuration(this.getParameter('sysparm_duration'));
            gdt.add(dur);
            return gdt.getValue();
        } catch (ex) {
            gs.info('Error:' + ex.string() + '\nLine:' + ex.lineNumber);
        }
    },

    type: 'Test'
});

 

 

 


Mark the response correct and helpful if the answer assisted your question.

View solution in original post

Something like this should work. 

Add pass the fields to line 1 and 2

var gdt = new GlideDateTime('pass fiueld value');
var dur = new GlideDuration(<pass field value>);
//dur.setValue('1970-01-02 00:00:00');
gdt.add(dur);
gs.info(gdt.getDisplayValue());
-Anurag

View solution in original post

9 REPLIES 9

Community Alums
Not applicable

@Dr Atul G- LNG  Hi Atul,

 

Can you please help on that

Hi @Community Alums 

 

I am not a developer .. scripting is out of my bucket. 

 

@Sandeep Rajput  @Anurag Tripathi any help here.

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Something like this should work. 

Add pass the fields to line 1 and 2

var gdt = new GlideDateTime('pass fiueld value');
var dur = new GlideDuration(<pass field value>);
//dur.setValue('1970-01-02 00:00:00');
gdt.add(dur);
gs.info(gdt.getDisplayValue());
-Anurag

Hi @Anurag Tripathi ,

 

We need to use gdt.getValue() instead of gdt.getDisplayValue().

 


Mark the response correct and helpful if the answer assisted your question.

Rupanjani
Giga Guru

Hi @Community Alums,

 

Check the below link, it may be of any help for you.

https://www.servicenow.com/community/now-platform-forum/add-duration-to-date-time/m-p/1153814

 

Try below script and check:

onChange Client Script (start date), make sure the duration is first selected and then the start date.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var gaChng = new GlideAjax('Test'); //script include name
    gaChng.addParam('sysparm_name', 'getEndDate'); //second parameter - function name in the SI
    gaChng.addParam('sysparm_date', newValue);
    gaChng.addParam('sysparm_duration', g_form.getValue('u_duration'));
    gaChng.getXML(getData2);

    function getData2(response) {
        var answer2 = response.responseXML.documentElement.getAttribute('answer');
        if (answer2) {
            g_form.setValue('end_date', answer2);
        }
    }
}

 

 

Script Include (client callable checkbox - true):

 

var Test = Class.create();
Test.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getEndDate: function() {
        try {
            var gdt = new GlideDateTime(this.getParameter('sysparm_date'));
            var dur = new GlideDuration(this.getParameter('sysparm_duration'));
            gdt.add(dur);
            return gdt.getValue();
        } catch (ex) {
            gs.info('Error:' + ex.string() + '\nLine:' + ex.lineNumber);
        }
    },

    type: 'Test'
});

 

 

 


Mark the response correct and helpful if the answer assisted your question.