Clear user comments and replace in business rule
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-15-2022 08:19 AM
I am trying to create a business rule that will make links in the activity stream of a task clickable. I need to be able to remove the original comment and replace it with the version altered by the business rule to make it clickable from the activity stream. This is what currently happens:
The comment is posted twice. It is a before business rule, running when Comments To/From Customer changes. Here is what my code looks like, with things I've tried in the comments:
(function executeRule(current, previous /*null when async*/) {
var commentsCurrent = current.comments;
var commentsArr = commentsCurrent.split(' ');
var commWithLink = commentsArr.map(function(item, index, array){
if(item.indexOf('https://') != -1) {
item = "[code]<a href='" + item + "' target='_blank'>" + item + "</a>[/code]";
return item;
}
else {
return item;
}
});
var arrStr = JSON.stringify(commWithLink.join(' '));
//current.clearValue('comments');
current.comments = arrStr;
//current.setValue('comments', '');
//current.comments.setDisplayValue(arrStr);
//current.setValue('comments', commWithLink);
//current.update();
//}
})(current, previous);
I am not sure if I have syntax errors, or if it's something weird because it's a journal field. Also, if anyone knows how/if it's possible to remove the quotes from the new string, that would be nice too.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-15-2022 12:18 PM
This is a known problem with updating journal fields.
Try using an onChange client script instead to update the field on the form rather than in the object itself.
Aoife
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-16-2022 10:48 AM
Thanks for your suggestion. I tried creating the script as an onChange client script, but unfortunately it isn't working. Since it runs on change, it ends up endlessly looping and breaking the page when the link is replaced with an 'a' tag. Is there any way you know of to get around this? I am guessing that it's not possible.
I also tried doing it as an onSubmit script, which is a potential solution. However, it doesn't work with the post button below the journal fields, only when saving or updating the record. I looked into this, and found that the options are to try adding an event listener to the button (hasn't worked so far, not sure it's possible anymore), and hiding the post button to force users to submit the form another way. Do you know anything about adding functionality to the post button?
Here's my onSubmit code for future reference:
function onSubmit(){
var commentsCurrent = g_form.getValue('comments');
var commentsArr = commentsCurrent.split(' ');
var commWithLink = commentsArr.map(function(item, index, array){
if(item.indexOf('https://') != -1) {
item = "[code]<a href='" + item + "' target='_blank'>" + item + "</a>[/code]";
return item;
}
else {
return item;
}
});
jslog(commWithLink);
var arrStr = commWithLink.join(' ');
g_form.setValue('comments', arrStr, arrStr);
}
Again, thanks for your suggestion. The onSubmit client script does work, so even if I can't get the post button to work, it might be an acceptable but flawed solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-16-2022 11:50 AM
In your onChange script the first part should be:
if (isLoading || newValue === '' || newValue === oldValue) {
return;
}
Adding the newValue === oldValue should prevent looping.
Aoife