Fix script help - do not want to run workflows and business rules

MS12
Kilo Sage

Hi,

 

I'm processing a bunch of old emails and I want to add the body as a comment to case record. I do not want to trigger BRs or update some fields since some of these cases might be closed. 

I have these two calls before the update call.

rec.setWorkflow(false);
rec.autoSysFields(false);

 

It runs the script correctly but the comment added is not shown on the case record. If I check the sys_journal table, the comment is being added each time I run the script. 

 

If I comment out the first one rec.setWorkflow(false);, then the comment shows up as additional comment on the case record.

 

I compared the two journal records created and they are identical. They point to the same case and is of type 'Additional Comment'. Why does one show up and other doesn't?

Is there a BR that adds this comment to the case even when all the references link them correctly?

Any ideas would be helpful.

Thank you!

3 REPLIES 3

Sohithanjan G
Kilo Sage
Kilo Sage

hey @MS12 ,

When you add a comment or journal entry to a record, especially through scripting, there are a few considerations to keep in mind. The behavior you are describing might be related to the order of operations in the system. Let me explain why the comment is not showing up as expected:

1. `rec.setWorkflow(false)`:
This line of code disables business rule (BR) processing for the record you are updating. This means that any business rules associated with the case record, including those related to comments and journal entries, won't be triggered. As a result, the comment is not added to the case as you've noticed.

2. `rec.autoSysFields(false)`:
This line of code disables the automatic setting of system fields, but it shouldn't affect the behavior of comments and journal entries directly.

When you comment out `rec.setWorkflow(false);`, you're enabling business rule processing, and this allows the system to properly process the comment and add it to the case.

Regarding the identical journal records that you mentioned, they may be created in the `sys_journal` table but might have different internal states or flags related to whether they are visible on the case form or not.

Here's what you can do to add comments without triggering business rules or BRs but still have them displayed on the case form:

1. Use the `rec.setWorkflow(false);` to avoid triggering business rules.

2. After you've made your updates, you can explicitly add the comment to the case record using JavaScript. For example:

var journal = new GlideRecord("sys_journal_field");
journal.initialize();
journal.entry = "Additional Comment";
journal.operation = "INSERT";
journal.field_name = "comments";
journal.value = "Your comment text here";
journal.sys_id = rec.sys_id; // Link the journal entry to the case record
journal.insert();

This way, you have more control over adding the comment without triggering business rules while ensuring it shows up on the case record. The `sys_journal` table keeps track of comments, but explicitly adding them like this ensures that they are displayed as expected on the form.

Please accept the answer as Accepted Solution and hit the helpful button if it suffices your requirement.


BR, Sohith

Please mark as Accepted Solution if this solves your query and HIT Helpful if you find my answer helped you. This will help other community mates too..:)

Thank you for your reply. Unfortunately the amount of emails I had to process was huge, I decided to disable the BRs that I wanted and ran the script. Enabled the BRs afterwards. I'm glad I'm done  with that one.

OlaN
Giga Sage
Giga Sage

Hi,

Adding to comments and worknotes are two special cases where you will have to run business rules, if you want the comments to actually show up on the records.

I encountered a similar scenario a while back, and the way I did as a workaround was the following:

Stop the email sending on the instance.

Then run the fix script (with processing of the business rules enabled).

Then go to the email table, and set all the emails that were generated by the comments as processed (or I might have deleted them, can't remember really).

Finally activated the email sending on the instance again.