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

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

Hi Rasheed,

It is working for me now. Thank you for your support and help.

 

Aman Kumar S
Kilo Patron

Hey,

I can see a few issues with your script, please follow below script:

var user = current.getValue("assigned_to");
var reqItem = current.getValue("request_item");
var grItem = new GlideRecord("sc_req_item");
if(grItem .get(reqItem)){
    var getExisitngList = grItem.getValue("watch_list").split(',');
    getExisitngList.push(user);
    grItem.setValue("watch_list", getExisitngList.join(','));
    grItem.update();
}

 

Feel free to mark correct, If I answered your query.

Will be helpful for future visitors looking for similar questions 🙂

Best Regards
Aman Kumar

Kartik Sethi
Tera Guru
Tera Guru

Hi @Anshika 

For your requirement, you can follow the below-provided approach:

  • Requested Item backend name on sc_task: request_item
  • Business Rule on sc_task:
    • When: Before
    • Insert: True
    • Update: True
    • Condition: Assigned To â–º Changes

      find_real_file.png
    • Script:
      (function executeRule(current, previous /*null when async*/) {
      	var reqWatchList = [],
      		arrayUtil = new ArrayUtil(),
      		oldAgent = JSUtil.notNil(previous.assigned_to + '') ? previous.assigned_to + '' : '',
      		newAgent = JSUtil.notNil(current.assigned_to + '') ? current.assigned_to + '' : '',
      		reqItem = current.request_item.getRefRecord(); //Get the GlideRecord object of the RITM
      	
      	//-----Logic to add Assigned to in RITM-----
      	reqWatchList = reqItem.watch_list.split(',');
      	reqWatchList.push(newAgent);
      	
      	//-----Logic to remove previous Assigned to in RITM-----
      	if(oldAgent) {
      		reqWatchList.splice(reqWatchList.indexOf(oldAgent),1);
      	}
      	
      	
      	//Remove duplicate entries if any
      	reqWatchList = arrayUtil.unique(reqWatchList);
      	
      	reqItem.watch_list = reqWatchList.join(',');
      	reqItem.update();
      })(current, previous);

Please try and let me know if this works for you or not.

 


Please mark my answer as correct if this solves your issues!

If it helped you in any way then please mark helpful!

 

Thanks and regards,

Kartik