OnSubmit Client Script Issue for calculation of Date Fields
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2024 12:27 PM
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 -
Script Include Code :-
But if user is changing date manually from 19 to 18 but not clicking outside End Date & saving form then -
But tried to fix it with Onsubmit Client Script but it's not working.
Onsubmit Client Script -
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2024 02:53 PM - edited 04-04-2024 02:54 PM
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....
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2024 09:13 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2024 09:37 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-09-2024 05:02 AM
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