Business Rule does not fully run (PLEASE)

AngelP83
Giga Guru

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:

current.work_notes = 'Before While statement';
current.work_notes = 'Attachment File Name';
current.work_notes = 'Found Attachment';
 
To check if the BR is being run after the ticket has been created, and IT DOES!!!
I am seeing the Before While statement statement being added to my ticket, but for some weird reason, it stops working right there, it doesnt reach WHILE. so the following dont run:
current.work_notes = 'Attachment File Name';
current.work_notes = 'Found Attachment';

 

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);

 

1 ACCEPTED SOLUTION

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!

View solution in original post

7 REPLIES 7

Hi,

Within the platform there is a system property that is set by default to "use" a number when you open an Incident record, even if you don't actually save it. So, that means that if you have inbound emails creating Incidents, but then later on the platform itself, someone clicks "New" to create an incident, but doesn't actually create it...that number is still "used" and so the counter moves to the next number. Then later in the day, if your inbound creates another incident, it uses the next number in the counter. More information can be found here: https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0784242 

 

So, I don't think it's your inbound action actually skipping numbers, it's most likely that someone clicked "New" to create a new incident on the platform, but didn't save, and so the number is still counted as used.

 

The current.insert() is just inserting/creating the record.

The event.state = "stop_processing"; means that no other inbound action after this one will run (so...stop...processing the inbound action). More info on that can be found here as well: https://www.servicenow.com/community/developer-blog/servicenow-things-to-know-8-stop-processing-of-a... 


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Thank you so much for all your help Allen!

SanjivMeher
Kilo Patron
Kilo Patron

As suggested by @Allen Andreas , you shouldn't use a current.update() in an onBefore BR.

I would use a Flow in your use case. The advantage of using a Flow is, it will run after insertion.

You can also a add Wait for a duration, incase it needs to wait before querying the attachment table and then update the source record.


Please mark this response as correct or helpful if it assisted you with your question.