Inserting multiple records at once.

ServiceNowSteve
Giga Guru

Good Morning All,

This is a repost from one I made last night but didn't get a reply on so I am hoping the morning shift may be able to help.

I am working with the m2m_sp_status_subscription table and I have a question I hope you may be able to help with.

What I want to do when creating a new record on that table is have two form fields.

Field #1 - Reference Field pointing to our user table
Field #2 - A list pointing to a table of services we offer.

The object of the record is to match a user with a service which I can do on a 1:1 match. Now I need to make it so I can select multiple services and one user then when I save or submit the record I would like it to do a while loop through the list and insert a new record for that user and that service.

My issue is that I am not sure how to change the save and update buttons on the record form to do that loop through each item logic since ave and Update are global UI actions.

Any ideas how I might accomplish this?

 

find_real_file.png

6 REPLIES 6

Dubz
Mega Sage

Hi Steve,

 

Can you clarify, you're inserting a record into the m2m_sp_status_subscription table and then inserting more records into the same table based on the list field?

 

Where does the user create these subscriptions? I think the logic should be to have the list field on that form and then use an after insert/update business rule to loop through the list and add records to the m2m table.

 

You could do it after insert on the m2m table but it seems a bit strange doing it that way.

 

The script below is more or less what you'd need to have in your after insert BR, you might have to change the field names.

 

if(!u_service_list.nil()){
var services = current.getValue('u_service_list');
var gr = new GlideRecord('cmdb_ci_service');
gr.addEncodedQuery('sys_idIN' + services);
gr.query();

while(gr.next()){
var service = gr.getValue('service');
var gr2 = new GlideRecord('m2m_sp_status_subscription');
gr2.initialize();
gr2.user = current.user;
gr2.service = service;
gr.insert();
}
}

David, thank you so much for the reply...I have tried so many times to get an answer and you are the closest! really thank you a lot!

 

So I did what you said and it seems to save the record with the user and the service list but it's a single record:

find_real_file.png

I need it to list each service as a separate record in the table, can you go over the top and bring this home for me?

 

Hi Steve,

 

It was the field names that needed to be changed, when you get scripts off the community always check field names are correct! I've tested the below on my dev instance and it works, i'm still assuming here that you list field is called u_service_list

 

	var services = current.getValue('u_service_list');
	var gr = new GlideRecord('cmdb_ci_service');
	gr.addEncodedQuery('sys_idIN' + services);
	gr.query();
	
	while(gr.next()){
		var service = gr.getValue('sys_id');
		var gr2 = new GlideRecord('m2m_sp_status_subscription');
		gr2.initialize();
		gr2.sys_user = current.sys_user;
		gr2.cmdb_ci_service = service;
		gr2.insert();	
	}

Hi ,

List always store comma seperated sys_id. So modify your code:

 

var strServices='';
while(gr.next()){
var service = gr.getValue('service');
if(strServices!='')
strServices=strServices+','+service;
else
strServices=service;
}

var gr2 = new GlideRecord('m2m_sp_status_subscription');
gr2.initialize();
gr2.user = current.user;
gr2.service = strServices;
gr.insert();

 

Thanks