How to create two records on submit of a custom form

Samiksha2
Mega Sage

Hi All,

I have a requirement to create separate records in a custom table based on list collector values.

 

This is the form

Samiksha2_0-1737709412434.png

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

2 ACCEPTED SOLUTIONS

Viraj Hudlikar
Giga Sage

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.

View solution in original post

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.

View solution in original post

11 REPLIES 11

Viraj Hudlikar
Giga Sage

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.

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

Also Records created successfully msg line didn't run.

Hi @Viraj Hudlikar ,
I removed return false in the client script. Now page is loading but it is creating extra record. 

Samiksha2_0-1737712063664.png