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

You're right Diogo, as I am able to reproduce this on OOB Jakarta instance. Give me some time so I can dig in to understand why this is happening.


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.


Thanks a lot for your investigation Sergiu, I thought about the same client script idea and I think it's the way we going to do it.



Cheers,


Diogo


If anyone is interested this is how I did it using a simple script:





function onSubmit() {


//Type appropriate comment here, and begin script below



function getSignature() {



var ga = new GlideAjax('getMySignature');


ga.addParam('sysparm_name','getMySignature');


ga.addParam('sysparm_user_id',g_user.userID);


ga.getXMLWait();


return ga.getAnswer(); // synchronous call wait until we got the value then proceed with the update


}



var comment = g_form.getValue("comments");


if (comment != "")


g_form.setValue("comments", comment + "\n\n" + getSignature());





}











Cheers


Diogo


harinalamalapu
Giga Expert

The onSubmit() approach doesn't seem to work when comments are posted by clicking 'Post' button in the Journal section, not by clicking Update / Save on the form. Can anyone think of a way to modify the comments when user clicks on 'Post' button without writing a Before Business Rule on sys_journal_field ?