Modifying a journal field before insert or update

dmartinc
Tera Expert

Hello,

I am trying to modify the comments (which is a journal / journaled field) inserted by a user into a task in some situations.
For that I have created a business rule of type "before" and "insert/update".
In the script of the business rule I do something similar to:


if (!gs.nil(current.comments)) {
   current.comments = current.comments + " additional text here";
}


The problem is that this does not modify the comment being inserted, but rather adds another comment. So if the user wrote "hello", we will have 2 comments:


hello additional text here
hello


I have searched the wiki and the forums and I have not found any easy way to do this. The only solution I can imagine is to delete the delete the older record from the sys_journal_field table but it would have to be done very well to not to delete the wrong comment.

Does anyone have any suggestion / ideas? 🙂

Thanks in advance,
David
9 REPLIES 9

tony_fugere
Mega Guru

What is even more interesting is the difference between INSERT and UPDATE (see attachment). I've tried to figure out a way around this without luck. Be careful when you modify the sys_journal_field table. You will likely need to delete the entry for this Incident from the Record History [sys_history_set] as well such that the INC rebuilds based on the journal change.

I think this may be a Service-now "feature/bug" though. Maybe report it on HI?


This is surprising to me as well, I am going to investigate some more.

dmartinc could you provide a bit more context, perhaps there is another way to do the desired outcome since this appears to not be functioning as we would expect.

I can confirm this is not functioning as you would expect. My opinion this is a bug, at least worth mentioning/discussing with Service Now.

I do not think it is worth scripting something to delete current journal entry and construct an entirely new one such as an "after" business rule.

/my two cents


dmartinc
Tera Expert

Thanks to a colleague, we found the solution 🙂

We created a business rule for the "sys_journal_field" table, not the "task" table.
So now the business rule is like this:
table: sys_journal_field
when: before
insert: true
And the script is like this:



if (!gs.nil(current.value)) {
var tableName = current.name;
var fieldName = current.element;
if (tableName == "task" && fieldName == "comments") {
current.value = ........... ;
}
}

And this works perfectly 🙂

Thanks both for your answers and I hope this helps you as well.


That solution you posted makes perfect sense, thanks for sharing!

Might be worth pointing out that this will only work for tables which extend task.

Ie. Not approvals or anything else that does not extend task since you are asserting tableName = "task". Not saying this is incorrect but wanted to mention it for others reference.