Search a string for keywords and return a value

leahdany
Giga Guru

I'm trying to search a string for keywords found on another table. If found I want to know what those keywords were and update the original record that had the search string with those keyword values in addition to the category.

In this example, I want to search the string in the "Question" field on table 1 for keywords found on table 2. If found on table 2 I want to populate table 1s "Keyword" and "Category" fields with the applicable keyword(s) and category.

"Category" on table 1 is a reference directly to table 2, so the sys id of table 2.

The "Keyword" field on both tables are strings.

find_real_file.png

1 ACCEPTED SOLUTION

leahdany
Giga Guru

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++;
        }

    }

}

View solution in original post

11 REPLIES 11

Hemanth M1
Giga Sage
Giga Sage

Hi,

 

You can write a before update BR on table table 1 and populate the values from table 2.

 

var serach = 0;
var gr = new GlideRecord("table2");
gr.query();
while(gr.next()){
if(gr.keyword.indexOf(current.key) > -1){ //use proper backend value of keyword field
current.category = gr.category //updae the category field
search ++;
}
}

if(!search){
gs.addInfoMessage("No key found")
current.setAbortAction(true);

}

 

 

Mark it as correct or helpful if it helps.

/Hemanth

Accept and hit Helpful if it helps.

Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025

Thanks for your reply. I'm looking to do a scheduled job. I see you are setting the category where you found the keyword, but I don't see where you are setting the keyword that was found. I need both values updated on table 1 - the keyword and category found from table 2.

Hi,

The above logic is, if you put a key word in table 1 of  the keyword filed and save the record it will search all the keyword fields in table 2 ,

if string found ,it will update the category field along with your input keyword else its will abort the submission with the error message.

Accept and hit Helpful if it helps.

Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025

Table 1 will never have a keyword manually input. The keyword and category fields are populated based on finding a matching keyword on table 2 from the question on table 1.