- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-08-2015 10:37 AM
I'm trying to create schedule span records from change requests and outage records. Both should be under the same parent schedule.
The code/query is simple and should work, but the begin and end date/times are not getting set on the records.
I simply go through approved changes and through all outages and create a cmn_schedule_span record. Records are generated with no begin/end date/time.
Any thoughts? On the UI form when creating a record the dates need to be put in through a formatter. It won't allow direct input into the beign/end fields, but through gr.initialize() i'm not sure why this UI functionality would matter.
<code>
var chGr = new GlideRecord('change_request');
chGr.addQuery('approval','approved');
chGr.query();
while(chGr.next()){
var cmnSpanOne = new GlideRecord('cmn_schedule_span');
cmnSpanOne.initialize();
cmnSpanOne.u_change = chGr.sys_id;
cmnSpanOne.type = "Change";
cmnSpanOne.repeat_type = "NULL_OVERRIDE";
cmnSpanOne.name = chGr.number;
cmnSpanOne.schedule = 'a9ae3c596f288200f85d652fae3ee486';
cmnSpanOne.start_date_time = chGr.start_date;
cmnSpanOne.end_date_time = chGr.end_date;
cmnSpanOne.insert();
}
var outGr = new GlideRecord('cmdb_ci_outage');
outGr.query();
while(outGr.next()){
var cmnSpanTwo = new GlideRecord('cmn_schedule_span');
cmnSpanTwo.initialize();
cmnSpanTwo.u_outage = outGr.sys_id;
cmnSpanTwo.type = "Outage";
cmnSpanTwo.repeat_type = "NULL_OVERRIDE";
cmnSpanTwo.name = outGr.u_number;
cmnSpanTwo.schedule = 'a9ae3c596f288200f85d652fae3ee486';
cmnSpanTwo.start_date_time = outGr.begin;
cmnSpanTwo.end_date_time = outGr.end;
cmnSpanTwo.insert();
}
</code>
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2015 08:45 AM
The answer to this is simple - It's actually the first thing I tried, but I must have fat-fingered something or messed up the syntax. I don't think anyone's watching this, but sorry for the late response just in case.
//Instantiate a new GDT:
var start_gdt = new GlideDateTime();
//Set the value based on the time you are setting the schedule gdt
start_gdt.setValue(outGr.begin);
//to string and you're good
targetRecord.targetField = start_gdt.toString();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-08-2015 10:46 AM
So it looks like the difference here, and the issue, is that the date/times from my outage and change records are stored in a different field type.
glide_date_time fields versus schedule_date_time fields.
So what I'm going to need to do is a conversion from gdt to sdt. I'll play with it and look that up and if I find it I'll post the solution.
-ww
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2015 08:45 AM
The answer to this is simple - It's actually the first thing I tried, but I must have fat-fingered something or messed up the syntax. I don't think anyone's watching this, but sorry for the late response just in case.
//Instantiate a new GDT:
var start_gdt = new GlideDateTime();
//Set the value based on the time you are setting the schedule gdt
start_gdt.setValue(outGr.begin);
//to string and you're good
targetRecord.targetField = start_gdt.toString();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-29-2015 01:02 PM
Hello Walt
Thanks for posting your resolution. If you could provide some assistance based off of what you have here I would appreciate it.
I'm creating a record producer to enter 'business hours' for customers of ours, which are easy to enter for business type folks and will still use them as guidance for maintenance windows for Changes etc.
Below is what I have and then I've attempted to incorporate your scripting into mine. I'm not sure how to do this; it seems to me that issue is with "//Set the value based on the time you are setting the schedule gdt" line of code.
Can you assist with the format at all?
Thanks in advance.
var spanTask = new GlideRecord('cmn_schedule_span');
spanTask.initialize();
spanTask.name = "Business Hours for: " + current.name;
spanTask.schedule = current.sys_id;
spanTask.u_customer_name = current.name;
//Instantiate a new GDT:
var start_gdt = new GlideDateTime();
//Set the value based on the time you are setting the schedule gdt
start_gdt.setValue(spanTask.begin);
spanTask.start_date_time = start_gdt.toString();
spanTask.insert();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-29-2015 02:47 PM
Hi Josh - What I wrote was actually a little confusing.
If you're trying to generate a cmn_schedule_span record from a record producer, how you do it will depend greatly on how your producer is set up.
If your record producer has two date/time variables, one that will equate to start_date_time and one to end_date_time, then that will be fairly simple.
To access your producer variables in the script, you'll use "producer.variable". So when you're setting the GlideDateTime(); for your date, you'll do the following:
*assuming your variable for start time is called "st"
var start_gdt = new GlideDateTime();
start_gdt.setValue(producer.st);
spanTask.start_date_time = start_gdt.toString();
It's been a while since I've played with Date/Times on catalogs, but I believe that should work.
Something you can do is have an option that mimics the "Repeat" drop-down on the cmn_schedule_span, so they can fill out 1 time span and have it repeat daily, weekly, on the weekends, etc., by selecting the appropriate options from the drop down. Or if they are filling out their schedule/hours for only a week, or three days in advance, they can choose "repeat daily" and then choose a date for the schedule_span to stop, which you can map like the above to the "repeat_until" field on the span record.
Anyway, hope the above helps. I'll try it out on the demo instance now and see what happens.