Passing variables from interatcion record to catalog item in Agent Workspace

mslocum
Mega Expert

We recently started using the ITSM Agent workspace and would like to pass the opened for variable on the interaction record to a a variable thats apart of a variable set on our catalog items. How can I pass a variable from the interaction record to a catalog item when an agent presses the create request button on the interaction record?

1 ACCEPTED SOLUTION

We used a default variable set for requestor information on all of our catalog items so we ended up doing a few things to make this work. Ill post the code below:

Script Include:

Client Callable:True

Accessible from: All application scopes

Name: RequestLinkSessionManager

var RequestLinkSessionManager = Class.create();
RequestLinkSessionManager.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	setComponentSessionValueMapJSON: function(session_data) {
		session_data = session_data || this.getParameter("workspace_request_session_data");

		// Set the session data
		gs.getSession().putClientData('workspace_request_session_data', session_data);

		return session_data;
	},

	getComponentSessionValueMapJSON: function() {
		var session_data = gs.getSession().getClientData("workspace_request_session_data");
		var parsed_session_data = global.JSON.parse(session_data);

		var tableName = parsed_session_data.sysparm_parent_table;
		var sysId = parsed_session_data.sysparm_parent_sys_id;
		var tableRequestedForFieldName = "requested_for";
		var response = {};

		var grMapping = new GlideRecord('request_parent_mapping');
		grMapping.addQuery("parent_table", tableName);
		grMapping.query();
		
		if(grMapping.next()){
			tableRequestedForFieldName = grMapping.getValue("requested_for_field");
		}

		var grCurrent = new GlideRecord(tableName);
		if(grCurrent.get(sysId)){
			response.requested_for = grCurrent.getValue(tableRequestedForFieldName);
		}

		// No longer needed, purge.
		gs.getSession().putClientData("workspace_request_session_data", "");

		var responseJSON = JSON.stringify(response);
		return responseJSON;
	},
	type: 'RequestLinkSessionManager'
});

 

Create Request UI Action Workspace Script:

function onClick() {
                var params = {};
                                params.sysparm_parent_table = "interaction";
                                params.sysparm_parent_sys_id = g_form.getUniqueValue();
 
                var paramsJSON = JSON.stringify(params);
                var request = new GlideAjax('RequestLinkSessionManager');
                request.addParam('sysparm_name', 'setComponentSessionValueMapJSON');
                request.addParam('workspace_request_session_data', paramsJSON);
                request.getXMLAnswer(function(answer){
                                onSubmitAfterSessionPut(params);
                });
}
 
function onSubmitAfterSessionPut(params) {
                var result = g_form.submit('sysverb_ws_save');
                if (!result) { // failed form submission
                                return;
                }
                result.then(function() {
                                // var params = {};
                                // params.sysparm_parent_table = "interaction";
                                // params.sysparm_parent_sys_id = g_form.getUniqueValue();
                                g_service_catalog.openCatalogItem('sc_cat_item', '-1', params);
                });
}

 

Catalog Client Script: This went on our variable sets for our requestor information

UI Type: Mobile/Service Portal

Type: onLoad

Applies on a Catalog Item View: True

function onLoad() {
	
	try{
		// Get the session data
		// var component_link_json = g_user.getClientData('lds_itp_request_link');
		var ga = new GlideAjax("RequestLinkSessionManager");
		ga.addParam("sysparm_name","getComponentSessionValueMapJSON");
		ga.getXMLAnswer(parseAjaxResponse);
	} catch(e){
		//                            console.log("There was an error parsing the request link data: " + e);
	}

	// GlideAjax response handler
	function parseAjaxResponse(answer) {
		//                            console.log("Parsing request link details: " + answer);
		// Get the response and parse it into an object
		var request_vars = JSON.parse(answer + "");
		// Go through the object and set the form fields to the values
		for(var i in request_vars){
			g_form.setValue(i, request_vars[i]);
		}
	}
}

 

View solution in original post

5 REPLIES 5

mslocum
Mega Expert

The ootb UI Action for creating a request looks like this

function onClick() {
	var result = g_form.submit('sysverb_ws_save');
	if (!result) { // failed form submission
		return;
	}
	result.then(function() {
		var params ={};
		params.sysparm_parent_table = "interaction";
		params.sysparm_parent_sys_id = g_form.getUniqueValue();
		g_service_catalog.openCatalogItem('sc_cat_item', '-1', params);
	});
}

How can this be modified to pass the opened_for variable to our requested_for variable in our variable set we put on all our catalog items?

Wintonsl
Tera Contributor

Did you ever find the answer to your question? I'm needing to do the exact same thing, and I can't find any good information on how to do this. Thanks.

We used a default variable set for requestor information on all of our catalog items so we ended up doing a few things to make this work. Ill post the code below:

Script Include:

Client Callable:True

Accessible from: All application scopes

Name: RequestLinkSessionManager

var RequestLinkSessionManager = Class.create();
RequestLinkSessionManager.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	setComponentSessionValueMapJSON: function(session_data) {
		session_data = session_data || this.getParameter("workspace_request_session_data");

		// Set the session data
		gs.getSession().putClientData('workspace_request_session_data', session_data);

		return session_data;
	},

	getComponentSessionValueMapJSON: function() {
		var session_data = gs.getSession().getClientData("workspace_request_session_data");
		var parsed_session_data = global.JSON.parse(session_data);

		var tableName = parsed_session_data.sysparm_parent_table;
		var sysId = parsed_session_data.sysparm_parent_sys_id;
		var tableRequestedForFieldName = "requested_for";
		var response = {};

		var grMapping = new GlideRecord('request_parent_mapping');
		grMapping.addQuery("parent_table", tableName);
		grMapping.query();
		
		if(grMapping.next()){
			tableRequestedForFieldName = grMapping.getValue("requested_for_field");
		}

		var grCurrent = new GlideRecord(tableName);
		if(grCurrent.get(sysId)){
			response.requested_for = grCurrent.getValue(tableRequestedForFieldName);
		}

		// No longer needed, purge.
		gs.getSession().putClientData("workspace_request_session_data", "");

		var responseJSON = JSON.stringify(response);
		return responseJSON;
	},
	type: 'RequestLinkSessionManager'
});

 

Create Request UI Action Workspace Script:

function onClick() {
                var params = {};
                                params.sysparm_parent_table = "interaction";
                                params.sysparm_parent_sys_id = g_form.getUniqueValue();
 
                var paramsJSON = JSON.stringify(params);
                var request = new GlideAjax('RequestLinkSessionManager');
                request.addParam('sysparm_name', 'setComponentSessionValueMapJSON');
                request.addParam('workspace_request_session_data', paramsJSON);
                request.getXMLAnswer(function(answer){
                                onSubmitAfterSessionPut(params);
                });
}
 
function onSubmitAfterSessionPut(params) {
                var result = g_form.submit('sysverb_ws_save');
                if (!result) { // failed form submission
                                return;
                }
                result.then(function() {
                                // var params = {};
                                // params.sysparm_parent_table = "interaction";
                                // params.sysparm_parent_sys_id = g_form.getUniqueValue();
                                g_service_catalog.openCatalogItem('sc_cat_item', '-1', params);
                });
}

 

Catalog Client Script: This went on our variable sets for our requestor information

UI Type: Mobile/Service Portal

Type: onLoad

Applies on a Catalog Item View: True

function onLoad() {
	
	try{
		// Get the session data
		// var component_link_json = g_user.getClientData('lds_itp_request_link');
		var ga = new GlideAjax("RequestLinkSessionManager");
		ga.addParam("sysparm_name","getComponentSessionValueMapJSON");
		ga.getXMLAnswer(parseAjaxResponse);
	} catch(e){
		//                            console.log("There was an error parsing the request link data: " + e);
	}

	// GlideAjax response handler
	function parseAjaxResponse(answer) {
		//                            console.log("Parsing request link details: " + answer);
		// Get the response and parse it into an object
		var request_vars = JSON.parse(answer + "");
		// Go through the object and set the form fields to the values
		for(var i in request_vars){
			g_form.setValue(i, request_vars[i]);
		}
	}
}

 

Stefan42
Tera Expert

Maybe my reply in this question can also help. This is about a simple way for passing the contact with the Create Request button to the catalog item in Agent Workspace:

https://community.servicenow.com/community?id=community_question&sys_id=a2fd8bbf1b058450d01143f6fe4b...