- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2025 01:06 AM
Hi All,
I have a requirement to create separate records in a custom table based on list collector values.
This is the form
Suppose user selects 2 accounts then 2 separate records will create with all the account details and number separately.
Or is there any way I can do this?
Please help.
Thanks,
Sam
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2025 01:21 AM - edited 01-24-2025 01:50 AM
Hello @Samiksha2
To create separate records in a custom table based on list collector values in ServiceNow, you can use a combination of client scripts and server-side scripts
Create an onSubmit client script to capture the list collector values and call a Script Include to create the records.
function onSubmit() {
var listCollectorValues = g_form.getValue('your_list_collector_field'); // Replace with your list collector field name
var ga = new GlideAjax('CreateCustomRecords');
ga.addParam('sysparm_name', 'createRecords');
ga.addParam('sysparm_values', listCollectorValues);
ga.getXMLAnswer(function(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer == 'success') {
g_form.addInfoMessage('Records created successfully.');
g_form.submit(); // Submit the form to refresh it
} else {
g_form.addErrorMessage('Error creating records.');
}
});
return false; // Prevent form submission until records are created
}
Create a Script Include to process the list collector values and create records in the custom table.
var CreateCustomRecords = Class.create();
CreateCustomRecords.prototype = Object.extendsObject(AbstractAjaxProcessor, {
createRecords: function() {
var values = this.getParameter('sysparm_values');
if (!values) {
return 'error';
}
var valuesArray = values.split(',');
var customTable = 'u_custom_table'; // Replace with your custom table name
for (var i = 0; i < valuesArray.length; i++) {
var gr = new GlideRecord(customTable);
gr.initialize();
gr.u_field_name = valuesArray[i]; // Replace with your field name and set other fields as needed
gr.insert();
}
return 'success';
},
type: 'CreateCustomRecords'
});
If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.
Thanks & Regards
Viraj Hudlikar.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-10-2025 12:02 AM
Hello @Samiksha2
For this issue if records are going to create on same table, then I would say that write a before insert BR and if your list collector has more than one record then split it and during initial insert use one of value and do iteration on list data and insert data at same time. If the list has only value then don't run the BR as per condition.
If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.
Thanks & Regards
Viraj Hudlikar.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2025 01:21 AM - edited 01-24-2025 01:50 AM
Hello @Samiksha2
To create separate records in a custom table based on list collector values in ServiceNow, you can use a combination of client scripts and server-side scripts
Create an onSubmit client script to capture the list collector values and call a Script Include to create the records.
function onSubmit() {
var listCollectorValues = g_form.getValue('your_list_collector_field'); // Replace with your list collector field name
var ga = new GlideAjax('CreateCustomRecords');
ga.addParam('sysparm_name', 'createRecords');
ga.addParam('sysparm_values', listCollectorValues);
ga.getXMLAnswer(function(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer == 'success') {
g_form.addInfoMessage('Records created successfully.');
g_form.submit(); // Submit the form to refresh it
} else {
g_form.addErrorMessage('Error creating records.');
}
});
return false; // Prevent form submission until records are created
}
Create a Script Include to process the list collector values and create records in the custom table.
var CreateCustomRecords = Class.create();
CreateCustomRecords.prototype = Object.extendsObject(AbstractAjaxProcessor, {
createRecords: function() {
var values = this.getParameter('sysparm_values');
if (!values) {
return 'error';
}
var valuesArray = values.split(',');
var customTable = 'u_custom_table'; // Replace with your custom table name
for (var i = 0; i < valuesArray.length; i++) {
var gr = new GlideRecord(customTable);
gr.initialize();
gr.u_field_name = valuesArray[i]; // Replace with your field name and set other fields as needed
gr.insert();
}
return 'success';
},
type: 'CreateCustomRecords'
});
If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.
Thanks & Regards
Viraj Hudlikar.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2025 01:35 AM
Hi @Viraj Hudlikar ,
Thanks for the reply. it is creating the records but have one problem. After clicking on Submit button page is not reloading. It should reload so that user can understand that form has been submitted.
Thanks,
Sam
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2025 01:39 AM
Also Records created successfully msg line didn't run.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2025 01:48 AM
Hi @Viraj Hudlikar ,
I removed return false in the client script. Now page is loading but it is creating extra record.