Using Different tags in ServiceNow catalog

ArunG29061990
Tera Expert

Hello Everyone,

I need to update a catalog in ServiceNow where a specific tag must be applied automatically after an incident is created.

In my Record Producer, I have a country variable with five country options. I have also created five different tags in System Definition → Tags, each with its own sys_id.

Right now, my script applies only one tag (shown below), and it gets added to every incident regardless of the selected country. What I need is country‑based tagging.
For example:

  • If the user selects Belgium, then the NW_CE_BE tag should be applied,
  • If they select Romania, then NW_CE_RO should be applied,
  • and the same logic should work for all five countries.

Below is the current example where only one hard‑coded tag is being applied:

 

// NW_CE_RO tag to use (SYS_ID is from Production SNOW)
var tagID = "d37b626883a78e14ad258598beaad31f";

 

I need help modifying this so that the correct tag is added based on the country selected in the Record Producer.

 

5 REPLIES 5

vaishali231
Tera Guru

hey @ArunG29061990 

Avoid hardcoding sys_ids because they differ between DEV / TEST / PROD. Instead, query the tag by its name.

Script

// Get selected country from Record Producer variable
var selectedCountry = producer.country; 

var tagName = "";

// Map country to tag name
switch (selectedCountry) {

case "Belgium":
tagName = "NW_CE_BE";
break;

case "Romania":
tagName = "NW_CE_RO";
break;

case "Germany":
tagName = "NW_CE_DE";
break;

case "France":
tagName = "NW_CE_FR";
break;

case "Netherlands":
tagName = "NW_CE_NL";
break;
}

// Apply tag if found
if (tagName) {

var labelGR = new GlideRecord('sys_label');
labelGR.addQuery('name', tagName);
labelGR.query();

if (labelGR.next()) {

var labelEntry = new GlideRecord('label_entry');
labelEntry.initialize();
labelEntry.label = labelGR.sys_id;
labelEntry.table = 'incident';
labelEntry.table_key = current.sys_id;
labelEntry.insert();
}
}


*************************************************************************************************************************************

If this response helps, please mark it as Accept as Solution and Helpful.

Doing so helps others in the community and encourages me to keep contributing.

 

Regards

Vaishali Singh

 

 

thanks @vaishali231 

I have tried this but not working for me. this code is not adding any tag in incident. I can manage the sys_ids in all environments so if possible and easy can we use sysids?

Using the below-given code : 
 
 
current.work_notes_list = gs.getUserID();
current.contact_type = 'self-service';
current.state = '1';
current.category = 'monitoring';
current.subcategory = 'Others';
current.u_internal_contact = gs.getUserID();

 

// Default caller/contact
current.caller_id = gs.getProperty("your.default.caller.property");

 

// Non-paying customer company
current.company = gs.getProperty("your.non_paying_company.property");

 

// Service Offering
current.service_offering = gs.getProperty("your.service_offering.property");

 

// Assignment Group
current.assignment_group = gs.getProperty("your.assignment_group.property");



// Build Short Description
current.short_description +=
    'Copy Monitoring' + " - " + producer.country + " - " +
    producer.impacted_flow + " - " +
    'Impacted Invoices: ' + producer.number_of_invoices_impacted;

 

// Build Long Description
current.description =
    'Country: ' + producer.country + "\n" +
    'Impacted Flow: ' + producer.impacted_flow + "\n" +
    'Number of Invoices Impacted: ' + producer.number_of_invoices_impacted + "\n" +
    'List of Impacted Invoices: Please view the attachment' + "\n" +
    'Error Message: ' + producer.error_message;


// Determine selected country

var selectedCountry = producer.country;

 

// Map country → Tag Name
var tagName = "";
switch (selectedCountry) {
    case "Belgium":
        tagName = "NW_CE_BE";
        break;

 

    case "Romania":
        tagName = "NW_CE_RO";
        break;

 

    case "Croatia":
        tagName = "NW_CE_HR";
        break;

 

    case "Poland":
        tagName = "NW_CE_PL";
        break;

 

    case "Malaysia":
        tagName = "NW_CE_MA";
        break;
}

 

// Apply Tag
if (tagName) {
    var labelGR = new GlideRecord('sys_label');   // Tag Definition Table
    labelGR.addQuery('name', tagName);
    labelGR.query();

 

    if (labelGR.next()) {
        var labelEntry = new GlideRecord('label_entry');
        labelEntry.initialize();
        labelEntry.label = labelGR.sys_id;
        labelEntry.table = 'incident';
        labelEntry.table_key = current.sys_id;
        labelEntry.insert();
    }
}

vaishali231
Tera Guru

hey @ArunG29061990 

add log in above script and check what errors come also share a screenshot