Copy RITM comments and state to SC Task , copy comments on SC Task to RITM

rajasekharteja
Tera Guru

Hi All,

I got a requirement,

 

For SCTASK: Whenever the requested for responds to the requests, the state has to automatically move from Pending to Work In Progress(This is Done).

For Sequential SCTASK: If the state is pending, then the RITM should be in Pending and the Pending Reason should copy from SCTASK to RITM(This is Done)
For Parallel SCTASK: If any one of the SCTASK is in Pending , then the RITM should be in Pending and the Pending Reason should copy from SCTASK to RITM(This is Done)

All the comments should be copied from RITM to SCTASK and vice-versa. ( When comments are added on RITM by Requested for or Ticket Worker comments are getting duplicated on RITM and activities also shown duplicates i.e., State changes activity, but comments from RITM to SCTASK copying only once which is expected behavior.)

When 'Requested for' comments on RITM on front end the state is changing and comments are going to SCTASK but if 'Requested for' only adds additional comments comment's are not copying to SCTASK

 

All SCTASK comments should be copied to RITM only when SCTask's are in active states.

 

I have written below business rules on RITM and sc_task please help me with your inputs to achive the required functionality.

 

Business rule on RITM:

rajasekharteja_0-1753817064493.png

 

 

Script Section :

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

    // Add your code here
    var userID = gs.getUserID();
    if (userID == current.requested_for.toString() ||
        userID == current.opened_by.toString()) {
        current.state = '2'; // WIP
        var sctask = new GlideRecord('sc_task');
        sctask.addQuery('request_item', current.sys_id);
        sctask.query();
        var count = sctask.getRowCount();
        if (count == '1') {
            while (sctask.next())
            {
                sctask.state = current.state;
                sctask.comments = current.comments;
                sctask.u_pending_reason = current.u_pending_reason;
                sctask.update();
            }
        }
    }


})(current, previous);
 
 
Business rule on SC_Task:
rajasekharteja_1-1753817252415.png

 

 

Script on sc_task table:

(function executeRule(current, previous /*null when async*/ ) {
    // Add your code here

    var ritmRecord = current.request_item.getRefRecord();

    ritmRecord.state = current.state;

    if (current.state == '-5') {
        ritmRecord.u_pending_reason = current.u_pending_reason;
    }
    if(previous.state== '-5' && current.state != '-5'){
        ritmRecord.u_pending_reason = '';
    }
    ritmRecord.comments = current.comments;
    ritmRecord.update();
    ritmRecord.setWorkflow(false);


})(current, previous);
 
 
 
There is one more Business rule on sc_task to map task status to ritm state if atleast one task is in pending then we are setting the ritm state to pending this is parallel sc tasks.
rajasekharteja_0-1753817447671.png

 

Script:

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

        // Add your code here

        var ritm = current.request_item;

        var taskpending = checkTaskStatus('state=-5^request_item=' + ritm);
        var taskclosed = checkTaskStatus('stateIN3,4,7^request_item=' + ritm);

        if (taskpending == true) {
            updateritmstate('-5');
        } else if (taskclosed == true) {
            updateritmstate('3');
        }


        function checkTaskStatus(query) {
            var task = new GlideRecord('sc_task');
            task.addEncodedQuery(query);
            task.query();

            if (task.hasNext()) {
                return true;
            }
        }

        function updateritmstate(state) {
            var grritm = new GlideRecord('sc_req_item');

            if (grritm.get(ritm)) {
                    grritm.state = state;
                    grritm.update();
                    grritm.setWorkflow(false);
                    gs.addInfoMessage('ritm update');
                }

            }

        })(current, previous);
Thanks,
Rajasekhar

 

1 ACCEPTED SOLUTION

@rajasekharteja 

it's setting with hard-coded state value then it should work fine,

use this to fetch the comments

Did you print state value in logs?

sctask.comments = current.comments.getJournalEntry(1);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

15 REPLIES 15

@rajasekharteja 

BR on sc_req_item should be after update and not before

Also setWorkflow(false) line should be before update() and not after that

AnkurBawiskar_0-1753852402981.png

 

AnkurBawiskar_1-1753852429280.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar 

I have made the above changes, now additional comments are showing only once on ritm but the state on ritm is not setting to Work In progress(2) and sc_task also not updating with comments and state.

  if (userID == current.requested_for.toString() ||
        userID == current.opened_by.toString()) {
        current.state = '2'// WIP
        var sctask = new GlideRecord('sc_task');
        sctask.addQuery('request_item', current.sys_id);
        sctask.query();
        var count = sctask.getRowCount();
        if (count == '1') {
            while (sctask.next())
            {
                sctask.state = current.state;
                sctask.comments = current.comments;
                sctask.u_pending_reason = current.u_pending_reason;
                sctask.update();
            }

@rajasekharteja 

Glad to know that the duplicate issue is gone based on my comment.

since you mad the BR as after update you will have to use current.update() to set the state.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar 

 

I have updated RITM business rule script as below,

 

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

    // Add your code here
    var userID = gs.getUserID();
    if (userID == current.requested_for.toString() ||
        userID == current.opened_by.toString()) {
        var latestcomment = current.comments.getJournalEntry(1);
        if (!latestcomment) {
            return;
        }
        gs.addInfoMessage('cur requester');
        var ritm = new GlideRecord('sc_req_item');
        if (ritm.get(current.sys_id)) {
            ritm.state = '2'; // WIP
            ritm.update();
        }

        var sctask = new GlideRecord('sc_task');
        sctask.addQuery('request_item', current.sys_id);
        sctask.query();
        var count = sctask.getRowCount();
        if (count == '1') {
            gs.addInfoMessage('cur task if ' + sctask.number);
            while (sctask.next()) {
                gs.addInfoMessage('cur task while ' + sctask.number);
                sctask.state = current.state;
                sctask.comments = current.comments;
                sctask.u_pending_reason = current.u_pending_reason;
                sctask.update();
            }
        }
    }


})(current, previous);
 

Now RITM state also updating to WIP when requester comments on ritm but it's not updating sctask even though we are getting sctask.number in the info message. 

 

@rajasekharteja 

are the state choice values same in both sc_task and RITM?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader