Workflow run script to take List Collector values and populate Related List.

Wesley Breshear
Tera Expert

Hello,

Needing some help with a workflow run script.  I am trying to take reference values from a List Colletor variable (i.e., Business Services) on my catalog request item and associate those records to a related list of my Certificate record.  Basically, I want to associate all Business Services that is tied to Certificate record being viewed or Certificate record that is used by the following Business Services.

My script is adding records to my M2M related list table but my Business Service and Certificate Common Name columns are blank; so I am currently getting three blank records.

Assistance with what I might be missing?

//var cert = current.common_name.getValue('sys_id');  // Request Item reference variable of certificate record
//var list = current.variables.u_business_service_list;  // Request Item list collector of related business services

var cert = '9ad10b62dbd56f001bd3c170ba9619e1';  // Testing with certificate's sys_id
var list = [];
var list = 'App 1,App 2,App 3'; // Testing with strings 
var array = list.split(',');

for (var i=0; i < array.length; i++) {
        // M2M table, related list, certificate to business service
	var bs = new GlideRecord('x_teth_ekcm_m2m_business_ser_certificates');  
	bs.setLimit(10);
	bs.addQuery('business_service', '!=', array[i]);
	bs.addQuery('certificate_common_name', '!=', cert);
	bs.query();
	
	if(bs) {
		bs.initialize();
		bs.business_service = array[i];
		bs.certificate_common_name = cert;
		bs.update();
	}
}

Thank you,

-Wesley

1 ACCEPTED SOLUTION

Hi Wesley,

Can you do this; don't declare bsArray and use like this

var bsArray = current.variables.u_business_service_list.toString();

bsArray = bsArray.split(",");

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

View solution in original post

13 REPLIES 13

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Wesley,

Even single record is not getting updated

So in your case if record found you need to update so use following

if(bs.next()) instead of if(bs)

also why to initalise(); if you are updating the record found. you can remove the line of code

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

Hi Ankur,

Your suggestion breaks the script or not records are generated.  I suppose that I should use insert() but get confused between linking records in related list vs creating new.

I am trying to 'add' Business Services under the Related List of my Certificate record.  Same action as "Edit" under the related list tab.  But I also need to validate that is doesn't already exist, otherwise, I might get repeating items in my related list.

-Wesley

Hi Wesley,

So you want to insert into related list table only when already record not present for that combination then use this

if(!bs.next())

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

Hi Ankur,

It didn't resolve my issue.  I had to make numerous other changes, but I now have a new issue.  I have following script working great from fix/background scripts but now getting errors when I try to run from a Workflow 'Run Script'.  From Workflow Run Script it doesn't like how I am using the split method.

Can you see what I might be doing wrong.

(function () {
	var bsArray = [];
	bsArray.push(current.variables.u_business_service_list); // Request Item list collector of related business services
	//var bsArray = ['e26db3d6db68cf80ced2f1e51d961906', '0f74bc7edbb9cf04714df1041d9619c8']; //for testing Fix Script
	var bsList = bsArray.split(','); // Try to split out but getting error
	gs.info("WLB - Length: " + bsArray.length); // Getting '1', but should have 2 or more.
	gs.info("WLB - Contents: " + bsArray);  // Getting list of BS sys_id's
	gs.info("WLB - Business Service(s): " + bsList); // Not running due to error
	
	var cert = current.common_name; // Request Item reference variable of certificate record
	
	for (var i=0; i < bsList.length; i++) {
		var bsName= bsList[i].trim();
		//gs.info("WLB - bsName: " + bsName);
		
		var rel = new GlideRecord('x_teth_ekcm_m2m_business_ser_certificates'); // M2M table, related list, certificate to business service
		rel.addQuery('business_service', bsName);
		rel.query();
		
		if(rel.next()) {
			var certList = rel.certificate_common_name; // .toString().split(',');
			if(certList.indexOf(cert)>=0) {
				//gs.info("WLB - certList: " + certList);
			} else {
				rel.initialize();
				rel.business_service = bsName;
				rel.certificate_common_name = cert;
				rel.insert();
			}
			// Add relationship record(s) if it does not exist all. [ if(!rel.next()) ]
		} else {
			rel.initialize();
			rel.business_service = bsName;
			rel.certificate_common_name = cert;
			rel.insert();
		}
	}
})();

gs.info:

WLB - Contents: c5046619dbf5ba80ced2f1e51d9619c9,85046619dbf5ba80ced2f1e51d9619ce

WLB - Length: 1

 

org.mozilla.javascript.EcmaError: Cannot find function split in object 85046619dbf5ba80ced2f1e51d9619ce,c5046619dbf5ba80ced2f1e51d9619c9.
Caused by error in Add to related list - advanced script at line 1

==> 1: (function () { 
2: var bsArray = []; 
3: bsArray.push(current.variables.u_business_service_list); // Request Item list collector of related business services 
4: var bsList = bsArray.split(','); 


org.mozilla.javascript.EcmaError: Cannot find function split in object 85046619dbf5ba80ced2f1e51d9619ce,c5046619dbf5ba80ced2f1e51d9619c9.
Caused by error in sys_script.2d0885b4c61122840070856bf5994bca.script at line 5

2: var bsArray = []; 
3: bsArray.push(current.variables.u_business_service_list); // Request Item list collector of related business services 
4: var bsList = bsArray.split(','); 
==> 5: gs.info("WLB - Length: " + bsArray.length); // Getting '1' 
6: gs.info("WLB - Length: " + bsList.length); // Getting 'undefined' 

 

Thank you,

-Wesley