How to populate "sys_template" fields to incident fields in inbound action

sinu2
Tera Expert

HI all,

 

I have a requirement that "sys_template" fields values needs to populate on "incident" inbound email action

Please find the below script for that but it is not working..This script i have written in my inbound action (this inbound placed in incident table)

u_template is a reference field in incident table

var template_id = '4e0f98cddbbbab0022f5d404ca9619e9';
var systemp = new GlideRecord('sys_template');
systemp.addQuery('sys_id',template_id);
systemp.query();
gs.addInfoMessage("number of templates:"+systemp.getRowCount());
if(systemp.next()){
var IncRec = new GlideRecord('incident');
IncRec.addQuery('u_template.sys_id',template_id );
IncRec.query();
gs.addInfoMessage("temp sys_id is:"+IncRec);
gs.addInfoMessage("number of templates:"+IncRec.getRowCount());

if(IncRec.next()){
IncRec.state = systemp.state;
gs.addInfoMessage("checking template state:"+IncRec.state);
IncRec.short_description = systemp.short_description;
gs.addInfoMessage("checking template short description:"+IncRec.short_description);

var id = IncRec.insert();
}

}

1 ACCEPTED SOLUTION

Hi Srisk,

Actually, you can use a OOB Function ApplyTemplate to apply it to your incident. Here is how you can do this.

var template_name = 'Your Template Name'; // in place of sys_id mention your template name.

var IncRec = new GlideRecord('incident');
IncRec.applyTemplate(template_name); 
IncRec.update();

Mark the comment as a correct answer and also helpful once worked.

View solution in original post

15 REPLIES 15

If you want to set values for that template, then you don't' need to use addQuery in the incident table. you can simply check if the template exists, if yes then simply create a incident and set the template.

Try the below code.

var template_id = '4e0f98cddbbbab0022f5d404ca9619e9';
var systemp = new GlideRecord('sys_template');
systemp.addQuery('sys_id',template_id);
systemp.query();
gs.log("number of templates:"+systemp.getRowCount());
if(systemp.next()){
  var IncRec = new GlideRecord('incident');
  IncRec.initialize();
  IncRec.state = systemp.state;
  IncRec.u_template = systemp.sys_id; //you can also use tmeplate_id directly here, but best not to hard code.
  gs.log("checking template state:"+IncRec.state);
  IncRec.short_description = systemp.short_description;
  gs.log("checking template short description:"+IncRec.short_description);
  var id = IncRec.insert();
}

Mark the comment as a correct answer and also helpful once worked.

Sure Asif... I will try this and let you know

Asif, I have tried your script with removing addQuery still i am able to get script execute up to this log

gs.log("number of templates:"+systemp.getRowCount());

Hi,

did you use the latest code that I have given?

If yes, then i think the rowCount might be 0 and not 1. can you check that once?

 

Yes i have checked asif the row count is comming 1 not 0