- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2024 02:06 PM - edited 07-26-2024 02:28 PM
I have a script that will check for an incoming email, it will create a new ticket, and it will reassign it to the right group, and contact_method.
That script works.
Now, I created a business rule that will check if the new ticket contains a WAV attachment,
If it does, it will change the Contact Method to VOICEMAIL. (by itself, it shouldnt need me to go to the ticket and update it)
(The funny part, this script works if I click UPDATE or SAVE button within the ticket), but it doesnt work when the ticket is created for the first time via email.
To troubleshoot it, I added the following:
Again, if i go to the ticket after it has been created manually, and update or saved something, the BR works as expected.
Thank you!
BUSINESS RULE
WHEN: BEFORE
Order: 4000
Insert and Update
When to Run: If Short Description starts with "Message from"
(function executeRule(current, previous /*, g_scratchpad */) {
// Check if the current record has attachments
var attachmentGR = new GlideRecord('sys_attachment');
attachmentGR.addQuery('table_name', current.getTableName());
attachmentGR.addQuery('table_sys_id', current.sys_id);
attachmentGR.query();
var hasWavAttachment = false;
current.work_notes = 'Before While statement';
while (attachmentGR.next()) {
var fileName = attachmentGR.file_name.getDisplayValue().toLowerCase();
current.work_notes = 'Attachment File Name';
// Check if the file ends with '.wav'
if (fileName.endsWith('.wav')) {
hasWavAttachment = true;
current.work_notes = 'Found Attachment';
break; // Exit the loop if a WAV file is found
}
}
// If there is at least one WAV attachment, update the contact_method field to 'voicemail'
if (hasWavAttachment) {
current.contact_method = 'Voicemail';
current.work_notes = 'Voicemail Test';
current.update(); // Save the record after updating the field
}
})(current, previous);
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2024 06:07 PM - edited 07-26-2024 06:12 PM
Hi,
You could add the script in the inbound email action itself, it's server side there, just the table_name would be slightly different: sys_email, instead. Here's a link to a post that gives an example of someone needing to query the name of the attachment, so you'll get the idea with this: https://www.servicenow.com/community/developer-forum/get-email-attachment-name-in-inbound-action/m-p...
From there, you should have access to current if this is on the incident table and the record has been identified by the watermark, etc.
Within the action, you would set the fields as needed and here, you would use current.update() (or I supposed current.insert() if this is new, you should see that already in your action, you would just evaluate the attachment there and already set the contact_method right in the action itself.
example here as well: https://docs.servicenow.com/csh?topicname=r_InboundEmailActionExamples.html&version=latest
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2024 04:30 PM
Hello,
You shouldn't be using current.update() in your BR script.
If this is a before BR, then you have access to current and can set it accordingly without saving anything, then when the record processes on the server, whatever value you set for the current.field will stay.
whatever you are setting as the work note will only show once and if you're saying that it's only showing your first statement, then that means it's not entering the while loop...if it's not entering the while loop then that means it's not finding any attachments...if it's not finding any attachments, then a before BR may be too early in the process. You'd have to track your attachment and see if the table is being adjusted after the record is saved (so prior to, it's not fully attached to the record yet).
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2024 05:18 PM
I removed the UPDATE() and the issue persist.
I used an IF statement to check if the attachments are being located and it also failed.
I was thinking to use gs.sleep to let the initial script run, and wait for the BR to run after, but i read that approach is not recommended.
You are correct, it is not entering the WHILE loop. I used BEFORE and AFTER, but issue persist.
Any recommendations?
do you think the inbound email action script is adding the attachment too slow that the BR script doesnt find it?
I honestly dont know what to do at this point.
I was thinking to add the script at inbound email action script, but I dont know how to do that yet.
Thank you
Thank you!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2024 06:07 PM - edited 07-26-2024 06:12 PM
Hi,
You could add the script in the inbound email action itself, it's server side there, just the table_name would be slightly different: sys_email, instead. Here's a link to a post that gives an example of someone needing to query the name of the attachment, so you'll get the idea with this: https://www.servicenow.com/community/developer-forum/get-email-attachment-name-in-inbound-action/m-p...
From there, you should have access to current if this is on the incident table and the record has been identified by the watermark, etc.
Within the action, you would set the fields as needed and here, you would use current.update() (or I supposed current.insert() if this is new, you should see that already in your action, you would just evaluate the attachment there and already set the contact_method right in the action itself.
example here as well: https://docs.servicenow.com/csh?topicname=r_InboundEmailActionExamples.html&version=latest
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2024 10:23 PM
Thank you so much!!!
Using the URL you provided. I made the following changes and it worked!!!
var emailSysId = sys_email.sys_id;
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_name', 'sys_email');
gr.addQuery('table_sys_id', emailSysId);
gr.query();
var isWavFile = false;
while (gr.next()) {
var attachmentName = gr.file_name;
if (attachmentName.toLowerCase().endsWith('.wav')) {
isWavFile = true;
break; // Stop checking further attachments
}
}
// If a .wav file is found, stop further processing
if (isWavFile) {
current.contact_method = 'Voicemail';
} else {
current.contact_method = 'Email';
}
One last question!,
I have noticed that my Inbound Email Script is creating tickets, but sometimes they skip the numbers.
Like:
INC00001
INC00002
INC00004
INC00006
INC00008
I am not sure why that is.
I noticed that at the end of the script, there is the
current.insert();
event.state = "stop_processing"; (could this be causing the issue?)
Thanks!!!!!