How to add tags in record while importing records from Data Source

Shreya Nagar Na
Tera Contributor

I want to add tags in records when records imported into target table 

4 REPLIES 4

p_kanczugowski
Tera Guru

Hi @Shreya Nagar Na, tag instances, once referred to as "labels", are stored in in [label_entry] table. Their definitions are created on [label] table. Hence, if you'd like to assign a tag to a record, you first need to get/create a [label], and then create the actual entry for the record:

 

{
    "table": "incident",
    "label": "08785c6e68fa1410f8776f44f8b2e456", // label_entry reference
    "table_key": "80eb579d53671010968addeeff7b1215", // id of the record
    "title": "Title of the Tag"
}

 

Please mark this response as useful and the question as solved if the issue has been solved. Thank you!

Mark Manders
Giga Patron

Tags are stored in the 'label' table and the relation to the record is done through the label_entry table. So your data source will need to import the tags to the label_entry table and attach them there. First import the records, so they exist and then add the label_entries to the correct record.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

DenisS578111483
Tera Contributor

I've had the same requirement and solved it like this:

onAfter Transform Script

 

(function runTransformScript(source, map, log, target /*undefined onStart*/) {

	// Import Tags
	if (source.u_stichwƶrter) {
		var tagsArr = source.u_stichwƶrter.split(',');
		tagsArr.forEach(function (tag) {

			var grL = new GlideRecord('label');
			grL.addQuery('name', tag);
			grL.addQuery('viewable_by', 'everyone');
			grL.query();
			var sysid;
			if (grL.next()) {
				sysid = grL.getUniqueValue();
			} else {
				var grL2 = new GlideRecord('label');
				grL2.initialize();
				grL2.name = tag;
				grL2.viewable_by = 'everyone';
				sysid = grL2.insert();
			}

			// Check if such tag entry already exists in table
			var grLE = new GlideRecord('label_entry');
			grLE.addQuery('table', 'x_suva_ideen_manag_ideen');
			// SysID of idea entry
			grLE.addQuery('table_key', target.getUniqueValue());
			// SysID of label
			grLE.addQuery('label', sysid);
			grLE.query();
			if (grLE.next()) {
				// Do nothing, tag already exists
			} else {
				// Create new tag
				var grLE2 = new GlideRecord('label_entry');
				grLE2.initialize();
				grLE2.table = 'x_suva_ideen_manag_ideen';
				grLE2.table_key = target.getUniqueValue();
				grLE2.label = sysid;
				grLE2.insert(); // Inserts the record into the database
			}
		});
	}

})(source, map, log, target);

DenisS578111483
Tera Contributor

I've had the same requirement and solved it like this:

onAfter Transform Script.
In my case, the tags have been saved in the variable 'u_stichwƶrter' of the import set table (comma-seperated).

 

(function runTransformScript(source, map, log, target /*undefined onStart*/) {

	// Import Tags
	if (source.u_stichwƶrter) {
		var tagsArr = source.u_stichwƶrter.split(',');
		tagsArr.forEach(function (tag) {

			var grL = new GlideRecord('label');
			grL.addQuery('name', tag);
			grL.addQuery('viewable_by', 'everyone');
			grL.query();
			var sysid;
			if (grL.next()) {
				sysid = grL.getUniqueValue();
			} else {
				var grL2 = new GlideRecord('label');
				grL2.initialize();
				grL2.name = tag;
				grL2.viewable_by = 'everyone';
				sysid = grL2.insert();
			}

			// Check if such tag entry already exists in table
			var grLE = new GlideRecord('label_entry');
			grLE.addQuery('table', 'my_table');
			// SysID of idea entry
			grLE.addQuery('table_key', target.getUniqueValue());
			// SysID of label
			grLE.addQuery('label', sysid);
			grLE.query();
			if (grLE.next()) {
				// Do nothing, tag already exists
			} else {
				// Create new tag
				var grLE2 = new GlideRecord('label_entry');
				grLE2.initialize();
				grLE2.table = 'my_table';
				grLE2.table_key = target.getUniqueValue();
				grLE2.label = sysid;
				grLE2.insert(); // Inserts the record into the database
			}
		});
	}

})(source, map, log, target);