Using indexOf() to loop through array values in script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2019 12:47 PM
We have been asked to setup an Inbound Action (IEA) for a recently acquired email address. The email will contain keywords in the subject or body that will determine where to send the email…HR Case or Incident. (For readability, I've reduced the number in the array significantly with an ultimate goal of using a system property)
My idea was to use an array with the keywords and use indexOf() to search the email subject/body and process an HR case if any entries in the array are located. 1st draft of the script is below.
Line 11 is not doing what I thought it would as it is not finding the known text in the emailText variable from Line 4.
-If I change the line to if (emailText.indexOf(“Open Enrollment”) > -1) { , I know it's working as evident in the log entries from line 12 or 15.
I don't have experience with arrays and maybe this approach will not work. Could someone take a look at the script below and see where I'm going wrong?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2019 01:08 PM
I have seen some weird issues in cases like this and always use the toString() function when using the indexOf() function. So change line 11 to:
if (emailText.toString().indexOf(keywordProperty[i]) > -1) {

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-21-2019 01:22 AM
Hi Kevin,
In addition to what Michael suggested I also see that 'i' is not defined in line 10. Can you replace i=0; with var i=0;
Thanks,
Jaspal Singh
Hit Helpful or Correct on the impact of response.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-21-2019 03:55 AM
I agree with previous posts, that your JavaScript code contains some errors. Missing declaration of i variable is one problem. Usage of indexOf method of emailText variable means that it should be probably string. Then you can use String(emailText) to make your code safer (String(emailText) is safer as emailText.toString() because it works even with null and undefined).
On the other side, I think that the main problem in your code is the following: the loop by i works only with i=0. After that it execute always return statement to exit from shouldSkipCaseCreation function. You can fix your code by removing else part of the if statement and placing return false; directly after the for-loop.
If we speak about JavaScript, then I would recommend you to use some helpful functions of Array, which supported by ServiceNow in both server and client code: some, every and forEach. forEach can be used if you need to do some actions or tests with every element of array. some and every functions are helpful in case of testing elements of array (like in your case). If you need be sure that some condition if true with all element of array then it's good to use every method and in your case, if you need find at least one element of the array, which correspond the condition, the usage of some method would be good.
Thus, I'd suggest you to modify your code to the following:
/* global Class */
var CFSInboundEmailUtil = Class.create();
CFSInboundEmailUtil.prototype = {
initialize: function () {
//this.keywordProperty = JSON.stringify(gs.getProperty("sn_hr_core.cfs.esc.keywords", "[]"));
//this.keywordProperty = gs.getProperty("sn_hr_core.cfs.esc.keywords", "").split(";");
this.keywordProperty = ["AIT Application", "Open Enrollment", "PlanSource",
"Plan Source", "Sellback", "Hire Date Change", "Employee Deletion"];
},
shouldSkipCaseCreation: function (emailText) {
// returns true if the subject contains text that indicates the email should not creare a case
emailText = String(emailText);
return this.keywordProperty.some(function (keyword) {
return emailText.indexOf(keyword) > -1;
});
},
type: "CFSInboundEmailUtil"
};
If you would fill keywordProperty from property "sn_hr_core.cfs.esc.keywords" (which I don't know) then you should not forget that gs.getProperty("sn_hr_core.cfs.esc.keywords") can return null (if the property doesn't exist) and the returned value will be probably string and not array. Thus you will need to use split method additionally or JSON.parse to convert the value to array. Thus, I'd recommend you to use the second parameter of gs.getProperty as "" or "[]" depend on which method you will use for converting the value to array.