- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
‎01-10-2025 03:17 AM - edited ‎01-10-2025 03:17 AM
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.
Solved! Go to Solution.
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
‎01-10-2025 03:39 AM
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:
- Navigate to: System Definition > Script Includes.
 - 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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
‎01-10-2025 03:39 AM
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:
- Navigate to: System Definition > Script Includes.
 - 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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
‎01-10-2025 06:12 AM
Hi @Ankur Bawiskar ,
Thanks for your reply. I tried you additional script it is working when I reload from ServiceNow backend table but it is not working when I save or reload on service portal. Is there any way to save the field when I reload or save a record from portal.
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
‎01-10-2025 06:50 AM
Glad to know.
Please mark my response as correct as I answered your question so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
‎01-10-2025 06:54 AM
the function isFieldVisible() is not supported in portal.
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
