How can I save my form data to backend database.

abhaysingh98
Tera Contributor

I have a string-type field in a form that stores the names of all the empty fields within that form. The names of these empty fields are captured using a client script, which makes the field data visible only on the frontend. However, I would like to save this data to the backend as well so that it becomes accessible in the list view of the corresponding table.

Currently, I am attempting to use g_form.save(); in my client script, but the form does not save due to the presence of mandatory fields that are left unfilled. How can I ensure that the data for empty fields is saved to the backend for all records, even when mandatory fields are not completed?

This is the client script I have used.

 

function onLoad() {
    var gaAct = new GlideAjax('x_roho_rwd.getEmptyFields');
    gaAct.addParam('sysparm_name', 'getEmptyFields');
    gaAct.addParam('sysparm_recordSysId', g_form.getUniqueValue());
    // g_form.setDisplay('empty_fields', false);

    gaAct.getXMLAnswer(function(response) {

        var array = response.split(",");
        var ind = 0;
        var y = 0;
        var arr = [];
        var clmValue = []
        for (ind = 0; ind < array.length; ind++) {
            if (g_form.isFieldVisible(array[ind])) {
                arr.push(array[ind]);
            }
        }
        for (y = 0; y < arr.length; y++) {
            var field = g_form.getLabelOf(arr[y]);
            clmValue.push(field);
        }
        //var newValue = arr.join(', ');
        var newValue = clmValue.join(', ');
        g_form.setValue('empty_fields', newValue);
        //g_form.save();

    });

}

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@abhaysingh98 

can you try this

Step 1: Modify the Client Script

Update your client script to call a GlideAjax script that will handle the backend update:

function onLoad() {
    var gaAct = new GlideAjax('x_roho_rwd.getEmptyFields');
    gaAct.addParam('sysparm_name', 'getEmptyFields');
    gaAct.addParam('sysparm_recordSysId', g_form.getUniqueValue());

    gaAct.getXMLAnswer(function(response) {
        var array = response.split(",");
        var ind = 0;
        var y = 0;
        var arr = [];
        var clmValue = [];
        for (ind = 0; ind < array.length; ind++) {
            if (g_form.isFieldVisible(array[ind])) {
                arr.push(array[ind]);
            }
        }
        for (y = 0; y < arr.length; y++) {
            var field = g_form.getLabelOf(arr[y]);
            clmValue.push(field);
        }
        var newValue = clmValue.join(', ');
        g_form.setValue('empty_fields', newValue);

        // Call GlideAjax to save the data to the backend
        var gaSave = new GlideAjax('x_roho_rwd.SaveEmptyFields');
        gaSave.addParam('sysparm_name', 'saveEmptyFields');
        gaSave.addParam('sysparm_recordSysId', g_form.getUniqueValue());
        gaSave.addParam('sysparm_emptyFields', newValue);
        gaSave.getXMLAnswer(function(response) {
            if (response === 'success') {
                console.log('Empty fields saved successfully.');
            } else {
                console.log('Failed to save empty fields.');
            }
        });
    });
}

Step 2: Create a Script Include

Create a Script Include to handle the backend update:

  1. Navigate to: System Definition > Script Includes.
  2. Create a New Script Include with the following details:
var SaveEmptyFields = Class.create();
SaveEmptyFields.prototype = {
    initialize: function() {},

    saveEmptyFields: function() {
        var recordSysId = this.getParameter('sysparm_recordSysId');
        var emptyFields = this.getParameter('sysparm_emptyFields');

        var gr = new GlideRecord('your_table_name'); // Replace with your table name
        if (gr.get(recordSysId)) {
            gr.setValue('empty_fields', emptyFields);
            gr.autoSysFields(false); // Prevent mandatory field validation
            gr.update();
            return 'success';
        }
        return 'failure';
    },

    type: 'SaveEmptyFields'
};

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

6 REPLIES 6

Thanks, it I tried isVisible() and now its working

@Ankur Bawiskar do you have any idea how can I update legacy records