Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Auto add work note to RITM using script

NathanHaywood
Tera Contributor

Hello, 

 

I have put together a very basic workflow. I am trying to add an activity to automatically set a work note in the RITM once approval has been gained and the catalog task has been created. 

 

I have tried many different options. The most recent is this:

 

Using a Run Script activity:

 

var gr = new GlideRecord("sc_req_item");
gr.addQuery("Item", "MS Teams Live Events host rights");
gr.query();
 
while(gr.next()) {
gr.work_notes = "Test WORK NOTE";
gr.update();
}
 
Help much appreciated!
 
Kind regards and thanks in advance.
4 REPLIES 4

Siyabend Sakik
Tera Contributor

Your query is on the field "Item" which is not a standard field I believe (I might be mistaken). Did you create a custom one for that? 

 

The actualy reference to the Catalog Item definition is in the "cat_item" field on the "sc_req_item" table. And since it's a reference you would have to query it via the sys id. 

 

The actual update on the work notes seems correct to me.

So basically something like this: 

var itemId = gs.getProperty('property.name.catitem'); //Store your catalog items sys id in a property, name it accordingly and get it via this function

var ritmGr = new GlideRecord("sc_req_item");
gr.addQuery("cat_item", itemId);
gr.query();
 
while(ritmGr .next()) {
  ritmGr .work_notes = "Test WORK NOTE";
  ritmGr .update();
}

Also the reminder to avoid naming your GlideRecord objects simply "gr" since that might cause you issues at some point 😉 

Thanks Siyabend. Very helpful.

 

Is there a better way of doing this i.e. not referencing the cat item itself?

 

Can I just have the script always add a work note into a RITM that uses this workflow? For example have an activity that will add a work note to the RITM once the task is created... it doesn't need to match any conditions? See screenshot of the current workflow. You can see the Run Script activity I am attempting to do this in after the Catalog Task activity.

 

Thanks again!

Hi Nathan

Is the workflow on the request item table?

If so in your query 

var ritmGr = new GlideRecord("sc_req_item");
ritmGr.addQuery("sys_id", current.sys_id);   // use ritmGr. not gr.
ritmGr.query();                                                // use ritmGr. not gr.
 

you can use the current.sys_id to return the record that you are processing