The CreatorCon Call for Content is officially open! Get started here.

Add an array to Widget Server script to remove duplicates

Dazler
Mega Sage

Hi,

I am needing some help with adding an array to remove duplicate records in the Server script of my custom widget.

I have a field on this table called portal identifier and several records have the same identifier, but I don't want to bring all the records just the unique values.

But I also need to be able to pass additional values from the server script to the HTML on the widget.  I have used ArrayUtil before but not in this setting where I still need to pass additional values as well.  Below is my script that I have.

(function() {
//create an array to populate with notes

data.rbac = [];
var rbacGR = new GlideRecord('u_role_base_access');
rbacGR.addQuery('u_active', true);
rbacGR.query();
while (rbacGR.next()) {

var rbacObj = {};
//use service portal helper method to get some display values
$sp.getRecordDisplayValues(rbacObj, rbacGR, 'u_job_code,u_job_profile_name,u_application,u_portal_identifier,sys_id');

data.rbac.push(rbacObj);
}

})();

Can anyone help me to fix this so I can remove the duplicates before passing the values over to the HTML?

Any help would be appreciated.

 

1 ACCEPTED SOLUTION

Dazler
Mega Sage

I got the answer that I needed.

https://community.servicenow.com/community?id=community_user_profile&user=169ecae9dbd41fc09c9ffb651f961916

View solution in original post

3 REPLIES 3

Sagar Pagar
Tera Patron

Hi,

Try this updated scripts -

(function() {
	// create an array to populate with notes

	// data.rbac = [];
	var rbacObj = [];
	var arrayVal = [];
	var uniqueVal = [];


	var rbacGR = new GlideRecord('u_role_base_access');
	rbacGR.addQuery('u_active', true);
	rbacGR.query();
	while (rbacGR.next()) {

		// use service portal helper method to get some display values
		$sp.getRecordDisplayValues(rbacObj, rbacGR, 'u_job_code,u_job_profile_name,u_application,u_portal_identifier,sys_id');

		arrayVal.push(rbacObj);
	}

	var arrUtil = new global.ArrayUtil();
	uniqueVal = arrUtil.unique(arrayVal);

	gs.info("length: " + uniqueVal.length); /* print lengths */

	/* // print the unique elements
	for(var i = 0; i< uniqueVal.length; i++){
	console.log(uniqueVal[i]);
	}
	*/

	data.rbac = uniqueVal;

})();

 

Thanks,

Sagar Pagar

The world works with ServiceNow

Hi @Sagar Pagar,

I tried your update but that didn't work.

In this part of the code, the field that I want to determine if the record is a duplicate is in the u_portal_identifier field.

// use service portal helper method to get some display values
		$sp.getRecordDisplayValues(rbacObj, rbacGR, 'u_job_code,u_job_profile_name,u_application,u_portal_identifier,sys_id');

I tweaked what you provided a little (see below script) and I got it to take out the duplicates, but I am still lost.  

(function() {
	// create an array to populate with notes

//data.rbac = [];
	var arrayVal = [];
	var uniqueVal = [];

	var rbacGR = new GlideRecord('u_role_base_access');
	rbacGR.addQuery('u_active', true);
	rbacGR.query();
	
	while (rbacGR.next()) {
	
		arrayVal.push(rbacGR.u_portal_identifier.toString());
		
	}

	var arrUtil = new ArrayUtil();
        uniqueVal = arrUtil.unique(arrayVal);

	console.log("length: " + uniqueVal.length); /* print lengths */

/* // print the unique elements
	for(var i = 0; i< uniqueVal.length; i++){
	console.log(uniqueVal[i]);
	}
	*/
	
      data.rbac = uniqueVal;

})();

In my body html, I did the ng-repeat and that is displaying the correct number of records.

But the values, I am not able to get correctly.  It look like the image below.  Also, since I only chose 1 fields that should determine whether there is a duplicate I can't pass any other values to the html.

find_real_file.png

I know I am missing something.  This is a learning practice for me.  Any help you can give would be appreciated.

Dazler
Mega Sage

I got the answer that I needed.

https://community.servicenow.com/community?id=community_user_profile&user=169ecae9dbd41fc09c9ffb651f961916