Pulling Approvers from Affected CIs

olivershields
Kilo Explorer

Hi,

I am trying to automate the change process to pull a list of approvers from all the affected CIs when the change state is set to 'Approval requested'.

I can get it working when i pull from a single Reference field on a CI but many items have multiple approvers required on them and when i try to pull it from a List field it does nothing at all.

Im using the below script which has been adapted from one i found here.

var answer = [];


var affCIs = new GlideRecord('task_ci');
affCIs.addQuery('task', current.sys_id);
affCIs.query();
while(affCIs.next()){
    answer.push(affCIs.ci_item.u_approver__person_); !
}

u_approver__person is the name of the list field im trying to pull from. If you can suggest a field that can serve this function better or correct the script to pull from the List correctly that would be great.

Thanks

1 ACCEPTED SOLUTION

Here's an example .. Make sure that the affected CI list is loaded before this activity is being called ...



answer = [];



try


{


      var approvers = '';


      var ci = new GlideRecord('task_ci');


      ci.addQuery('task',current.sys_id);


      ci.query();


      while (ci.next()) {


              if(approvers.length == 0)


                      {


                      approvers = approvers + ci.ci_item.u_approver__person_.toString();


              }


              else


                      {


                      approvers = approvers + ',' + ci.ci_item.u_approver__person_.toString();


              }


      }


     


      var approversArray = (approvers.toString() + ',').split(',');


     


      for(i=0;i<approversArray.length;i++)


              {


              if(JSUtil.notNil(approversArray[i]))


                      {


                      answer.push(approversArray[i]);


              }


      }


}



catch(err)


{


      gs.log('Error While Processing-'+err + '-' +approvers);


}


View solution in original post

19 REPLIES 19

didnt realise i left those first 2 lines in. When i take them out and check the code again it says:



WARNING at line 2: Expected a string and instead saw var.

ERROR at line 2: Expected ':' and instead saw 'approvers'.


Have created a demo for you ..



URL : https://demo023.service-now.com/login.do


Username and Password : admin



Create a new emergency change ticket and fill the watch list before submitting the ticket .. Ecab approvers are loaded using the watch list field .. Check the emergency workflow for your reference ...



Let me know if you still face any issue.


I think the problem is that im trying to pull from a field inside an item on a related list.



I have a field called 'u_approver__person_' which is on each CI. I want to use the 'Affected CI' related list to pull from that field and put the user listed into the approvals. The demo you have setup is pulling from a field that is part of the change form so seems a lot simpler.


Is the field on the CI a reference field or glidelist ?


And the field is on each CI or the affected CI related list?