Copy a list field to a list collector via catalog script

Chad R
Tera Expert

Hey all I have a good one today, so we created a field on our business service table tgms and BU - CIOs both are list fields

 

tgmfield.png

 

We have a catalog item where both TGM and BU CIO fields are listed as list collector variables that we want to auto populate based on when someone chooses a business service on the form as you can see below:

catalog.png

 

I have written a script include and catalog script to accomplish this but I am getting no joy and a unhandled glide Ajax error in browser console to boot. Any help here would be appreciated, below are my script includes and client scripts.

 

Client Script:

 

/* 
Auto-populating TGM users from business Service
*/
function onChange(control, oldValue, newValue, isLoading) {
	
	  // give here the list collector variable name
	g_form.clearValue('u_tgm_approver');

	var parm = '';
	if (window === null)
	   parm = 'portal';
	else
	   parm = 'native';

	var ajax = new GlideAjax('ConduentAJAX');
	ajax.addParam('sysparm_name', 'getApprovers');
	ajax.addParam('sysparm_bs', newValue);
	ajax.addParam('sysparm_view', parm);
	ajax.getXML(populateValues);
}

function populateValues(response) {
	var answer = response.responseXML.documentElement.getAttribute("answer");
	var arr = answer.split('||');

        // give here the list collector variable name
	if (window === null) {
		g_form.setValue('u_tgm_approver', arr[0]); // Mobile/Portal Compatible

	} else {
		addItemstoList('u_tgm_approver', answer); // Native Compatible
	}
}

function addItemstoList(listCollector, userList) {

	var parser = JSON.parse(userList);

	var arrSysId = [];
	var arrName = [];
	for(var i=0;i<parser.length;i++){
		arrName.push(parser[i].name.toString());
		arrSysId.push(parser[i].sys_id.toString());
	}

	var varName = listCollector;
	var leftBucket = gel(varName + '_select_0');
	var rightBucket = gel(varName + '_select_1');
	var rightOptions = rightBucket.options;
	var rightIDs = [];

	//Remove --None--
	for (var k= 0; k < rightOptions.length; k++) {
		var value = rightOptions[k].innerHTML;
		if (value == '--None--') {
			rightBucket.remove(0);
		}
	}

	// Add new options
	if (arrName.length > 0) {
		var myCIArray = arrName.toString().split(',');
		for (var j = 0; j < myCIArray.length; j++) {
			addOption(rightBucket, arrSysId[j], myCIArray[j]);
			sortSelect(rightBucket);
			leftBucket.onchange();
		}
	}

	// sort the buckets
	sortSelect(rightBucket);
}

 

Script Include:

var ConduentAJAX = Class.create();
ConduentAJAX.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	getApprovers : function() {
		
		var tmglist = [];
		var bsparm = this.getParameter('sysparm_bs');
		var parm = this.getParameter('sysparm_view');

		var jsonArr = [];

		var tgmusr = new GlideRecord('sys_user');
		tgmusr.addQuery('sys_id', bsparm.u_tgms);
		tgmusr.query();
		while (tgmusr.next()) {

			if(parm == 'portal'){
				tmglist.push(tgmusr.name.toString());
			}
			else if(parm == 'native'){
				var obj = {};
				obj.name = tgmusr.name.toString();
				obj.sys_id = tgmusr.name.toString();
				jsonArr.push(obj); 
			}

		}

		if(parm == 'portal')
			return tgmlist.toString();
		else
			return JSON.stringify(jsonArr);
			
		
	},
	
	
    type: 'ConduentAJAX'
});

 Thanks again stumped on this one, I feel like I'm over complicating it. 

1 ACCEPTED SOLUTION

Jim Coyne
Kilo Patron

Before I dig deeper into a solution, will your users be able to edit the list of approvers?  If not, then I'd question why even show them.  Let your Flow/Workflow get the approvers from the CI behind the scenes and not even show them to the user.  Not much point showing someone something they can't change.

View solution in original post

5 REPLIES 5

You are welcome.  Feel free to bounce things here all you want, that's why we are here.  And sometimes those questions are not technical "how do I", but rather "I wonder if I should".