Scripted Tags - ServiceNow Community
Mark Roethof
Tera Patron
Tera Patron

Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

 

Hi there,

 

Tags on records have been in ServiceNow for a while. Though questions about how Tags actually work and if these can be automated are still very common. Recently I applied scripted Tags to a custom app, so I've extracted this part from the App and gave it a go on describing the how-to here.

Tags

As mentioned, Tags are not new. They've been around for a while. Good to know, Tags will be found on lists though not on forms. Tags are also available on (almost) all tables, and immediately available on custom tables.

 

find_real_file.png

Data structure

The example above concerns the Incident table. The Tags are actually not stored on the Incident table. Instead, the tags are stored in the Tag table [lable]. The relationship between the Tag and - in case of the example - the Incident table, is stored in the Label Entries table [label_entry].

 

Key to know:
- Tag [label], "Name" which is unique in case of "Viewable by" other than "Me", and type is "Standard";
- Label Entry [label_entry], which has a reference (Label) to the Tag table, and two string fields (Table and Table key) which contain the Table name and the Sys Id of the record where the Tag is on. In the example above the Table would be "incident". The Target field on the Label Entry will be populated automatically.

Scripting Tags on records

Time to translate the above structure into scripting. For example, we could set up a Business Rule which runs on insert of an Incident. This could well be achieved through the conditions.

 

find_real_file.png

 

For testing, let's say the label we automatically want to add is the "Community article" label.

 

// Comma seperated list of Tags
var tagsArr = 'Community article';

// Iterate thru the Array and create the Tags and Label Entries
tagsArr = tagsArr.split(',');

for(var i = 0; i < tagsArr.length; i++) {
	var tagSysId = '';

	var grTag = new GlideRecord('label');
	grTag.addActiveQuery();
	grTag.addQuery('name', tagsArr[i]);
	grTag.addQuery('type', 'standard');
	grTag.addQuery('viewable_by', '!=', 'me');
	grTag._query();

	if(grTag._next()) {
		tagSysId = grTag.getUniqueValue();
	} else {
		grTag.initialize();
		grTag.setValue('name', tagsArr[i]);
		grTag.setValue('viewable_by', 'everyone');
		grTag.insert();

		tagSysId = grTag.getUniqueValue();
	}

	var grTagEntry = new GlideRecord('label_entry');
	grTagEntry.initialize();
	grTagEntry.setValue('label', grTag.getUniqueValue());
	grTagEntry.setValue('table', current.getTableName());
	grTagEntry.setValue('table_key', current.getUniqueValue());
	grTagEntry.insert();
}

 

The script itself isn't that huge and difficult. Only two catches:
1) Multiple Tags could be entered;
2) To check if the Tag already exists. If so, only a relation (Label Entry) to be created. If not, both a Tag and a relation to be created.

---

And that's it actually. Hope you like it. If any questions or remarks, let me know!

 

C

If this content helped you, I would appreciate it if you hit bookmark or mark it as helpful.

 

Interested in more Articles, Blogs, Videos, Podcasts, Share projects I shared/participated in?
- Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

 

Kind regards,


Mark Roethof

ServiceNow Technical Consultant @ Paphos Group
---

LinkedIn

Comments
Robert Gentry
Kilo Contributor

Mark, Thanks for the article.

I'm brand new to ServiceNow and I am trying to find a way to report on tickets "Incorrectly Routed" to our team.

We report daily and I thought Tags would be good but I can't figure out how to report only on tickets that were marked (tagged) IR today.

Any thoughts? Is Tags the way to go?

Thanks for any help you can offer.

Arjun5
Tera Contributor

This (grTagEntry.setValue('label', grTag.getUniqueValue());) should be grTagEntry.setValue('label', tagSysId); 

 

Otherwise, helpful article thanks

Gokul Nath
Kilo Sage

Tried out same script using auto Mapping based on Category

 

TYPEMAPPING = {
        'software': 'Software Incident',
        'Hardware': 'Hardware Incident'
    };


    // Comma seperated list of Tags
    var tagsArr = (TYPEMAPPING[current.getValue('category')]).toString();
	gs.addInfoMessage(current.category);
	
    // Iterate thru the Array and create the Tags and Label Entries
    tagsArr = tagsArr.split(',');

    for (var i = 0; i < tagsArr.length; i++) {
        var tagSysId = '';

        var grTag = new GlideRecord('label');
        grTag.addActiveQuery();
        grTag.addQuery('name', tagsArr[i]);
        grTag.addQuery('type', 'standard');
        grTag.addQuery('viewable_by', '!=', 'me');
        grTag._query();

        //Check if tag is available or not else create tag

        if (grTag._next()) {
            tagSysId = grTag.getUniqueValue();
        } else {
            grTag.initialize();
            grTag.setValue('name', tagsArr[i]);
            grTag.setValue('viewable_by', 'everyone');
            grTag.insert();

            tagSysId = grTag.getUniqueValue();
        }
    }
    //Log a entry in the table for the record and the created/Available label

        var grTagEntry = new GlideRecord('label_entry');
        grTagEntry.initialize();
        grTagEntry.setValue('label', tagSysId);
        grTagEntry.setValue('table', current.getTableName());
        grTagEntry.setValue('table_key', current.getUniqueValue());
        grTagEntry.insert();
amol_joshi
Tera Contributor

This is really good. Basically to anyone who is new, the "Label" is a reference field in "Label Entry" table and it points to the "Label" table (tags). Whatever is in this field, shows up as a tag against something like a user or an incident. I have tested it with sys_user table successfully.

an261998
Tera Explorer

Hi Guys,

I am new in ServiceNow and required you little help.

I was trying to fetch data which is present in string into tag and it's working but the issue is when I am trying to see it on list view it show empty and by encode query it's working.

 

Could you guys suggest me what to do in this case

Version history
Last update:
‎08-10-2024 08:14 AM
Updated by:
Contributors