- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-22-2023 11:26 AM
Hello All,
I have a requirement where I have 2 fields i.e. 'Please Select the duration' (Radio Button with 2 fields i.e. 24 Hours and 48 Hours)(Name - please_select_the_duration) and 'Start Date' (Date Time field) (Name - start_date). Based upon the radio button selected and on change of Start Date the End date field needs to be populated by adding 24 Hours or 48 Hours based upon the selectiom.
I tried multiple approaches from community, but no luck. Can someone please help me with the same.
Script Include:
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-23-2023 04:41 AM
So in the script include you write:
var dt = new GlideDateTime(start_date);
This will only work if start_date is a string that contains a date and time in UTC time zone and format YYYY-DD-MM HH:mm:ss.
The original poster wrote:
var selected_date = new GlideDate();
selected_date.setDisplayValue(this.getParameter('sysparm_Date'));
which is way better, only it is the wrong object.
Should have been:
var selected_date = new GlideDateTime();
selected_date.setDisplayValue(this.getParameter('sysparm_Date'));
That is because GlideDateTime's setDisplayValue expects a string representing a date and time in the current user's time zone and the current user's format - whichever that format is.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-23-2023 04:53 AM
I have just checked by changing the format.
In PDI currently don't have format above mentioned .
yes...
var selected_date = new GlideDateTime(); selected_date.setDisplayValue(this.getParameter('sysparm_Date'));
with this Timezone issue also resolved.
try to update the below line in script include
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-23-2023 06:07 AM
I have added the Date format as per you's and checked
Thanks to @-O- , It really works. Learned something new today.
Below is the full script :
Script include :
pastDate: function() {
var result = {
'isStartDateValid': '',
'endDate': ''
};
// get values selected on form
var radio_button = this.getParameter('sysparm_Duration');
var start_date = this.getParameter('sysparm_Date');
//get Current date and time
var getCurrentDate = new GlideDateTime();
getCurrentDate.getDisplayValue();
// get future date depends upon duration selected.
var dt = new GlideDateTime();
dt.setDisplayValue(start_date);
//Check if start date is not past date
if (dt > getCurrentDate) {
result.isStartDateValid = true;
} else {
result.isStartDateValid = false;
}
if (radio_button == '24') { //use your backend value here
dt.addDaysLocalTime(1); //24 hrs = 1 day
} else {
dt.addDaysLocalTime(2); //48 hr = 2 days
}
gs.log("CommunityDate= " + dt);
result.endDate = dt.getDisplayValue();
return JSON.stringify(result);
},
Client Script :
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
alert("Start date=" + newValue);
var radio_button = g_form.getValue('duration');
var ga = new GlideAjax('UtilsAjax');
ga.addParam('sysparm_name', 'pastDate');
ga.addParam('sysparm_Date', newValue);
ga.addParam('sysparm_Duration', radio_button);
ga.getXMLAnswer(CheckDateValidation);
}
function CheckDateValidation(answer) {
var result = JSON.parse(answer);
if (result.isStartDateValid == true) {
//Set the end date depending upon result returned by script include
g_form.setValue('end_date', result.endDate);
} else {
alert("start date should not be past date");
}
}
Output :
Test case1 : If user selects the past date
Test case 2 : depending upon duration and start date - end date should populate
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-23-2023 06:17 AM
You're welcome. I'm glad I could help.