Using Templates (sys_template records) for record producers in the Service Portal

dooks99
Kilo Contributor

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:

find_real_file.png

Selecting a value I can get data back like the sys_id and template info for the selected sys_template record:

find_real_file.png

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

find_real_file.png

<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

find_real_file.png

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

find_real_file.png

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!!!

4 REPLIES 4

dooks99
Kilo Contributor

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:

find_real_file.png

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.

find_real_file.png

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:

find_real_file.png

 

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!

 

 

 

Hello @dooks99 ,

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

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.

dreinke
Tera Contributor

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.