Search string for lookup table values

Community Alums
Not applicable

Hi,

I am trying to create a Business Rule to search through the short description of an Incident raised via a portal against keywords in a lookup table.

I have explored using Assignment Rules, but I need the ability to set multiple fields as opposed to just the Assignment Group/Assigned To fields, which I do not believe you can do with these?

I have also looked at using Data Lookup Definitions, which solved the above problem, but as far as I can tell the match field needs to match the lookup field exactly which doesn't allow me the functionality to search a whole paragraph for multiple keywords.

(more than happy to be corrected on either of the above statements and pointed in the right direction!)

I have managed to create a rule that works, however, its not the most efficient or elegant, and am hoping to be able to refine it with some guidance.

My current hack is below:

(function executeRule(current, previous /*null when async*/) {
	
	var lookup = new GlideRecord('u_assignment_data_lookup');
	lookup.orderByDesc('order');
	lookup.query();
	while(lookup.next())
		{
			var keyword = lookup.u_keyword.toLowerCase();
			if(current.short_description.toLowerCase().indexOf(keyword)>-1)
				{
					current.assignment_group = lookup.u_assignment_group;
					current.category = lookup.u_category;
					current.subcategory = lookup.u_sub_category;
					current.impact = lookup.u_impact;
					current.urgency = lookup.u_urgency;
				}
		}
	
})(current, previous);

Essentially, this does a query against the lookup table and returns ALL records (currently only a handful in dev, but could be into the 100's in production).

Then it loops through the records from the lookup table and checks whether the keyword is contained in the short description.

If is is, it will set the relevant values.

If there are multiple results matched against keywords, the the values are overwritten with each loop.

As the original query is ordered by the 'Order' field (which I am utilizing as a priority field) in descending order, the record with the highest priority will be matched last and thus the values from this record on the lookup tables will be written to the record.

I'm sure there must be a slicker way to do this without having to loop through all of the lookup values, but I have not been able to come up with it yet!

What I really want to do is use the 'current.short_description' in an addQuery as the first variable and the 'lookup.u_keyword' as the last variable, something like this:

var lookup = new GlideRecord('u_assignment_data_lookup');

lookup.addQuery(current.short_description, 'CONTAINS', lookup.u_keyword)

But this does not seem to work as I guess the first variable has to be from the table the query is against, i.e. 'u_assignment_data_lookup'?

Hoping someone will be able to guide me towards a more efficient way of achieving my aim.

Thanks in advance!

Jason

 

1 REPLY 1

sebastian_g_snc
ServiceNow Employee
ServiceNow Employee

Hi Jason,

I agree with your statements above: unfortunately, there is no "order" field on the data lookup definition (as in the assignment rule) - which could help you with your priority.

Since you are saying there could be 100s of lookup keywords, I think you should pay attention to performance. Hence my idea would be to start looking for the highest priority keywords first so you avoid updating the record many times - once is enough (orderBy should do, if the lowest order number is your highest priority).

If the highest priority keyword was found, do your thing and then break the loop. That should save performance.

Alternatively, I am also thinking about putting the keywords into an array. If the array becomes really big, you could create multiple arrays of maybe 50 keywords. You would have to loop through the arrays nevertheless.

Best

Seb