We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

Calculate the End date based on the start date

Sirri
Tera Guru

Hi All,

 

Please help the Regarding  & Any source code available please share.

 

1)Start Date - date
2)Temporary Access Duration Required -Drop Down -

5 Minutes
15 Minutes
30 Minutes
1 Hour
5 Hours
12 Hours
1 Day
5 Days
10 Days


3)End Date -Date

 Note is for the end Date.
Note : Prepopuated Date Based on duration selected - Use 'start date' to calculate the 'end date'. uneditable/greyed out.
If minutes will select then end date should be as start date

5 REPLIES 5

Alka_Chaudhary
Mega Sage

Hello @Sirri  ,

Please replace your scripts with the below script and try it.

Client Script:-

    var da = g_form.getValue('start_date_time');
    var ac = g_form.getValue('temporary_access_duration_required');
    var ga = new GlideAjax('EndDateAutoCalculate');
    ga.addParam('sysparm_name', 'test1');
    ga.addParam('sysparm_daa', da);
    ga.addParam('sysparm_acc', ac);
    ga.getXML(callback);

    function callback(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        alert(answer);

        g_form.setValue('end_date_time', answer);

    }

Script Include:

test1: function() {
        var ab = this.getParameter('sysparm_daa');
        var ac = this.getParameter('sysparm_acc');
        var gdt = new GlideDateTime(ab);
        if (ac == '5 Minutes') {
            gdt.addSeconds(300);
        } else if (ac == '15 Minutes') {
            gdt.addSeconds(900);
        } else if (ac == '30 Minutes') {
            gdt.addSeconds(1800);
        } else if (ac == '1 Hour') {
            gdt.addSeconds(3600);
        } else if (ac == '5 Hours') {
            gdt.addSeconds(18000);
        } else if (ac == '12 Hours') {
            gdt.addSeconds(43200);
        } else if (ac == '1 Day') {
            gdt.addDays(1);
        } else if (ac == '5 Day') {
            gdt.addDays(5);
        } else if (ac == '10Days') {
            gdt.addDays(10);
        }
        return gdt;
    },

 

Please Mark my answers Helpful & Accepted if I have answered your questions.

Thanks,

Alka