Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Data from Catalog request to the target table

Rahul84
Tera Contributor

Hi @Ankur Bawiskar / @shloke04 / @Sandeep Dutta / Leaders,

I am having a tricky requirement for service catalog. Can you help me to start with the logic and guide me in the right path.

I am having two fields on a service catalog form. Fields are (User name & department name). 

Both fields are of type list_collector and variable attributes as glide_list.

User can select multiple values on the form for the fields ( User name & department name). 

So the exact requirement here is : 

Upon submission of a request , the values(data) selected for the fields ( User name & department name) should be inserted in one of the target table( for example table name is u_abc) in ServiceNow.

The target table is having three fields as (FieldA, FieldB & FieldC)

1). FieldA is a type of reference field.

2). FieldB is also a type of reference field. 

3). FieldC is a type of choice field. ( choices as type-user, type-department)

Now after submission of a service request , data of a field "User name" (type of list_collector on a form) should be added in a FieldA (type of a reference field) in the table.

Data of a field "department name" (type of list_collector on a form) should be added in a FieldB (type of a reference field) in the table.

Let me take an example for the scenario.

If a user is selecting 2 values for field "User name" (type of list_collector on a form) and 3 values for field "department name" (type of list_collector on a form) 

then total 5 entries should be inserted in the target table in ServiceNow 

"User name" -> FieldA ( two entries ) and this third field "FieldC " will set as "type-user" for two entries (in the table)

"department name" -> FieldB  (three entries)  and this third field "FieldC " will set as type-department for three entries (in the table)

I am able to implement the logic for the same and it is working fine but having addition requirement in this.

If the requested values are already available in the target table , then we do not need to insert the data. If it is new only then we need to insert.

for eg. If user is selecting 3 values from a glide_list on a catalog form and out of 3 values 1 is already available in the target table , then we need to insert only two value that are new.

Pls help me to extend my logic for this addition requirement.

var uList = objRITM.variables.user_list.toString();

var uList _split = uList .split(',');
if (uList _split.length> 0) {
for (var i = 0; i < uList _split.length; i++) {
var gr = new GlideRecord('table_name');
gr.initialize();
gr.user_field = uList _split[i];
gr.choice_field = type-user;
gr.insert();
}
}

Thanks !!

1 ACCEPTED SOLUTION

Hi,

something like this

var uList = objRITM.variables.user_list.toString().split(',');

for (var i = 0; i < uList.length; i++) {
	var gr = new GlideRecord('table_name');
	gr.addQuery('user_field', uList[i]); // query with user_field to check if record is already present
	gr.query();
	if(!gr.hasNext()){
		gr.initialize();
		gr.user_field = uList[i];
		gr.choice_field = type-user;
		gr.insert();
	}
}

Regards
Ankur

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

View solution in original post

9 REPLIES 9

Hi,

something like this

var uList = objRITM.variables.user_list.toString().split(',');

for (var i = 0; i < uList.length; i++) {
	var gr = new GlideRecord('table_name');
	gr.addQuery('user_field', uList[i]); // query with user_field to check if record is already present
	gr.query();
	if(!gr.hasNext()){
		gr.initialize();
		gr.user_field = uList[i];
		gr.choice_field = type-user;
		gr.insert();
	}
}

Regards
Ankur

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

Thanks Ankur let me try this and will let you know the result.

 

Rahul84
Tera Contributor

Hi @Ankur Bawiskar ,

Do we not required the if condition in this case , as earlier I have applied this if condition

if (uList.length> 0){

// here for loop condition

}

Pls let me know..

 

Hi @Ankur Bawiskar ,

Do we not required the if condition in this case , as earlier I have applied this if condition

if (uList.length> 0){

// here for loop condition

}

Pls let me know..

Thanks a lot Ankur , the solution worked..

Thanks !!