- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-18-2020 08:41 AM
Hi,
I have a script which is on inbound action that takes the subject and search for a keyword and get the knowledge article number and push into an array. Which is working fine
But if in case i have duplicate keywords then it is pushing the kb number multiple times. how to push only unique number
var arr = [];
var substring = email.subject;
substring = substring.split(" ");
for(var i=0; i<substring.length; i++){
if(substring[i] != " "){
var kn = new GlideRecord('kb_knowledge');
kn.addEncodedQuery('metaLIKE'+substring[i]);
kn.query();
if(kn.next()){
gs.log('Found kb');
checked = 'true';
var url = '/sp?id=kb_article&sys_id=' + kn.sys_id;
var com = '[code]<a href="' + url + '" target="_blank" >' + gs.getMessage(kn.number) + '</a>[/code]';
com += "\n";
arr.push(com);
}
}
}
If i send the subject as "I have issue with my computer" then i get one article like KB0000002
If i send the subject as "My Computer is not working, i already troubleshooted my computer" then i get result in my array as KB0000002, KB0000002
Since i found the Computer keyword twice i get kb number twice but i want to display only once
Can anyone please help me how to do this
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-18-2020 10:17 AM
Hm... you want to avoid the same KB twice, right? So you need the unique check on the "arr" array to see if a given KB is already in it. I think I'd do it like this:
if (arr.indexOf(kn.number) == -1)
arr.push('[code]<a href="' + url + '" target="_blank" >' + gs.getMessage(kn.number) + '</a>[/code]');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-18-2020 08:48 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-18-2020 09:06 AM
Hey shaik,
refer below link might help you,
kindly mark Correct and Helpful if applicable.
Regards,
Indrajit.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-18-2020 09:30 AM
Hi Irfan,
Can you try below.
var arr = [];
var substring = email.subject.toLowerCase(); //converts to lower case
substring = substring.split(" ");
var converttoarray=[];
for(var j=0;j<substring.length;j++) //gts substring.length
{
converttoarray.push(substring[j]);//passes as comma separated values.
}
var getarr = new ArrayUtil();
var getuniqueis = getarr.unique(converttoarray);
//gets unique from text My Computer is not working, i already troubleshooted my computer which becomes
//is,not,working,,i,already,troubleshooted,my,computer which took my computer once
for(var i=0; i<getuniqueis.length; i++){
if(getuniqueis[i] != " "){ //changed after
var kn = new GlideRecord('kb_knowledge');
kn.addEncodedQuery('metaLIKE'+getuniqueis[i]);
kn.query();
if(kn.next()){
gs.log('Found kb');
checked = 'true';
var url = '/sp?id=kb_article&sys_id=' + kn.sys_id;
var com = '[code]<a href="' + url + '" target="_blank" >' + gs.getMessage(kn.number) + '</a>[/code]';
com += "\n";
arr.push(com);
}
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-18-2020 09:51 AM
Just a minor update to the below condition is required. I think instead of "substring[i]" we should use a unique array "getuniqueis[i]".
if(substring[i] != " "){ --> if(getuniqueis[i] != " "){
Muhammad