Run Script to Add/Delete on Related List
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2017 05:44 AM
Anyone have any examples of adding/removing items from a related list via a Run Script in a workflow? I am not finding much in my Google searches and I have a related list on our sys_user records and I wanted to update via a Run Script in a workflow.
Please mark this response as correct and/or helpful if it assisted you with your question.
Steven

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2018 09:53 AM
Hi Steven,
Did you ever figure out how to do this? I am trying to do the same as well.
-Wesley
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2018 10:34 AM
It really depends on the list and what you are trying to add.
In my instance, I was looking to track each application a user had been added to through Access Request. So essentially the related list of "Applications" on the User Record...I wanted to update that in a run script. It basically boiled down to getting the list of applications in an array, glide record to our app table, and pushing the users name to the "User Access" column for each application on the "Application" table for our related list.
So now a Users Record will have the applications they have had access request for on their user record. The example below is one user record showing the Applications they should currently have access for based on ServiceNow request:
This situation might be very different from yours, but if you want to see the script, let me know...it's our Access Request which handles "New Hire", "Special Access Request", "Position Change", "Remove Special Access", and "Terminate Access".
Please mark this response as correct and/or helpful if it assisted you with your question.
Steven

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2018 10:43 AM
Hi Steve,
Yes please, would like to compare your script with mine. My script is semi working. It is creating the records but my values are blank.
I am trying to take values from a List Colletor variable (i.e., Business Services) on a catalog request item and associate those records to a related list (Certificate record). Basically, I want to associate all Business Services that are tied to a Certificate record.
Basically doing this via a script (edit only).
Any assistance will be greatly appreciated!
-Wesley
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2018 10:52 AM
Position Change is complicated on our end (if it's part of their old job and not their new job, then we need to remove it) so some of this may be a bit confusing. The bottom portion of the script is for adding and in this section "//THE BELOW ADDS THE USER NAME TO THE APP" you'll notice that I pull the list of users for an app and split it, then push the new user to that split list, rejoin the list of users together, then add it back to the column on the table.
(I am looking at your thread you posted now to see if I see anything):
var apps = [];
var apps = workflow.scratchpad.apparraylist;
var length = apps.length;
gs.log("Length: " + length);
//REMOVING USER FROM APPS FOR POSITION CHANGE REQUEST ONLY
if(current.variables.type_of_request == 'position_change') {
var oldapps = [];
var oldapps = workflow.scratchpad.oldapparraylist;
var newapps = [];
var newapps = workflow.scratchpad.newapparraylist;
for(var i=0; i<length;i++){
if(oldapps.contains(apps[i]) && !newapps.contains(apps[i])) {
var oldappname = apps[i].trim();
var requestedfor = current.variables.requested_for;
var rel = new GlideRecord('u_founders_application_list');
rel.addQuery('u_application',oldappname);
rel.query();
if(rel.next()){
var accesslist = rel.u_user_access;
gs.log("Access List: " + accesslist);
var arrUtil = new ArrayUtil();
var accessListArray = rel.u_user_access.toString().split(',');
var arrayposition = arrUtil.indexOf(accessListArray,requestedfor);
gs.log("Position of App: " + arrayposition);
if(arrayposition>=0){
accessListArray.splice(arrayposition,1);
gs.log("Access List Array after Splice: " + accessListArray);
rel.u_user_access = accessListArray.join(',');
rel.update();
}
}
}
}
}
for(var i=0; i<length;i++){
//******************** START ADDING APPS TO USER RECORD RELATED LIST ****************************
//ATTEMPTING TO ADD APPS TO USER RECORD - New Hire, Special Access Request, and Position Change
if(current.variables.type_of_request == 'new_hire' || current.variables.type_of_request == 'access_request' || current.variables.type_of_request == 'position_change'){
var appname = apps[i].trim();
var requestedfor = current.variables.requested_for;
var rel = new GlideRecord('u_founders_application_list');
rel.addQuery('u_application',appname);
rel.query();
if(rel.next()){
var accesslist = rel.u_user_access;
gs.log("Access List: " + accesslist);
if(accesslist.indexOf(requestedfor)>=0){
}else{
//CHECK TO SEE IF THE USER NEEDS TO BE ADDED TO THE APP FOR POSITION CHANGE REQUEST
if(current.variables.type_of_request == 'position_change' && oldapps.contains(apps[i]) && !newapps.contains(apps[i])){
gs.log("APP NOT TO BE ADDED: " + appname);
}else{
//THE BELOW ADDS THE USERS NAME TO THE APP
var accessListArray = rel.u_user_access.toString().split(',');
accessListArray.push(requestedfor);
rel.u_user_access = accessListArray.join(',');
rel.update();
}
}
}
}
//ATTEMPTING TO REMOVE USER FROM APP IN USER RECORD - Remove Special Access
if(current.variables.type_of_request == 'remove_access' || current.variables.type_of_request == 'terminate_access'){
var appname = apps[i].trim();
var requestedfor = current.variables.requested_for;
var rel = new GlideRecord('u_founders_application_list');
rel.addQuery('u_application',appname);
rel.query();
if(rel.next()){
var accesslist = rel.u_user_access;
gs.log("Access List: " + accesslist);
var arrUtil = new ArrayUtil();
var accessListArray = rel.u_user_access.toString().split(',');
var arrayposition = arrUtil.indexOf(accessListArray,requestedfor);
gs.log("Position of App: " + arrayposition);
if(arrayposition>=0){
accessListArray.splice(arrayposition,1);
gs.log("Access List Array after Splice: " + accessListArray);
rel.u_user_access = accessListArray.join(',');
rel.update();
}
}
}
//******************** END ADDING APPS TO USER RECORD RELATED LIST ****************************
}
Please mark this response as correct and/or helpful if it assisted you with your question.
Steven
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2018 11:05 AM
So if I am reading your post right, you want to add each Business Service associated with a Certificate. Similiar to what I was doing. This piece of my code should be helpful:
//THE BELOW ADDS THE USERS NAME TO THE APP
var accessListArray = rel.u_user_access.toString().split(',');
accessListArray.push(requestedfor);
rel.u_user_access = accessListArray.join(',');
rel.update();
You want to pull your Business Services into a variable and split it, push the new Business Service you are currently looping through in your array to the split variable, then rebuild that column (rel.u_user_access above) with a join and update. This is basically adding a new Business Service each type you loop through the array. Just make sure your Business Service field is long enough to handle the length it could grow to.
You'll likely need something like this too:
if(accesslist.indexOf(requestedfor)>=0){
In my Example if it's greater than 0 it already exist...but basically you would want to check and make sure the Business Service didn't already exist before you added it to a certificate...assuming it could already be there.
Please mark this response as correct and/or helpful if it assisted you with your question.
Steven