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

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.

Nishant8
Giga Sage

Hello @Samiksha2 , By any chance, did you manage to try writing BR? If not, then probably, can you write BR as described below and share the outcome?

1. write BR on 'sc_req_item' table (configure to run after update, but if not immediately records to be created then you can try async)

2. Read the variable using following script: 

var listRecored = current.variables.<list collector variable name>.toString().split(',');
3. loop thru the array and create the records in your table.
 
Regards,
Nishant