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.

Help with Custom UI Actions for Associating Related Records in a Modal (GlideModal + GlideAjax)

mattmurdock
Tera Contributor

Hi everyone,

 

I'm working on a customization in ServiceNow (Xanadu version) and could use some advice on where I'm stuck.

 

Scenario:

  • I have a custom table: u_sap_transport_request with reference field u_change_task pointing to  the change_task table.
  • On the change_task form, I've added a related list for u_sap_transport_request.
  • The goal is to make it easier for users to associate multiple records from the custom table with the parent form's change_task (compared to using the OOB slushbucket that comes with the default related list "Edit" UI Action)

What I've done so far:

  • Created a client-side UI Action on the custom table that opens a modal using GlideModal and loads the standard list view of the custom table (with a filter for where the u_change_task reference field is empty)
  • Create a client-side UI Action (List Choice type) on the custom table that is intended to pass the selected records on the modal list along with the parent change_task's sys_id to the Ajax Script Include defined in the next point
  • Built an Ajax Script Include to set the value of each record from the custom table's u_change_task field to the parent change_task sys_id which was provided by second UI Action.

Where I'm stuck: Retrieving the parent change_task sys_id in the second UI Action.

  • I have been able to use the GlideModal's setPreference method to provide the g_form's sys_id to the modal.
  • However, I have not be able to find a way to access this value from my other UI Action in the modal.

What I've tried (to retrieve the parameter's value in the modal UI Action):

  • g_list's getParm method
  • RenderProperties's getParameterValue method

Questions:

  • How can I retrieve the change_task sys_id from my second UI Action in the modal list's drowdown menu?
  • Bigger picture: Am I missing a much easier way to develop a more user-friendly alternative to the OOB slushbucket?

 

I have attached screenshots and code snippets of my current UI Actions and Script Include for reference. I am quite new to ServiceNow so I am open to any feedback or recommendations.

 

Thanks!

 

UI Action - Modal Opener:

function openTransport() {
	var modal = new GlideModal('show_list', false);
	modal.setTitle('Select Transport Requests');
	modal.setPreference('table', 'u_sap_transport_request_list');
	modal.setPreference('sysparm_view', 'Transport_Selector_Modal');
	modal.setPreference('sysparm_query', 'u_change_taskISEMPTY');
	modal.setPreference('sysparm_parent_sys_id', g_form.getUniqueValue());
	alert("Modal Preference: " + modal.getPreference("sysparam_parent_sys_id"));
	
	modal.setPreference('sysparm_list_mode', 'true');
	modal.setPreference('focus_trap', true);

	modal.render();
}

 

UI Action - Add Selected Records from Modal:

function addSelectedTransports() {
	
	var selectedTransports = g_list.getChecked();

	var parentId = g_list.getParm('sysparm_parent_sys_id');

	alert('Test Alert');

	var ga = new GlideAjax('TransportAssociationAjax');
	ga.addParam('sysparm_name', 'associateTransports');
	ga.addParam('parent_sys_id', parentId);
	ga.addParam('sysparm_transport_ids', selectedTransports);

	ga.getXMLAnswer(function(answer) {
		window.top.location.reload();
	});
}

 

Ajax Script Include:

var TransportAssociationAjax = Class.create();
TransportAssociationAjax.prototype = Object.extendsObject(AbstractAjaxProcessr, {
    initialize: function() {
    },

	associateTransports: function() {
		var changeTaskSysId = this.getParameter('sysparm_parent_sys_id');
		var selectedTransports = this.getParameter('sysparm_transport_ids').split(',');

		var gr = new GlideRecord('u_sap_transport_request');

		selectedTransports.forEach(transport => {
			gr.get(transport);
			gr.setValue('u_change_task', changeTaskSysId);
			gr.update();
		})

	},

    type: 'TransportAssociationAjax'
});

 

0 REPLIES 0