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

@Samiksha2  - Do check now I edited code by adding a line in client script when we get response as success. 

No, Still that record is not submitting.  I clicked on submit but nothing is happening. (but records are creating.)

Samiksha2_0-1737713914976.png

 

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

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.
 

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