- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-12-2022 08:54 AM
I am trying to update some cmn_schedule_span objects (move them forward one day). I understand that start_date_time and end_date_time are the fields I need, but these are of type GlideScheduleDateTime but stored as strings. However, I seem unable to create a 'regular' GlideDateTime which has has nice functions for addition to dates. This is roughly what I am trying to achieve:
var cmn_schedule_span = new GlideRecord('cmn_schedule_span');
cmn_schedule_span.addQuery('schedule', my_cmn_schedule_object);
cmn_schedule_span.query();
while(cmn_schedule_span.next()){
cmn_schedule_span.start_date_time.addDays(1);
cmn_schedule_span.update();
}
while(cmn_schedule_span.next()){
var start = new GlideDateTime(cmn_schedule_span.start_date_time);
start.addDays(1);
cmn_schedule_span.start_date_time = start.toString();
cmn_schedule_span.update();
}
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-13-2022 04:40 AM
Hello Kevin,
Could you please check with below script:
var cmn_schedule_span = new GlideRecord('cmn_schedule_span');
cmn_schedule_span.addQuery('schedule', cmn_schedule);
cmn_schedule_span.query();
while(cmn_schedule_span.next()){
var startDateTime = cmn_schedule_span.getDisplayValue("start_date_time");
gs.print("START DATE: " + startDateTime);
var gdt = new GlideDateTime();
gdt.setDisplayValue(startDateTime);
gdt.addDays(1);
var newDate = gdt.getDate();
gs.print("NEW DATE: " + newDate);
cmn_schedule_span.start_date_time = gdt.getDisplayValue();
cmn_schedule_span.end_date_time = gdt.getDisplayValue();
cmn_schedule_span.update();
}
Please mark my respsone as helpful/correct, if it answer your question.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-12-2022 09:24 AM
hello
try this usually those two fields are stored in this format
20220421T190000Z -->which is like date+T+Time is the format in which the concatenation is happening OOB
See below screenshot:
you can try this script
var cmn_schedule_span = new GlideRecord('cmn_schedule_span');
cmn_schedule_span.addQuery('schedule', my_cmn_schedule_object);
cmn_schedule_span.query();
while(cmn_schedule_span.next()){
var gdt=new GlideDateTime(cmn_schedule_span.start_date_time);
gdt.addDays(1);
var added = gdt;
var spt=added.toString().split(' ');
var datespt=spt[0];
var timespt=spt[1];
var dateCombo =datespt.split('-');
var timeCombo=timespt.split(':');
var finalValue = dateCombo[0]+dateCombo[1]+dateCombo[2]+"T"+timeCombo[0]+timeCombo[1]+timeCombo[2];
cmn_schedule_span.start_date_time = finalValue;
cmn_schedule_span.update();
}
Output:
Hope this helps
Mark my answer correct if this helps you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-13-2022 01:33 AM
Thanks for your help Mohith, but unfortunately that doesn't work. The following line doesn't result in a valid gdt:
var gdt=new GlideDateTime(cmn_schedule_span.start_date_time);
The following code:
var cmn_schedule_span = newGlideRecord('cmn_schedule_span');
cmn_schedule_span.addQuery('schedule', cmn_schedule);
cmn_schedule_span.query();
while(cmn_schedule_span.next()){
var start = newGlideDateTime(cmn_schedule_span.start_date_time);
gs.info('cmn_schedule_span.start_date_time: [' + cmn_schedule_span.start_date_time + ']');
gs.info('start: [' + start + ']');
}
results in the following output:
*** Script: cmn_schedule_span.start_date_time: [20180418T110000Z]
*** Script: start: []
i.e. the GlideDateTime has not been created.
Thanks, Kevin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-13-2022 04:40 AM
Hello Kevin,
Could you please check with below script:
var cmn_schedule_span = new GlideRecord('cmn_schedule_span');
cmn_schedule_span.addQuery('schedule', cmn_schedule);
cmn_schedule_span.query();
while(cmn_schedule_span.next()){
var startDateTime = cmn_schedule_span.getDisplayValue("start_date_time");
gs.print("START DATE: " + startDateTime);
var gdt = new GlideDateTime();
gdt.setDisplayValue(startDateTime);
gdt.addDays(1);
var newDate = gdt.getDate();
gs.print("NEW DATE: " + newDate);
cmn_schedule_span.start_date_time = gdt.getDisplayValue();
cmn_schedule_span.end_date_time = gdt.getDisplayValue();
cmn_schedule_span.update();
}
Please mark my respsone as helpful/correct, if it answer your question.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-13-2022 08:18 AM
Thanks guys, we're nearly there! I updated your code slightly Mahendra:
while(cmn_schedule_span.next()){
gs.print("cmn_schedule_span.start_date_time: " + cmn_schedule_span.start_date_time);
gs.print(cmn_schedule_span.start_date_time.getTimeZone());
var startDateTime = cmn_schedule_span.getDisplayValue("start_date_time");
gs.print("startDateTime: " + startDateTime);
var gdt = new GlideDateTime();
gdt.setDisplayValue(startDateTime);
gs.print("gdt: " + gdt);
gdt.addDays(1);
gdt.addMinutes(1);
gs.print("Updated gdt: " + gdt);
cmn_schedule_span.start_date_time = new GlideScheduleDateTime(gdt.toString());
gs.print('updated cmn_schedule_span.start_date_time:' + cmn_schedule_span.start_date_time)
gs.print(cmn_schedule_span.start_date_time.getTimeZone());
}
*** Script: cmn_schedule_span.start_date_time: 20180418T110000Z
*** Script: undefined
*** Script: startDateTime: 2018-04-18 12:00:00
*** Script: gdt: 2018-04-18 11:00:00
*** Script: Updated gdt: 2018-04-19 11:00:00
*** Script: updated cmn_schedule_span.start_date_time:20180419T110000
*** Script: undefined