The CreatorCon Call for Content is officially open! Get started here.

check if string begins with XYZ

Not applicable

Hi, I want to check in a business rule if a string begins with three specific characters. I thought I had read somewhere in the wiki how to do this, but can't find it anymore.

Can anyone assist please?

5 REPLIES 5

Mark Stanger
Giga Sage

There are a couple of ways you could do this. A great resource if you're not already aware of it is the w3schools site. Here's a link to their javascript string reference.

http://www.w3schools.com/js/js_obj_string.asp

I would probably use the indexOf() method. It checks the entire string for a certain set of characters and returns the index of the first character in the string if it finds the character set. If it doesn't find it, it returns a -1.

var str="Hello world!";
document.write(str.indexOf("Hello") + "
"); -------returns 0
document.write(str.indexOf("World") + "
"); ------returns -1
document.write(str.indexOf("world")); -------returns 6

So, you could use str.indexOf("XYZ") and if the indexOf value returned was 0, then you would know that it found it at the beginning of the string.


Just one more option.
if (str.substring(0,3)=='XYZ')


Just to be sure:

I would like to run this business rule on all records in the incident table:
So I go to System Definition - Scheduled jobs, create a new one and click execute now (I'm doing this on our dev instance)?

script:
var rec = new GlideRecord('incident');
var str;
rec.query();
while (rec.next()) {
str = rec.u_incident_subtype;
if (str.substring(0,3)=='REQ' || str.substring(0,3)=='TRB')
{
rec.u_categorization = rec.u_incident_subtype;
}
rec.update();
}


Should this work? How can I monitor if it's progress?


Looks good at a quick glance. I always like to test small scale before running a script against an entire table. Use addQuery() to limit the list to a small group just to make sure you get the results you expect.
You can monitor progress by filtering the incident list to show update today or in last hour. Depending on how many incidents you have this could be a lot of i/o and may hang your instance for a bit, which mean you wouldn't be able to check the status. If you have a lot, it might be best to control the query to update them in chunks.