The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Avoid duplicate entries in Portal Widget

DB1
Tera Contributor

Hello All,

I have created a custom widget to show up a list but I would need help with avoiding duplicate entries of same string retrieved via the script.

HTML

<div class="modal" tabindex="-1" role="dialog" id="myModal">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h3 class="modal-title">Client CIs</h3>
<p>Select a Client</p>
</div>
<div class="modal-body">

<ul class="list-group result-container">
<li ng-repeat="inc in data.incident">
<a href  ng-click="c.openPopUp(inc.sys_id)">{{inc.ci}}</a>       
</li>
</ul>
</div>
</div>
</div>
</div>

 

Server

(function() {
	/* populate the 'data' object */
	/* e.g., data.table = $sp.getValue('table'); */
data.incident=[];
data.user = gs.getUserID();
	var gruser_list1 = new GlideRecord('u_authorized_approver');
	gruser_list1.addQuery('u_user',data.user);
	gruser_list1.addQuery('u_authorized_approver','true');
	gruser_list1.query();
	var results = [];
	while(gruser_list1.next())
	{
		var inc = {};		
		inc.ci = gruser_list1.getDisplayValue('u_ci.u_application_name');
		//inc
		data.incident.push(inc);
	}

//data.example = $sp.getPage('demo_widget_for_manage_acc', {});
})();

 

The output is 

 

DB1_0-1665996521288.png

However I would like to avoid duplicate entries or two values with same name into 1 instead

@Chuck Tomasi @Ankur Bawiskar @Community Alums @Abhinay Erra 

 

TIA,

Bhavani Bharathi

1 ACCEPTED SOLUTION

ChrisBurks
Mega Sage

Hi @DB1 

You could dedupe your array using with built-in javascript array methods like reduce or filter. Or maybe add more information into the object being collected for the incidents.

If you wanted to dedupe what you currently have you could leverage the results variable that doesn't seem to be used.

For example:

(function() {
	/* populate the 'data' object */
	/* e.g., data.table = $sp.getValue('table'); */
data.user = gs.getUserID();
	var gruser_list1 = new GlideRecord('u_authorized_approver');
	gruser_list1.addQuery('u_user',data.user);
	gruser_list1.addQuery('u_authorized_approver','true');
	gruser_list1.query();
	var results = [];
	while(gruser_list1.next())
	{
		var inc = {};		
		inc.ci = gruser_list1.getDisplayValue('u_ci.u_application_name');
		//inc
		results.push(inc);
	}
data.incidents = results.reduce(function(acc,curr,idx){  if(acc.used.indexOf(curr.ci) == -1){ acc.used.push(curr.ci); acc.incs.push(curr) } return acc},{"used":[],"incs":[]}).incs 

//data.example = $sp.getPage('demo_widget_for_manage_acc', {});
})();

 

That should dedupe your array. And it won't mutate the original results array in case you still need to do something else with it.

View solution in original post

4 REPLIES 4

ChrisBurks
Mega Sage

Hi @DB1 

You could dedupe your array using with built-in javascript array methods like reduce or filter. Or maybe add more information into the object being collected for the incidents.

If you wanted to dedupe what you currently have you could leverage the results variable that doesn't seem to be used.

For example:

(function() {
	/* populate the 'data' object */
	/* e.g., data.table = $sp.getValue('table'); */
data.user = gs.getUserID();
	var gruser_list1 = new GlideRecord('u_authorized_approver');
	gruser_list1.addQuery('u_user',data.user);
	gruser_list1.addQuery('u_authorized_approver','true');
	gruser_list1.query();
	var results = [];
	while(gruser_list1.next())
	{
		var inc = {};		
		inc.ci = gruser_list1.getDisplayValue('u_ci.u_application_name');
		//inc
		results.push(inc);
	}
data.incidents = results.reduce(function(acc,curr,idx){  if(acc.used.indexOf(curr.ci) == -1){ acc.used.push(curr.ci); acc.incs.push(curr) } return acc},{"used":[],"incs":[]}).incs 

//data.example = $sp.getPage('demo_widget_for_manage_acc', {});
})();

 

That should dedupe your array. And it won't mutate the original results array in case you still need to do something else with it.

DB1
Tera Contributor

Hi Chris,

 

thanks for the quick reply

but how to replace the html in that case 

<li ng-repeat="inc in data.incident">

 

ChrisBurks
Mega Sage

You shouldn't have to replace the html. I made a type in my script and used data.incidents instead of data.incident.

So in the script I did, just change to data.incident and you shouldn't have to change the html. All the script is doing is removing duplicates.

DB1
Tera Contributor

It worked! Thank you