Update the on-call calendar via script?

Jason Stephens
Kilo Guru

Is it possible to update the on-call calendar from a script (record produce, scheduled job, etc)? I would like to be able to do the same thing as the "drag" from the calendar when the "Add item" pop-up shows and asks me for the name and how long they will be covering on-call. I'm not sure how straightforward this will be - if possible...

Thanks for the help!

Jason

4 REPLIES 4

zschneider
Kilo Expert

It's technically possible, I think, but would require a good bit of work.

I believe the table those are written to is cmn_schedule_span, you could create a scheduled job that writes to that table with the information you need.

I tried writing a quick test script and was blocked by the business rule called "Schedule Item validate", so you'd have to make sure all the information is properly filled in in order for the script to actually run.

Here's an example script I wrote (I wasn't able to get it to work). I would definitely test any script like this thoroughly in sub-prob, since the rotation isn't really designed to be written to like this:



addToSchedule();

function addToSchedule() {
var entry = new GlideRecord('cmn_schedule_span');
entry.initialize();
entry.user = "NAME"; //need to check what table this field is referencing, I don't think it's sys_user
entry.start_date_time = gs.beginningOfThisWeek(); //not sure if this is the correct time property
entry.end_date_time = gs.endOfThisWeek();
entry.type = "on_call";
entry.group = "Group_Name";
entry.roster = "Primary";
entry.all_day = true;
entry.insert();
}


You might also look into the scripting API, which may enable you to do this way more easily but isn't very well documented at the moment: http://wiki.servicenow.com/index.php?title=Scripting_of_On-Call_Rotations


Jason Stephens
Kilo Guru

I was trying to do it against the roster_schedule_span table because that's what seems to hold the data for the visual calendar. Maybe I am looking at it the wrong way. I'll see if I can make it work against the table you mentioned....

Thanks for the idea.

Jason


andrew_och
ServiceNow Employee
ServiceNow Employee

As of Helsinki release there is a Scripted REST API: OnCallRotation


http://<instance>/api/now/on_call_rota/addoverride




This calls script include: OCAddItem as follows



new OCAddItem(new OCFullCalendarFormatter())


      .setMemberSysId(<cover user sys_id>)


      .setRosterSysId(<roster sys_id>)


      .setStartDateTime(<start_date_time>)


      .setEndDateTime(<start_date_time>)


      .createOverride()




If you do not want to use the API/Script Include and want to just create data, or you are on an instance prior to Helsinki, to create cover for another member you would need:


1. create a cmn_schedule record for that user, document=sys_user document_key=<sys_id of the sys_user>


2. create a roster_schedule_span, schedule=<sys_id of the schedule created in step 1>, type=on_call and the start/end dateTime




Even if on an instance prior to Helsinki it would be worth looking at a Helsinki instance script include: OCAddItem to see how cover is created as a basis for your customization.


arya6
ServiceNow Employee
ServiceNow Employee

Just to add to Andrew's answer.



If you are on a release prior to Helsinki, you can take a look at the sys_ui_page that handles the Add Item dialog to get some idea about adding overrides and time-off records in script.



Name: on_call_add_item


Link: <instance_name>sys_ui_page.do?sys_id=8df6dfc3c0a801650077c7b194df3e6d



Hope that helps,


Arya