- 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:52 AM
@Samiksha2 - Do check now I edited code by adding a line in client script when we get response as success.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2025 02:19 AM
No, Still that record is not submitting. I clicked on submit but nothing is happening. (but records are creating.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-05-2025 03:03 AM
Hi @Viraj Hudlikar ,
Hope you remember you have given me the solution to create the records based on selection of account.
That is working for more than 1 account. But If I am selecting only one account then the record is not creating in the Communication record table.
Please help me.
Thanks,
Sam
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-05-2025 09:40 PM
Hello @Samiksha2
Yes, I do remember that I gave you a solution and it has a condition that if in list collector if only 1 account is selected then new record will not be created as the record creation was needed on same table so why to duplicate it so have prevented it from happening so.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-05-2025 09:59 PM
Hi @Viraj Hudlikar ,
Problem is when I am adding two accounts it is creating three records.
two separate and one with two accounts.
Thanks,
Sam