The CreatorCon Call for Content is officially open! Get started here.

Help with 'Summary of Requested Item' Script in RITM workflow

Kevin Ng
Tera Expert

Background:

We are using the script below to add a summary of the requested item in our catalog items to the work notes of the the sc_task. This was previously only used in catalog items with 1:1 relationships for Request -> Requested Item -> Service Catalog Task.

var gr = new GlideRecord("sc_req_item");
gr.addQuery("request", current.request.sys_id);
gr.query();

while (gr.next()) {
    // Get Owned Variables for Requested Item and sort by Order
    var ownvar = new GlideRecord('sc_item_option_mtom');
    ownvar.addQuery('request_item.number', gr.number);
    ownvar.addQuery('sc_item_option.value', '!=', '');
    ownvar.orderBy('sc_item_option.order');
    ownvar.query();

    var items = "Summary of " + gr.number + ":   " + gr.cat_item.getDisplayValue() + "\n\n";

    while (ownvar.next()) {

        var field = ownvar.sc_item_option.item_option_new;
        var fieldValue = ownvar.sc_item_option.item_option_new.name;
        // Print variable name
        items += field.getDisplayValue() + ": " + gr.variables[fieldValue].getDisplayValue() + "\n";

    }
}
task.work_notes = items;

Issue:

We've recently begun utilizing an onboarding order guide in our service catalog and I've identified an issue that I need some help with. 

When someone uses the order guide, which will create 1-3 separate requested items depending on their needs, the resulting work note is being duplicated across all sc_task records rather than displaying the summary of the unique RITM associated with it.

I've tried adding timers to the workflow prior to sc_task creation as well as renaming the 'items' variable to unique names for each workflow, but receive the same result. 

Has anyone run into this or know of a potential solution? The requested item summary is helpful to our technicians when they need to seek additional approval. 

Thank you,

Kevin

1 ACCEPTED SOLUTION

Sumit Pandey1
Kilo Guru

Is this code written in workflow? Can you do this in a business role written on sc_task table. It could be a after insert business role and your first line should change to - 

var gr = new GlideRecord("sc_req_item");
gr.addQuery("sys_id", current.request_item.toString());  - changed here
gr.query();

 

Also the last line should change to - 

current.work_notes = items

The intention is that business rule will only trigger per unique tasks and thus would remove duplicates.

View solution in original post

4 REPLIES 4

Sumit Pandey1
Kilo Guru

Is this code written in workflow? Can you do this in a business role written on sc_task table. It could be a after insert business role and your first line should change to - 

var gr = new GlideRecord("sc_req_item");
gr.addQuery("sys_id", current.request_item.toString());  - changed here
gr.query();

 

Also the last line should change to - 

current.work_notes = items

The intention is that business rule will only trigger per unique tasks and thus would remove duplicates.

Hi Sumit,

This code is written in the workflow. I created an after insert BR on the sc_task table as you recommended but I'm not seeing the summary in the work notes. 

Could you take a look at the BR code? I may have misread your instructions...

(function executeRule(current, previous /*null when async*/) {

var gr = new GlideRecord("sc_req_item");
gr.addQuery("sys_id", current.request_item.toString());
gr.query();

while (gr.next()) {
    // Get Owned Variables for Requested Item and sort by Order
    var ownvar = new GlideRecord('sc_item_option_mtom');
    ownvar.addQuery('request_item.number', gr.number);
    ownvar.addQuery('sc_item_option.value', '!=', '');
    ownvar.orderBy('sc_item_option.order');
    ownvar.query();

    var items = "Summary of " + gr.number + ":   " + gr.cat_item.getDisplayValue() + "\n\n";

    while (ownvar.next()) {

        var field = ownvar.sc_item_option.item_option_new;
        var fieldValue = ownvar.sc_item_option.item_option_new.name;
        // Print variable name
        items += field.getDisplayValue() + ": " + gr.variables[fieldValue].getDisplayValue() + "\n";

    }
}
current.work_notes = items;

})(current, previous);

Thank you!

Kevin

Just wanted to follow up to my previous post in case anyone else finds this helpful. Below is the final script that's working as intended now. 

(function executeRule(current, previous /*null when async*/) {

var gr = new GlideRecord("sc_req_item");
gr.addQuery("sys_id", current.request_item.toString());
gr.query();

while (gr.next()) {
    // Get Owned Variables for Requested Item and sort by Order
    var ownvar = new GlideRecord('sc_item_option_mtom');
    ownvar.addQuery('request_item.number', gr.number);
    ownvar.addQuery('sc_item_option.value', '!=', '');
    ownvar.orderBy('sc_item_option.order');
    ownvar.query();

    var items = "Summary of " + gr.number + ":   " + gr.cat_item.getDisplayValue() + "\n\n";

    while (ownvar.next()) {

        var field = ownvar.sc_item_option.item_option_new;
        var fieldValue = ownvar.sc_item_option.item_option_new.name;
        // Print variable name
        items += field.getDisplayValue() + " " + gr.variables[fieldValue].getDisplayValue() + "\n";

    }
}
	
current.work_notes = items;
	
current.update();

})(current, previous);

Thanks for your help Sumit!

Kevin

So nice of you to update the thread, thank you!