The CreatorCon Call for Content is officially open! Get started here.

OnSubmit Client Script Issue for calculation of Date Fields

Krishnakant Arv
Tera Expert

Hi Developers,

 

Hope you are doing great!!

 

I have two date fields in form u_occ_start_date(Start Date) & u_occ_end_date(End Date). Also I have one more string field that contains calculated value of both date fields u_occu_no_days(No. days). My code is working fine Onchange of u_occ_end_date but when I am writing same code On Submit then it's not working.

 

Actually If user is selecting Date using Date Picker in date fields then onchange of field only it is calculating fine but user also able to change date manually but without clicking somewhere else onload code is not able to work but user is able to save so whatever value in alreday there in No.days it not getting change and form is getting saved so technically value in No.days is not correct so thought to implement same logic Onsubmit of record but I am getting correct alert but it not setting value correctly in No.days field So help me fix this issue.

 

Code in Client Script -

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading) {
        return;
    }
    var start_date = g_form.getValue('u_occ_start_date'); //First Date/Time field
    var end_date = g_form.getValue('u_occ_end_date'); //Second Date/Time field
    var dttype = 'day'; //this can be day, hour, minute, second. By default it will return seconds.


    function doSomething(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        g_form.setValue('u_occu_no_days', parseInt(answer));
    }

    if (start_date != '') {
        var ajax = new GlideAjax('adc');
        ajax.addParam('sysparm_name', 'getDateTimeDiff');
        ajax.addParam('sysparm_fdt', start_date);
        ajax.addParam('sysparm_sdt', end_date);
        ajax.addParam('sysparm_difftype', dttype);
        ajax.getXML(doSomething);

                if (end_date < start_date && end_date != '') {

                    g_form.clearValue('u_occ_end_date');
                    g_form.clearValue('u_occu_no_days');
                    g_form.showFieldMsg('u_occ_end_date', 'End date should not be before the Start Date', 'error');

                }
            } else if (end_date != '') {

                g_form.clearValue('u_occ_end_date');
                g_form.clearValue('u_occu_no_days');
                g_form.showFieldMsg('u_occ_end_date', 'Start date should not be empty.', 'error');
            }

        if (end_date == '' || start_date == '') {
            g_form.clearValue('no_of_days');
        }
    }

 

Script Include Code :- 

 

getDateTimeDiff: function() {
        var firstDT = this.getParameter('sysparm_fdt'); //First Date-Time Field
        var secondDT = this.getParameter('sysparm_sdt'); // Second Date-Time Field
        var diffTYPE = this.getParameter('sysparm_difftype'); // Date-Time Type to return the answer as. Can be second, minute, hour, day
        var fromDate = new GlideDateTime(firstDT);
        var toDate = new GlideDateTime(secondDT);
        var dayCounter = 0;
        while (fromDate.onOrBefore(toDate)) {

            var days = fromDate.addDaysUTC(1);
            if (!(fromDate.getDayOfWeekUTC() == '1' || fromDate.getDayOfWeekUTC() == '7'))
                dayCounter++;
        }
        return dayCounter;
    },
 
If date is selected with date picker then No.days is fine -
 KrishnakantArv_0-1712257937489.png

 

But if user is changing date manually from 19 to 18 but not clicking outside End Date & saving form then - 

KrishnakantArv_1-1712258139135.png

 

 

But tried to fix it with Onsubmit Client Script but it's not working.

 

Onsubmit Client Script - 

 

function onSubmit() {
    var start_date = g_form.getValue('u_occ_start_date'); //First Date/Time field
    var end_date = g_form.getValue('u_occ_end_date'); //Second Date/Time field
    var dttype = 'day'; //this can be day, hour, minute, second. By default it will return seconds.

    var ajax = new GlideAjax('global.Terminationerhruser');
    ajax.addParam('sysparm_name', 'getDateTimeDiff');
    ajax.addParam('sysparm_fdt', start_date);
    ajax.addParam('sysparm_sdt', end_date);
    ajax.addParam('sysparm_difftype', dttype);
    ajax.getXML(doSomething);

    function doSomething(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        alert("Answer = "+ answer);
        g_form.setValue('u_occu_no_days', parseInt(answer));
        alert("Action Completed");
    }

}
 
But alert I am getting it correctly -
 
KrishnakantArv_2-1712258366844.png

 

KrishnakantArv_4-1712258421144.png

 

 

What could be possible reason that value is not setting Properly in No.days OnSubmit  of form & how I can fix it.

 

Thanks in Advance!!

 

Thank You,

Krishnakant

4 REPLIES 4

Sohail Khilji
Kilo Patron

Try this :

 

function onSubmit() {
    var start_date = g_form.getValue('u_occ_start_date'); 
    var end_date = g_form.getValue('u_occ_end_date');
    var dttype = 'day'; //this can be day, hour, minute, second. By default it will return seconds.

    var ajax = new GlideAjax('global.Terminationerhruser');
    ajax.addParam('sysparm_name', 'getDateTimeDiff');
    ajax.addParam('sysparm_fdt', start_date);
    ajax.addParam('sysparm_sdt', end_date);
    ajax.addParam('sysparm_difftype', dttype);
    ajax.getXML(doSomething);

    function doSomething(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        alert("Answer = " + answer);
        g_form.setValue('u_occu_no_days', parseInt(answer));
        alert("Action Completed");
        return true; 
    }
    return false; 
}

 


☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect

Hi @Sohail Khilji ,

 

Thanks for your Reply!

 

Tried above suggested code but it's behaving exactly same as it was before and due to return false not able to submit also.

 

I have removed return false and tried again but what exactly happening here is before the form getting submitted I can see value in No.days is setting correctly due to On submit Client script but once submission completed still old value is there.

 

Why this is happening not able to understand🤔

 

Thanks,

Krishnakant

 

Deepak Shaerma
Kilo Sage
Kilo Sage

Hi @Krishnakant Arv 
When the form is submitted, the client script initiates the asynchronous call, but doesn’t wait for the response. Instead, the form submission continues, and the page may reload or redirect before the asynchronous callback has a chance to execute and update the number of days field. This is why your alert might show the correct calculation, but the field doesn’t get updated in the database.

Note: Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning ‌‌
Thanks & Regards 
Deepak Sharma



Krishnakant Arv
Tera Expert

Hi @Deepak Shaerma ,

 

Thank for your explanation.

 

Need little more help to achieve that. Please let me know that how I can make it correct!!

 

Thanks,

Krishnakant