Bussiness Rules - Copying Comment from Approval Record in Change Request Record.

Keang
Tera Contributor

Hello, 

I have been assigned with updating the change request process to automatically transfer comments from the approval table to the change record whenever a comment is present. I'm relatively new to working with the change request module, so I'm reaching out to the community for guidance and support. 

Here's my progress thus far.  Thank you

 

Keang_0-1719954512730.png

 

Condition: 

current.comments.changes() && (!current.sysapproval.isNil()) && current.sysapproval.sys_class_name == 'change_request'

Script:

function onBefore(current, previous) {
	
    var gr = new GlideRecord('change_request');
    gr.addQuery('sys_id', current.sysapproval);
    gr.query();
    if (gr.next())
    {
        gr.work_notes = "The following comments have been added by the approver " + current.approver.name + " for this Change request:\n " + current.comments;
        gr.update();
    }
}

 

2 ACCEPTED SOLUTIONS

SAI VENKATESH
Tera Sage
Tera Sage

Hi @Keang 

 

Can you try the below script:

business rule : before, insert/update

script:

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

    var changerequest = new GlideRecord('change_request');
    changerequest.addQuery('sys_id', current.document_id);
    changerequest.query();
    if (changerequest.next()) {
        changerequest.comments = changerequest.comments + "\n" + current.comments.getJournalEntry(-1); // Appending the latest comment
        changerequest.update();
    }

})(current, previous);

 

cn330.PNG

 

It is working in my PDI.

 

Thanks and regards

Sai Venkatesh

View solution in original post

Hi @Keang 

 

you can try the below code

 

(function executeRule(current, previous /*null when async*/ ) {
    if (!current.sysapproval.isNil() && current.sysapproval.sys_class_name == 'change_request') {

        var changeRequest = new GlideRecord('change_request');
        if (changeRequest.get(current.sysapproval.sys_id)) {
            var changeType = changeRequest.type.getDisplayValue(); // Assuming 'type' is the field holding change type

            if (changeType.toLowerCase() === 'normal') {
                changeRequest.comments = changeRequest.comments + "\n" + current.comments.getJournalEntry(-1);
                changeRequest.update();
            }
        }
    }
})(current, previous);

 

Thanks and Regards

Sai Venkatesh

View solution in original post

4 REPLIES 4

SAI VENKATESH
Tera Sage
Tera Sage

Hi @Keang 

 

Can you try the below script:

business rule : before, insert/update

script:

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

    var changerequest = new GlideRecord('change_request');
    changerequest.addQuery('sys_id', current.document_id);
    changerequest.query();
    if (changerequest.next()) {
        changerequest.comments = changerequest.comments + "\n" + current.comments.getJournalEntry(-1); // Appending the latest comment
        changerequest.update();
    }

})(current, previous);

 

cn330.PNG

 

It is working in my PDI.

 

Thanks and regards

Sai Venkatesh

Hello @SAI VENKATESH 

Thank you for helping.  Your suggestion code works.  Is it possible to trigger the business rule only on

Type: "Normal" not Emergency Change.  

Hi @Keang 

 

you can try the below code

 

(function executeRule(current, previous /*null when async*/ ) {
    if (!current.sysapproval.isNil() && current.sysapproval.sys_class_name == 'change_request') {

        var changeRequest = new GlideRecord('change_request');
        if (changeRequest.get(current.sysapproval.sys_id)) {
            var changeType = changeRequest.type.getDisplayValue(); // Assuming 'type' is the field holding change type

            if (changeType.toLowerCase() === 'normal') {
                changeRequest.comments = changeRequest.comments + "\n" + current.comments.getJournalEntry(-1);
                changeRequest.update();
            }
        }
    }
})(current, previous);

 

Thanks and Regards

Sai Venkatesh

Thank you so much @SAI VENKATESH .  It is working great .