The CreatorCon Call for Content is officially open! Get started here.

How to insert record to the sys_template table?

JLeong
Mega Sage

Hi!

How can I insert a template record into the sys_template table via script?

Data is coming from a Service Catalog request. I've started but not sure how to proceed to insert template information.

var rec = new GlideRecord('sys_template');

rec.initialize();

rec.table = 'incident';

rec.name = current.variable_pool.inc_temp_name;

rec.short_description = current.variable_pool.inc_temp_short_desc;

// --- Template --//

rec.insert();

Thanks!

Jocelyn

1 ACCEPTED SOLUTION

If what you want to do is put the value entered in variables inside of the template field, you would need to go with something along the lines of:




var rec = new GlideRecord('sys_template');


rec.initialize();


rec.table = 'incident';


rec.name = current.variable_pool.inc_temp_name;


rec.short_description = current.variable_pool.inc_temp_short_desc;


// --- Template --//



rec.template = "category=" + current.variable_pool.inc_temp_category + "^subcategory=" + current.variable_pool.inc_temp_subcategory; //im adding this part



rec.insert();



This is just an example and it requires the variables on your catalog item to be references to the actual sys_choice records of the incident category and subcategory fields. If you want to hardcode the sys_id in there,you could go with something like:


rec.template = "category=sys_id^subcategory=sys_id";


View solution in original post

9 REPLIES 9

Philippe Simard
Kilo Expert

Hi Jocelyn,



The template field on the sys_template stores the information under the following format:


company=sysID^cmdb_ci=sysID^assignment_group=sysID^EQ



So if you are to dynamically generate a template based on the variables of the requested item, you would need to generate a String that follows this structure.


If you want more examples, you can go to a template record and simple use the "Show XML" action to view how the value is stored.



Phil,


Cheers


Inactive_Us1474
Giga Guru

Can you check what is the value of the below


current.variable_pool.inc_temp_name in log ? Try using current.variables.variable_name





Try using encoded query like below to populate data :


//Template


rec.template = "active=true^description=test^short_description=best thing"; //Don't use ^EQ


Hi Akhil,



The value of "current.variable_pool.inc_temp_name " is the name of the template provided in the service request form.



Right now, only 2 fields are being populated: Name and Short Description.




Not sure how to call the Template section.



Thanks!