Adding assign to user from sc_task to RITM form's watchlist field.

Anshika2
Kilo Expert

Hi, I am trying to add the user from sc_task field 'assigned to' to the RITM form field 'watchlist'.

Watchlist is a glide list type field.

When the assigned to is filled on sc_task form then the user should get filled on watchlist on ritm.

Kindly help in achieving this.

I have made a after insert/update BR on sc_task.

Condition is when Assigned to is not empty.

Script 

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

    // Add your code here
    var user = current.assigned_to;
    var req = current.request_item;
    var gr = new GlideRecord(sc_req_item);
    gr.addQuery('number',req);
    gr.query();
    if(gr.next()){
        gr.watch_list = user;
        gr.insert();
    }

})(current, previous);

1 ACCEPTED SOLUTION

Hello,

Try below. Don't forgot to mark my answer as correct if that helps

var ritm = current.request_item.getRefRecord();  
var wlArr = ritm.watch_list.toString().split(','); 
var idx = wlArr.indexOf(previous.assigned_to.toString()); //get the index of sys_id of user you want to remove from watch list.
wlArr.splice(idx,1);      // Remove the specific user from array. idx is the index from which you want to remove and '1' is the number of elements you want to remove.
wlArr.push(current.assigned_to.toString());  
ritm.watch_list = wlArr.join();  
ritm.update();  

 

Please hit like and mark my response as correct if that helps
Regards,
Musab

View solution in original post

8 REPLIES 8

suvro
Mega Sage
Mega Sage

Try this

add the condition assigned_to changes

var user = current.assigned_to;
    var req = current.request_item;
    var gr = new GlideRecord('sc_req_item');
    gr.addQuery('sys_id',req);
    gr.query();
    if(gr.next()){
        gr.watch_list = gr.watch_list + user;
        gr.update();
    }

Hi Suvro,

It's not working still. BR is after update/insert. Is it ok?

Musab Rasheed
Tera Sage
Tera Sage

Hello,

Define a BR on catalog task table...

condition

current.assigned_to.changes() && current.assigned_to != ''

 

script

var ritm = current.request_item.getRefRecord();  
var wlArr = ritm.watch_list.toString().split(',');  
wlArr.push(current.assigned_to.toString());  
ritm.watch_list = wlArr.join();  
ritm.update();  

getRefRecord() should work

GlideRecord is not required please try this solution.

Please hit like and mark my response as correct if that helps
Regards,
Musab

Hi Rasheed,

Your solution is working for me but when assigned to is getting changed then it is adding new record in watchlist and not deleting the previous one. Do you know how it can be fixed?