applyTemplate on Agent Workspace/Portal
Import
·
Jan 13, 2021
·
article
Hello Everyone,
In this article, I will discuss about the applyTemplate for Agent Workspace/Portal.
Purpose of applyTemplate
It is a client side function use to populate the template values on form which currently works on native UI and not supported on Agent Workspace/Portal.
Workaround for Agent Workspace/Service Portal
Add the following functions in the client script
function applyTemplateWorkspace(template_sysid){
var ga=new GlideAjax('AjaxClientHelper');
ga.addParam('sysparm_name','getValues');
ga.addParam('sysparm_sys_id',template_sysid);
ga.addParam('sysparm_current_table_name',g_form.getTableName());
ga.getXML(UserData1);
}
function UserData1(response) {
var answerA = response.responseXML;
var items = answerA .getElementsByTagName('item');
for (var i = 0; i < items.length; i++) {
var item = items[i];
var name = item.getAttribute('name');
if(!g_form.isReadOnly(name))
applyItem(name,item.getAttribute('value'));
}
}
function applyItem (element, value) {
if (value == null || value == 'null')
return;
g_form.setValue(element, value);
g_form.hideFieldMsg(element);
g_form.showFieldMsg(element,"This field modified by template","info");
}
and use applyTemplateWorkspace inplace of applyTemplate in client script
applyTemplateWorkspace(<template_sys_id>); //applyTemplate(<template_sys_id>);
Sample client script
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var grTemplate = new GlideRecord('sys_template');
grTemplate.addQuery('sys_id','acd87481d7930200f2d224837e6103f3');
grTemplate.query(templateResponse);
function templateResponse(template) {
if (template.next()) {
//applyTemplate(template.sys_id)
applyTemplateWorkspace(template.sys_id);
}
}
}
function applyTemplateWorkspace(template_sysid){
var ga=new GlideAjax('AjaxClientHelper');
ga.addParam('sysparm_name','getValues');
ga.addParam('sysparm_sys_id',template_sysid);
ga.addParam('sysparm_current_table_name',g_form.getTableName());
ga.getXML(UserData1);
}
function UserData1(response) {
var answerA = response.responseXML;
var items = answerA .getElementsByTagName('item');
for (var i = 0; i < items.length; i++) {
var item = items[i];
var name = item.getAttribute('name');
if(!g_form.isReadOnly(name))
applyItem(name,item.getAttribute('value'));
}
}
function applyItem (element, value) {
if (value == null || value == 'null')
return;
g_form.setValue(element, value);
g_form.showFieldMsg(element,"This field modified by template","info");
}
Result
Currently this method is working for most fields.
Let me know in the comments if you have any questions or suggestions.
View original source
https://www.servicenow.com/community/now-platform-articles/applytemplate-on-agent-workspace-portal/ta-p/2321483