Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Pranesh072
Mega Sage

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

find_real_file.png

 

 

Currently this method is working for most fields.

 

Let me know in the comments if you have any questions or suggestions.

Comments
Luiz Lucena
Mega Sage

Awesome job, Pranesh!

Mikuro
Tera Expert

Just wanted to say thank you for this. My company uses a regular applytemplate solution back in the New Call (Service Desk Call) module for last couple years. Since ServiceNow is no longer supporting New Call soon we decided to begin moving to Agent Workspace and Interactions.

Without your code here to apply a template in Workspace I would have 100% never been able to complete this requirement. You saved me from an immense amount of headache. Thanks!

Version history
Last update:
‎01-12-2021 11:32 PM
Updated by: