Using Templates (sys_template records) for record producers in the Service Portal
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2019 05:04 AM
I am currently working on a requirement for a customer that would like me to look into using templates from the Service Portal.
The scenario would be:
(1) User selects an item from the Service Portal that is a record producer.
(2) On the 'sc_cat_item' page there is a drop-down list of available Templates that user can select from.
(3) Selecting the template will populate the fields on the form (for example, the short_description field), and the user now just needs to hit 'Submit'.
At this point with some experimenting I have created a widget that displays the list of Templates I want the user to select from:
Selecting a value I can get data back like the sys_id and template info for the selected sys_template record:
Note that Template SysId and Template Info fields are ones created in my widget's HTML Template section.
Looking at my widget code so far I have:
Widget HTML Template
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">Template Selector</h4>
</div>
<div class="panel-body">
<sn-record-picker field="my_template" table="'sys_template'" default-query="'nameSTARTSWITHGEN-'" display-field="'name'" value-field="'sys_id'" search-fields="'name'" page-size="100" ></sn-record-picker>
<form class="m-t form-horizontal" role="form">
<div class="form-group">
<label class="col-sm-2 control-label">Template SysId</label>
<div class="col-sm-10">
<input type="text" placeholder="{{data.myTemp.sys_id}}" class="form-control" readonly>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Template Template Info</label>
<div class="col-sm-10">
<input type="text" placeholder="{{data.myTemp.template}}" class="form-control" readonly>
</div>
</div>
</form>
</div>
</div>
Widget Client Script
function($scope, spUtil) {
var c = this;
$scope.my_template = {
displayValue: c.data.myTemp.name,
value: c.data.myTemp.sys_id,
name: 'my_template'
};
$scope.$on("field.change", function(evt, parms) {
if (parms.field.name == 'my_template'){
c.data.setMyTemplate = parms.newValue;
}
c.server.update().then(function(response) {
spUtil.update($scope);
})
});
}
Widget Server Script
var templateSysId = "";
if (input.setMyTemplate)
templateSysId = input.setMyTemplate;
var templateGR = GlideRecord('sys_template');
templateGR.get(templateSysId);
data.myTemp = {};
$sp.getRecordDisplayValues(data.myTemp, templateGR, 'sys_id,template');
Now that I have the sys_id of the template and the template field, I want to set my fields on my intake form with the values I am pulling back in the 'Template Info' field, by splitting at the '^' and setting the fields accordingly.
I am not sure where to go from here??? Is there a way I can leverage "$scope.page.g_form.setValue" in my widget's Client Script to set my 'short_description' variable??
Any assistance or guidance will be greatly appreciated.
Thanks in advance!!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2019 08:41 AM
I figured it out 🙂
On my record producer I added a Variable of type "Macro". In the 'Type Specifications' section I referenced the Widget I built.
In the record producer I also created a "single line text" field to hold the 'template_sysid'. I have a UI policy in place to hide this sys_id field as it's only a working variable that I will be using.
Now, in my widget's Client Script I can do a setValue:
function($scope, spUtil) {
var c = this;
$scope.my_template = {
displayValue: c.data.myTemp.name,
value: c.data.myTemp.sys_id,
name: 'my_template'
};
$scope.$on("field.change", function(evt, parms) {
if (parms.field.name == 'my_template'){
c.data.setMyTemplate = parms.newValue;
}
$scope.page.g_form.setValue('template_sysid', c.data.setMyTemplate);
c.server.update().then(function(response) {
spUtil.update($scope);
})
});
}
This will set the template_sysid field variable for me on selection of the template from the drop-down of templates.
Once this template_sysid field is set i run a catalog client script to pull the template record and set the values on the catalog item.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//g_form.setValue('short_description',newValue);
//need to pull the template info based on the template_sysid
var gr_template = new GlideRecord('sys_template');
gr_template.addQuery('sys_id', newValue);
gr_template.query(template_info_callback);
function template_info_callback(gr_template){
var fields = [];
var field_name = [];
if(gr_template._next()){
var template_str = gr_template.template;
fields = template_str.replace('^EQ','').split('^');
for(var inx = 0; inx < fields.length; inx++){
field_name = fields[inx].split('=');
g_form.setValue(field_name[0], field_name[1]);
}
}
}
}
End result is this:
Template selector drop-down in the record producer:
Upon selection, set the fields on my record producer form!
Catalog script still needs some cleanup to clear fields prior to setting fields... but the above will give the idea.
The only issue I'm having now, it works when i'm an admin, but not as a regular user, or even a user with itil role... getting closer!
I checked ACL on the sys_template table and that looked okay... need to figure the constraints not allowing a user to see the values in the drop-down.
Any ideas would be great!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2020 03:10 AM
Hello
Can you please tell me if you are able to achieve this, i also have the similar requirement.
Please assist me on this!
Thanks,
Prerana
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2021 08:33 AM
Hi,
Did you manage to resolve this issue? I have a similar request to upload values from a template into a record producer. Maybe a script include would work.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-06-2022 02:39 PM
Hi - following up to see if you were ultimately successful with this? I'm trying to do the same thing. I'm mainly concerned about the 'itil' thing - that's a must for me.