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

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

Bingo!  Thanks for all your help.  This one was a major struggle for me.

Here is the final code that 'finally' works.

(function () {
	var bsArray = current.variables.u_business_service_list.toString();  // Request Item list collector of related business services
	var cert = current.variables.common_name;  // Request Item reference variable of certificate record
	bsArray = bsArray.split(",");
	//gs.info("WLB - Length bsArray: " + bsArray.length);
	//gs.info("WLB - Contents bsArray: " + bsArray);
	
	for (var i=0; i <bsArray.length; i++) {
		var bsName = bsArray[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()) {
			// Pulling from current M2M table to validate if cerificate and business service already exists
			var certList = rel.certificate_common_name;
			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();
		}
	}
})();

-Wesley

Hi Ankur,

I struggle sometimes to know if I need function declaration or not.  I noticed in my workflow run script it is not needed and appears to function the same with or without it.  Do you have a rule or condition when it should be used?

(function () {

})();

Thanks,

-Wesley

Hi Wesley,

I didn't get the context. Can you explain me more what you are trying to ask?

Regards

Ankur

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

Nilanjan1
Mega Sage

@Ankur Bawiskar : I am exactly in the same situation as this post. This is what I am trying to achieve: 

I have a custom table which houses a list of applications. Each application has a owner...for audit purpose every owner would check the their list of applications and change ownership to a different person as required through the form. when I select more than 2 application and select the applications owners only one is getting updated , specifically the first name that I select and not the second one. 

Application Name : List Collector

Ownership : List Collector

var cldappl = current.variables.application_names.toString();//List Collector
var gr = new GlideRecord('u_table_name');
gr.addQuery('sys_id', cldappl);
gr.query();
while (gr.next()) {
    gr.u_business_owner = current.variables.please_update_the_business_owner;//field is getting updated in the table this is again a list collector.
    gr.update();
}