Insert Comments Busines Rules

Rick Larson
Tera Contributor

Hello, I have a BR that checks if a user has delegated their approver functions, if true, it inserted a comment notifying the user, but the comment in the preview appears as if it were a user who was notifying and not the system.
I will send my script and in 2 prints.

(function executeRule(current, previous /*null when async*/ ) {
 
    // Check if the approving user has delegated their functions
    var delegateGR = new GlideRecord('sys_user_delegate');
    //delegateGR.addEncodedQuery('user=' + current.approver + '^endsBETWEENjavascript:gs.beginningOfToday()@javascript:gs.endOfNextMonth()');
delegateGR.addQuery('user', current.approver);
delegateGR.addQuery('approvals','true');
    delegateGR.addQuery('ends', '>', current.sys_created_on);
    delegateGR.query();
 
    if (delegateGR.next()) {
        // Get the name of the user to whom the function was delegated
        var delegateTo = delegateGR.delegate.getDisplayValue();
var delegate = delegateGR.user.getDisplayValue();
 
        // Get the approval records of the approving user with requested status
        var approvalGR = new GlideRecord('sysapproval_approver');
        approvalGR.addQuery('approver', current.approver);
        approvalGR.addQuery('state', 'requested');
        approvalGR.query();
 
        // Add a comment to each approval record
        while (approvalGR.next()) {
 
 
var message = gs.getMessage('delegated their approval functions to:');
var notification = delegate + ' ' + message + ' ' + delegateTo;
approvalGR.comments.setJournalEntry(notification,'admin');
            //approvalGR.comments = delegate + ' ' + message + ' ' + delegateTo;
            approvalGR.update();
        }
     }
 
})(current, previous);


2 REPLIES 2

Community Alums
Not applicable

Hello @Rick Larson 

 

Can you please try the below code..

 

(function executeRule(current, previous /*null when async*/ ) {
 
    // Check if the approving user has delegated their functions
    var delegateGR = new GlideRecord('sys_user_delegate');
    //delegateGR.addEncodedQuery('user=' + current.approver + '^endsBETWEENjavascript:gs.beginningOfToday()@javascript:gs.endOfNextMonth()');
delegateGR.addQuery('user', current.approver);
delegateGR.addQuery('approvals','true');
    delegateGR.addQuery('ends', '>', current.sys_created_on);
    delegateGR.query();
    if (delegateGR.next()) {
        // Get the name of the user to whom the function was delegated
        var delegateTo = delegateGR.delegate.getDisplayValue();
var delegate = delegateGR.user.getDisplayValue();
 
        // Get the approval records of the approving user with requested status
        var approvalGR = new GlideRecord('sysapproval_approver');
        approvalGR.addQuery('approver', current.approver);
        approvalGR.addQuery('state', 'requested');
        approvalGR.query();
while (approvalGR.next()) {
var message = gs.getMessage('delegated their approval functions to:');
var notification = delegate + ' ' + message + ' ' + delegateTo; // Create a new comment record
var commentGR = new GlideRecord('sys_journal_field');
commentGR.setNewGuidValue();
commentGR.table_name = 'sysapproval_approver';
commentGR.field_name = 'comments';
commentGR.document_key = approvalGR.getUniqueValue();
commentGR.type = 'comment';
commentGR.value = notification;
commentGR.internal = true; // Set the comment as internal comment
GR.insert(); // Update the approval record, if needed
approvalGR.update();
}

}

 

Don't forget to mark my answer as Correct & Helpful, if applicable.

Krishna Sharma

Thank you for responding, unfortunately it is not working and giving this error Unique Key violation detected by database ((conn=327087) Duplicate entry 'undefined' for key 'PRIMARY')