How to get information from one template and past the value in another template (Incident to Case)

BlGeorgiev
Tera Contributor

Since I tried to find information in many places on how to take everything stored in one template and move it as information to another template for another table and found nothing similar, I decided to delve deeper and try to automate the process. In my case, I had to take all the templates from the incident table and make the same templates, but for the Case table. As you know, after Insert and Stay, if the fields are the same, the values for the fields are retained, but going through each template and doing this manually is not cost-effective. So I created the following script

 

Remark:

You cannot query more than one record from the sys_template table using standard while(gr.next()) syntax because the table contains a column literally named "next". This column conflicts with the GlideRecord .next() function, causing the script to fail after the first record

var getTemplate= new GlideRecord('sys_template');
getTemplate.addQuery('active', true);
getTemplate.query();// Use _next() instead of next()
while(getTemplate._next()){
    gs.print(getTemplate.name);
}

 

 

var templateList = "";
var splitListFields = "";
var separateFieldValues = "";
var table = 'sn_customerservice_case';

// Get source template
var getT = new GlideRecord('sys_template');
getT.get('sys_id', 'your_sys_id'); 

//you can modify your query to get more than one record

 

templateList = getT.getValue("template");
splitListFields = templateList.split("^");

var templateFields = {};

// Create a object for fields part of current template (name and values)


// Set all field values
for (var i = 0; i < splitListFields.length; i++) {
if (splitListFields[i].includes("=")) {
separateFieldValues = splitListFields[i].split("=");
var key = separateFieldValues[0];
var value = separateFieldValues[1];

// Add key/value to object
templateFields[key] = value;
}
}
var templateString = '';

//I had differences in the fields, so I had to check fields from Incident to be equal with fields in Case

//u_category field name of the Incident table == to  u_case_service_interruption_category where i store categories
var fieldMap = { 
'u_category': 'u_case_service_interruption_category',
'u_subcategory': 'u_case_subcategory'
};

for (var field in templateFields) {
if (templateFields.hasOwnProperty(field)) {
var mappedField = fieldMap[field] || field;

 

// use mapped name if exists, else original


templateString += mappedField + '=' + templateFields[field] + '^'; 

 

// here is the most important part of the query ootb fields are comming in following way short_description='TEST'^Impact='3' that's way we need to have ^ at the end
}
}

// Remove trailing ^ 
templateString = templateString.slice(0, -1);

var tpl = new GlideRecord('sys_template');
tpl.table = table;
tpl.short_description = getT.short_description;
tpl.name = getT.name;
tpl.contact_type = 'Web'
tpl.sys_domain = getT.sys_domain;
tpl.user = getT.user;
tpl.groups = getT.groups;
tpl.active = true;
tpl.template = templateString;
tpl.insert();

1 REPLY 1

Mark Manders
Mega Patron

Why? You will have templates that nobody uses, because Cases and Incidents are completely different records. They use different (mandatory) fields. Spending a bit of time to do this correct, will make sure you don't end up with useless or even incomplete/wrongly used templates.


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