- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-18-2024 09:52 PM - edited 11-18-2024 09:54 PM
Hello, everyone.
I tested the script below in the background script, and it works well. However, it is not in inbound action, and the problem appears to be with the indexOf. Do you have any clue why?
var str = email.body_html;
var subject = email.subject.toString(); // return "Membership updated - [[FYI]New member added]"
var mailType = 'Type:';
var contains = str.indexOf(mailType);
var flag = false;
if (contains > -1) { //Tag found
var index = str.lastIndexOf(mailType); //Extract string begin from
var index2 = str.lastIndexOf("Comment:"); //Extract string end on
var theTag = str.substring(index, index2);
//First check if tag = ACCESS
if (theTag == ('ACCESS')) {
gs.info('Scenario 1: ' + flag);
createINC();
} else {
//Second check if incident can be created as closed
var excludedtags = '[FYI],[JUNK],[SUSPICIOUS],[BULK]';
var outputStr = excludedtags.split(',');
var tagArr = [];
for (var i = 0; i < outputStr.length; i++) {
tagArr.push(outputStr[i]);
}
tagArr.join();
for (var i2 = 0; i2 < tagArr.length; i2++) {
if (subject.indexOf(tagArr[i2]) > -1) { // contains one of the tag, create a closed incident as record
flag = true;
gs.info('Scenario 2: ' + flag);
break;
} else { //None of the condition above, create the incident
gs.info('Scenario 3: ' + flag);
createINC();
break;
}
}
}
}
function createINC() {
current.caller_id = gs.getUserID();
current.comments = "received from: " + email.origemail + "\n\n" + email.body_text;
current.short_description = email.subject;
current.description = email.body_html;
current.assignment_group = "xxxxxxxxxxxxxxxxxxx";
current.incident_state = IncidentState.NEW;
current.contact_type = "email";
current.impact = 2;
current.urgency = 2;
current.insert();
}
Instead of scenario 2, the outcome proceeds to scenario 3
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-18-2024 11:39 PM - edited 11-18-2024 11:40 PM
Hi again,
I think you want to move the "else" part outside of the loop.
Like this - this is just a raw "test" from a background script which checks for all of your "junk" inputs.
In case non of the indexes evaluate true then it will continue out of the loop and this will be scenario 3.
In the below i just changed the subject to something thats not a part of the "junk" list.
var subject = "Membership updated - [[ACCESS]New member added]"; // return "Membership updated - [[FYI]New member added]"
//Second check if incident can be created as closed
var excludedtags = '[FYI],[JUNK],[SUSPICIOUS],[BULK]';
var outputStr = excludedtags.split(',');
var tagArr = [];
for (var i = 0; i < outputStr.length; i++) {
tagArr.push(outputStr[i]);
}
tagArr.join();
var flag = false;
for (var i2 = 0; i2 < tagArr.length; i2++) {
gs.info('Evaluate ' + tagArr[i2]);
if (subject.indexOf(tagArr[i2]) > -1) { // contains one of the tag, create a closed incident as record
flag = true;
gs.info('Scenario 2: ' + flag);
break;
}
}
if(flag === false){
gs.info('Scenario 3: ' + flag);
createINC();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-18-2024 10:50 PM
Hi tsoct,
It's strange for me as well. Follow the below points to overcome this issue :
1. Make sure str variable is string type, Please apply explicit type conversion.
2. Use includes function instead of indexOf for condition checks if possible.
As you mentioned same script has been working in background script, Could you please tell me what value you were used in place of email object in background script.
Thanks, Please click thumps up if these points help you and also provide me the script which you have tried through background script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-18-2024 11:20 PM
Hi,
Theres something wrong with your setup.
First -
outputStr
and
tagArr
are identical which make tagArr redundant.
2nd - Your for loop ALWAYS breaks after the first index
var excludedtags = '[FYI],[JUNK],[SUSPICIOUS],[BULK]';
for (var i2 = 0; i2 < tagArr.length; i2++)
This means that it only checks the first index [FYI] - then it stops duo to breaks.
Try printing out some more outputs
for (var i2 = 0; i2 < tagArr.length; i2++) {
gs.info('subject ' + subject);
gs.info(tagArr[i2]);
gs.info(subject.indexOf(tagArr[i2]);
There shouldnt be any difference in script evaluation across the platform - its Javascript afterall.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-18-2024 11:33 PM
Thanks for the analysis @Simon Christens .
I wanted the inbound action to create only one occurrence. Without 'break', an incident will be generated for each loop. Could you advise me what I can do to ensure that just one incident is created after the loop?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-18-2024 11:39 PM - edited 11-18-2024 11:40 PM
Hi again,
I think you want to move the "else" part outside of the loop.
Like this - this is just a raw "test" from a background script which checks for all of your "junk" inputs.
In case non of the indexes evaluate true then it will continue out of the loop and this will be scenario 3.
In the below i just changed the subject to something thats not a part of the "junk" list.
var subject = "Membership updated - [[ACCESS]New member added]"; // return "Membership updated - [[FYI]New member added]"
//Second check if incident can be created as closed
var excludedtags = '[FYI],[JUNK],[SUSPICIOUS],[BULK]';
var outputStr = excludedtags.split(',');
var tagArr = [];
for (var i = 0; i < outputStr.length; i++) {
tagArr.push(outputStr[i]);
}
tagArr.join();
var flag = false;
for (var i2 = 0; i2 < tagArr.length; i2++) {
gs.info('Evaluate ' + tagArr[i2]);
if (subject.indexOf(tagArr[i2]) > -1) { // contains one of the tag, create a closed incident as record
flag = true;
gs.info('Scenario 2: ' + flag);
break;
}
}
if(flag === false){
gs.info('Scenario 3: ' + flag);
createINC();
}