- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-24-2020 09:39 AM
I typed the for loop wrong, it should be reversed, so i < kw.length.
If that does not fix it try doing a
gs.print(found.length);
And see if it gives you a count. If it does not then I would add some gs.print statements to the code to see what values are where and go from there.
Also you should probably find a specific record that you know should return a result and what that result should be. Then query only for that record for testing.
To access the values its just an array with properties for each element. So something like this will print it out.
for(var x = 0; x < found.length; x++){
gs.print(found[x].keyword);
}
I do not know how you built things and if its possible to find multiple records or not in Table 2 so you will need to sort that part out if you need an array or can just update the record you queried for..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-24-2020 10:06 AM
It's not possible to find multiple records with the same keyword in table 2, it'll only ever find one category for the keyword.
I did update "category: t2.getValue("sys_id")" since it is a reference value being passed back for the category.
Once it finds the keyword from table 2 I need to grab that and update that in table 1, and same with the category. So something like the below. I just don't know where to put it.
report.u_keyword = keyword found from table 2;
report.u_category = category (sys_id) from that keyword found from table 2;
report.update();
I also narrowed my query to be 5 records that should update once the keyword is found and when I print the found.length it does return 1s and 2s so it's working, I just need to update the table 1 record (report object).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-24-2020 10:26 AM
You need to put your update code in place of the array being built. But if you are getting length of 2 or more then you are finding multiple keyword matches and need to decide what you are going to do about it. If its the same category its just a matter of adding multiple keywords to table 1 but if not they you may have an issue.
Something like the below with the found var changed to have a default value of false instead of an array.
found = false;
if (kw.length > 0) {
for (var i = 0; kw.length < i; i++) {
if (question.indexOf(kw[i]) > -1) {
found = true;
}
}
if(found){
//Do your record update here
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-24-2020 11:59 AM
Hello leahdany,
Please use below before business rule,
I have checked on personal instance and working fine.
(function executeRule(current, previous /*null when async*/) {
var question=current.u_question;
var arr=question.split(" ");
var gr=new GlideRecord('u_table_2');
for(var i=0;i<arr.length;i++)
{
gr.addEncodedQuery('u_keywordLIKE'+arr[i]);
gr.query();
if(gr.next())
{
current.u_keyword=arr[i];
current.u_category=gr.u_category;
break;
}
}
})(current, previous);
I have attached screenshot too.
Please mark it correct and helpful if it works.
regards,
Arjun Shinde
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-24-2020 08:28 PM
Thanks for all the replies. Here's what ended up working for me.
var report = new GlideRecord('u_conversation_message_report');
report.addEncodedQuery("u_categoryISEMPTY^u_questionISNOTEMPTY");
report.query();
while (report.next()) {
var question = report.u_question.toString().toLowerCase();
var keywords = [];
var i = 0;
var category = '';
var keywordsNoComma = '';
var catTable = new GlideRecord("u_conversation_message_category");
catTable.query();
while (catTable.next()) {
i = 0;
keywords = catTable.u_keyword.toString().toLowerCase().split(',');
while (i < keywords.length) {
if (question.indexOf(keywords[i]) > -1) {
if (keywordsNoComma != '') { // This is removing the leading comma from the keywords
keywordsNoComma += ',';
}
keywordsNoComma += keywords[i];
category = catTable.sys_id;
report.u_keyword = keywordsNoComma;
report.u_category = category;
report.update();
}
i++;
}
}
}