How to populate a field with approval comments?

astanley
Tera Contributor

I have a business rule running on the sysapproval_approver table after insert/update with the filter conditions:

Comments > changes

Approval for.Task type > is > Change Request

created to update the "CAB Recommendation" field on the change request form when it gets approved by our change manager. Here is the script I used:

	// Adding approval comment to CAB recommendation field on change request record
	var updateInitiatedBy = updateInitiatedBy || current.sys_id.toString();

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

    if (updateInitiatedBy != current.sys_id.toString()) {
        return;
    }

    var gr = new GlideRecord('change_request');
    gr.get(current.sysapproval);
    gr.addQuery('approval','requested');
    gr.cab_recommendation = current.comments.getJournalEntry(1);
	gr.setWorkflow(false);
    gr.update();


})(current, previous);

Right now it is currently populating the field like so: find_real_file.png

I need to modify this script so that only the comment gets populated but not the time, approver, reply from, etc. All of the information at the top and bottom (Ref:MSG268) I want to remove from the field. How can I achieve this?

1 ACCEPTED SOLUTION

Jaspal Singh
Mega Patron
Mega Patron

Try below

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

    if (updateInitiatedBy != current.sys_id.toString()) {
        return;
    }

    var gr = new GlideRecord('change_request');
    gr.addQuery('sys_id', current.sysapproval);
    gr.addQuery('approval', 'requested');
    gr.query();
    if (gr.next()) {
        var getcomments = new GlideRecord('sys_journal_field');
        getcomments.addQuery('element_id', current.sys_id);
        getcomments.addQuery('element', 'Comments');
        getcomments.query();
        if (getcomments.next()) {
            gr.cab_recommendation = getcomments.value;
            gr.setWorkflow(false);
            gr.update();
        }
    }

})(current, previous);

View solution in original post

1 REPLY 1

Jaspal Singh
Mega Patron
Mega Patron

Try below

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

    if (updateInitiatedBy != current.sys_id.toString()) {
        return;
    }

    var gr = new GlideRecord('change_request');
    gr.addQuery('sys_id', current.sysapproval);
    gr.addQuery('approval', 'requested');
    gr.query();
    if (gr.next()) {
        var getcomments = new GlideRecord('sys_journal_field');
        getcomments.addQuery('element_id', current.sys_id);
        getcomments.addQuery('element', 'Comments');
        getcomments.query();
        if (getcomments.next()) {
            gr.cab_recommendation = getcomments.value;
            gr.setWorkflow(false);
            gr.update();
        }
    }

})(current, previous);