How to add record watcher in scripted list menu item

Naga23
Giga Expert

Hi All,

I have created one Scripted List menu item which shows list of incidents which are in new state. Please find below script and its working fine. Here I wanted to filterout the incidents which are not in new state and I would like to refresh it by using record watcher. Can anyone help me by how to add record watcher in below script.

 

var items = data.items = [];
var max = 30;
var u = gs.getUserID();
data.count = 0;
data.omitBadge = false;
var gr = new GlideRecord('incident');
gr.orderByDesc('number');
gr.setLimit(max);
gr.addEncodedQuery('state=1');
gr.query()
while (gr.next())
{
var title = gr.getValue('number') + " : " + gr.assignment_group.getDisplayValue() + " : " + gr.short_description;
add(title, gr.getValue('sys_id'));
}
function add(title, sysid)
{
data.count++;
var link = {};
link.title = title;
link.type = 'link';
link.href = '?id=form&table=incident&view=ess&sys_id=' + sysid;
items.push(link);
}
1 ACCEPTED SOLUTION

Oleg
Mega Sage

You need fill data.record_watchers with array, which items have properties tablefilter and display. For example,

data.record_watchers = [
    {
        table: "incident",
        filter: "state=1^ORDERBYDESCnumber",
        display: "short_description"
    }
];

A small problem - the value of display can't be more complex as field name or dot-working string with path to a field. Thus I used "short_description" in the above example. You can replace it to "number", but I don't know more complex template, which will be interpreted by Service Portal.

Additionally, I'd recommend you to wrap your code in function if you declare at least one variable. The variables will be global and can produce side effects with other parts of your or Service Portal code. Simple modification of your code could look as following:

(function (data) {

    var max = 30;
    var grInfo = {
        table: "incident",
        filter: "state=1^ORDERBYDESCnumber",
        display: "short_description"
    };
    var gr = new GlideRecord(grInfo.table);
    gr.addEncodedQuery(grInfo.filter);
    gr.setLimit(max);
    gr.query()
    var items = [], title;
    while (gr.next()) {
        title = gr.getValue('number') + " : " +
                gr.assignment_group.getDisplayValue() + " : " +
                gr.short_description;
        items.push({
            title: title,
            type: 'link',
            href: '?id=form&table=incident&view=ess&sys_id=' + gr.getUniqueValue()
        });
    }

    data.record_watchers = [ grInfo ];
    data.omitBadge = false;
    data.items = items;
    data.count = items.length;

})(data);

 

View solution in original post

1 REPLY 1

Oleg
Mega Sage

You need fill data.record_watchers with array, which items have properties tablefilter and display. For example,

data.record_watchers = [
    {
        table: "incident",
        filter: "state=1^ORDERBYDESCnumber",
        display: "short_description"
    }
];

A small problem - the value of display can't be more complex as field name or dot-working string with path to a field. Thus I used "short_description" in the above example. You can replace it to "number", but I don't know more complex template, which will be interpreted by Service Portal.

Additionally, I'd recommend you to wrap your code in function if you declare at least one variable. The variables will be global and can produce side effects with other parts of your or Service Portal code. Simple modification of your code could look as following:

(function (data) {

    var max = 30;
    var grInfo = {
        table: "incident",
        filter: "state=1^ORDERBYDESCnumber",
        display: "short_description"
    };
    var gr = new GlideRecord(grInfo.table);
    gr.addEncodedQuery(grInfo.filter);
    gr.setLimit(max);
    gr.query()
    var items = [], title;
    while (gr.next()) {
        title = gr.getValue('number') + " : " +
                gr.assignment_group.getDisplayValue() + " : " +
                gr.short_description;
        items.push({
            title: title,
            type: 'link',
            href: '?id=form&table=incident&view=ess&sys_id=' + gr.getUniqueValue()
        });
    }

    data.record_watchers = [ grInfo ];
    data.omitBadge = false;
    data.items = items;
    data.count = items.length;

})(data);