Automate a tag from a record producer to incident

JR1
Tera Guru

All, 

 

I am struggling with this and need some help. I am trying to pass a Tag value captured from a record producer (field name tag, mapped to the Tags (sys_tags) incident field as a lookup select box, looking up from the Label table, lookup value is name, and label is name with a variable attribute of the following: map_to_field=tag

 

The idea is to give users submitting this record producer the ability to select a predefined set of global tags, to add it to the incident when submitted. 

I am trying to achieve this with the following script and failing miserably. If you can review and suggest solutions I would be appreciative. 

I want to be able to pass one single tag from the predefined list. The business rule has conditions to fire, if one of those tags are selected. 

and the script is thus:

 

// Define specific predefined tags
var predefinedTags = ['Mobile', 'Deerfield', 'DC', 'San Francisco', 'Rockville', 'Dublin', 'Waterford'];
// For testing purposes, set the tagValue to a predefined tag directly
//var tagValue = 'Mobile';

// Get the tag value provided in the record producer
var tagValue = current.variables['sys_tags']; // Replace 'tag_field' with the actual field name

// Trim whitespace from the provided tag value
tagValue = tagValue.trim();

// Check if the provided tag value is one of the predefined tags
if (predefinedTags.indexOf(tagValue) !== -1) {
// The provided tag matches one of the predefined tags

// Query for existing tags with a case-insensitive 'name' matching the current tag
var grTag = new GlideRecord('label');
grTag.addQuery('name', 'LIKE', tagValue, 'COLLATE Latin1_General_BIN');
grTag.query();

if (!grTag.next()) {
// If the tag doesn't exist, create it
grTag.initialize();
grTag.setValue('name', tagValue);
grTag.setValue('viewable_by', 'everyone');
grTag.insert();
}

// Create a label entry for the current record and tag
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.setValue('title', current.number);
grTagEntry.insert();
} else {
// The provided tag does not match any of the predefined tags, handle accordingly
gs.warn('Provided tag (' + tagValue + ') does not match any predefined tags.');

gs.info('Provided tag value: ' + tagValue);
}

1 ACCEPTED SOLUTION

Peter Bodelier
Giga Sage

Hi @JR1,

 

Try it like this:

 

// Define specific predefined tags
var predefinedTags = ['Mobile', 'Deerfield', 'DC', 'San Francisco', 'Rockville', 'Dublin', 'Waterford'];
// For testing purposes, set the tagValue to a predefined tag directly
//var tagValue = 'Mobile';

// Get the tag value provided in the record producer
var tagValue = current.variables['sys_tags']; // Replace 'tag_field' with the actual field name

// Trim whitespace from the provided tag value
tagValue = tagValue.trim();

// Check if the provided tag value is one of the predefined tags
if (predefinedTags.indexOf(tagValue) !== -1) {
// The provided tag matches one of the predefined tags

// Query for existing tags with a case-insensitive 'name' matching the current tag
var grTag = new GlideRecord('label');
grTag.addQuery('name', 'LIKE', tagValue);
grTag.query();
var grTagID;
if (grTag.next()){
grTagID = grTag.getUniqueValue();
}
else {
// If the tag doesn't exist, create it
grTag.initialize();
grTag.setValue('name', tagValue);
grTag.setValue('viewable_by', 'everyone');
grTagID = grTag.insert();
}


// Create a label entry for the current record and tag
var grTagEntry = new GlideRecord('label_entry');
grTagEntry.initialize();
grTagEntry.setValue('label', grTagID);
grTagEntry.setValue('table', current.getTableName());
grTagEntry.setValue('table_key', current.getUniqueValue());
grTagEntry.setValue('title', current.number);
grTagEntry.insert();
} else {
// The provided tag does not match any of the predefined tags, handle accordingly
gs.warn('Provided tag (' + tagValue + ') does not match any predefined tags.');

gs.info('Provided tag value: ' + tagValue);
}

 

Not really sure what you where trying here, so removed the last bit:

grTag.addQuery('name', 'LIKE', tagValue, 'COLLATE Latin1_General_BIN');


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

View solution in original post

1 REPLY 1

Peter Bodelier
Giga Sage

Hi @JR1,

 

Try it like this:

 

// Define specific predefined tags
var predefinedTags = ['Mobile', 'Deerfield', 'DC', 'San Francisco', 'Rockville', 'Dublin', 'Waterford'];
// For testing purposes, set the tagValue to a predefined tag directly
//var tagValue = 'Mobile';

// Get the tag value provided in the record producer
var tagValue = current.variables['sys_tags']; // Replace 'tag_field' with the actual field name

// Trim whitespace from the provided tag value
tagValue = tagValue.trim();

// Check if the provided tag value is one of the predefined tags
if (predefinedTags.indexOf(tagValue) !== -1) {
// The provided tag matches one of the predefined tags

// Query for existing tags with a case-insensitive 'name' matching the current tag
var grTag = new GlideRecord('label');
grTag.addQuery('name', 'LIKE', tagValue);
grTag.query();
var grTagID;
if (grTag.next()){
grTagID = grTag.getUniqueValue();
}
else {
// If the tag doesn't exist, create it
grTag.initialize();
grTag.setValue('name', tagValue);
grTag.setValue('viewable_by', 'everyone');
grTagID = grTag.insert();
}


// Create a label entry for the current record and tag
var grTagEntry = new GlideRecord('label_entry');
grTagEntry.initialize();
grTagEntry.setValue('label', grTagID);
grTagEntry.setValue('table', current.getTableName());
grTagEntry.setValue('table_key', current.getUniqueValue());
grTagEntry.setValue('title', current.number);
grTagEntry.insert();
} else {
// The provided tag does not match any of the predefined tags, handle accordingly
gs.warn('Provided tag (' + tagValue + ') does not match any predefined tags.');

gs.info('Provided tag value: ' + tagValue);
}

 

Not really sure what you where trying here, so removed the last bit:

grTag.addQuery('name', 'LIKE', tagValue, 'COLLATE Latin1_General_BIN');


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.