The CreatorCon Call for Content is officially open! Get started here.

On before business rule on comments

diogo_ramos
Mega Expert

Hi guys I currently have a problem, when I write a comment and I have a before business rule (comment changes), and I want to edit what was wrote in the comment field, when the business rule runs it duplicates the information as you can see:

Business Rule Conditions : Comments changes

(function executeRule(current, previous /*null when async*/) {

var comments_added = current.comments;

var merged = comments_added   + "SAMPLE TEXT";

current.comments = merged;

})(current, previous);

Do you guys have any idea how can I changed the comment added to include more test ?

1 ACCEPTED SOLUTION

So what is happening is that "comments" on Task table and it's extensions is a special one, it's not really a field, but part of a formatter. So, playing with the business rule here is not the right way, as the comments update is actually doing an insert into the sys_journal_field and sys_audit. So if you would like a business rule, it should have been one on the sys_journal_field before insert. But even so, I would not recommend going that far.



What if you try a before onSubmit client script like:



Screen Shot 2017-10-02 at 2.34.49 PM.png



Works for me at least.


View solution in original post

14 REPLIES 14

Hi! I am in a similar situation, I need to modify the comments and work_notes field before it is written to the database (mask specific PII). I get a simple BR to work, but i need to invoke a Script include, and for some reason it does seem to work differently than from example the Incident table. When i pass current object into the Script include, it is not handled in the same way as for an incident, which work perfectly. Instead it seems that "current" is not an object, rather a value when coming from sys_journal_field table (when logging data i get "undefined" instead of the expected value). 

I will answer myself here. I solved it by having one before business rule on sys_journal_field with all the code, didn´t manage to call the Script include and pass the correct values. Additional to this, as you might know, it´s not enough to change the journal field value. Sys_history_line and sys_audit records also need to be updated. I did this by calling the corresponding records (audit and history) in the journal BR and update them with the new value. A bit tricky and maybe not the slimmest solution, but it works.

 

Hope it will help someone else too.

Jay Janssen
Mega Contributor

Facing this same problem myself.  I'm unclear how you would reliably link between the sys_journal_field record and the sys_audit.    Would you mind posting your BR Code?

Sure Jay, sorry for the late response. I extracted the part from my script where I find the records in the sys_audit and the sys_history_line table. 

 

updateAudit();
updateHistory();
//update corresponding audit and history lines

function updateAudit(){

var jour_val = current.element_id.toString(); //get sys_id of the task record

var audit = new GlideRecord('sys_audit');
audit.addQuery('documentkey', jour_val); //find the audit records for the task record
audit.addQuery('newvalue', cc_info); //cc_info is the value I want to match the audit record against
audit.orderByDesc('record_checkpoint'); //well, since I limit the result to only one record I guess I can skip this 🙂
audit.setLimit(1);
audit.query();

if(audit.next()){
audit.setValue('newvalue', new_value); //new_value is the value I want to replace the previous value with (I am masking some data based on specific patterns)
audit.update();
gs.info('Audit entry with sys_id ' + audit.documentkey + ' updated');
}
}

function updateHistory(){

var jour_val = current.element_id.toString(); //get sys_id of the task record

var history = new GlideRecord('sys_history_line');
history.addQuery('set', jour_val);
history.addQuery('new', cc_info); //cc_info is the value I want to match the audit record against
history.orderByDesc('update');
history.setLimit(1);
history.query();

if(history.next()){
history.setValue('new', new_value); //new_value is the value I want to replace the previous value with (I am masking some data based on specific patterns)
history.update();
gs.info('History entry updated: ' + history.set);
}
}
gs.info('Data masking has processed data in: '+ current.getDisplayValue());

return new_value;
}

 

By the way, how do you get the nice JavaScript-style to the code nowadays? The Insert Code Sample button in the bar only paste an image of the code.

Hope this helps.

 

//Per

UsamaS
Giga Contributor

It looks like your business rule is appending text every time a comment is updated, which causes duplication. To avoid this, you can modify your script to check if the "SAMPLE TEXT" has already been added before appending it again. For example, you could use a condition like if (!current.comments.includes("SAMPLE TEXT")) to ensure it's only added once. This way, your comment field won’t keep duplicating the same text when you make further edits. I face the same issue on my 3 projects, Brand in Marketing, Brand Counters and Likely A Business