System Schedule - creating cmn_schedul_span records

will27
Giga Contributor

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>

1 ACCEPTED SOLUTION

will27
Giga Contributor

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();


View solution in original post

8 REPLIES 8

will27
Giga Contributor

So yes - the above works.   I   tried the following in a record producer on the demo instance, and it properly set the start/end date/times.     I didn't use a GlideRecord initialize() to insert a record, but linked the producer directly to the table for expediency.   If you're going this route, you can probably directly map the variables to the span table and not even worry about scripting, but it looks like you're generating a parent first.



sdt is my producer variable name for start date/time


edt is my producer variable name for end date/time




var start_gdt = new GlideDateTime();


start_gdt.setValue(producer.sdt);


current.start_date_time = start_gdt.toString();



var end_gdt = new GlideDateTime();


end_gdt.setValue(producer.edt);


current.end_date_time = end_gdt.toString();


Hello William - Thank you so much for helping me with this.



However, I can't seem to get it to work as displayed.   And yes, I'm creating both the parent Schedule record along with the Schedule Entry 'related' record.


The time zone is not included with the current scripting however, so the hours are incorrect on the entry record.



//Create new parent Schedule with related Schedule Span


current.type = "customer";


current.name = producer.name.getDisplayValue();




// create Schedule Span 1 for Monday-Friday business hours




var spanTask = new GlideRecord('cmn_schedule_span');


spanTask.initialize();


spanTask.name = "Business Hours for: " + producer.name.getDisplayValue();


spanTask.schedule = current.sys_id;


spanTask.u_customer_name = producer.name.getDisplayValue();



spanTask.start_date_time = new GlideScheduleDateTime(producer.sdt);


spanTask.end_date_time     = new GlideScheduleDateTime(producer.edt);


//scheduleStart.setTimeZone(timeZone);


//scheduleEnd.setTimeZone(timeZone);








spanTask.insert();


I actually was able to figure this out but haven't updated with resolution here.



Snap49.png


snowuser13
Tera Contributor

Did it create with Repeat == "Does not repeat" type span entry