how can I update legacy(old) records from server side script.

abhaysingh98
Tera Contributor

Hi,

 

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 on load client script. I am able to store empty fields data that are available in the form it is working fine for a new record and when a record is opened or reloaded however it is not saving empty fields data to old records. We need to manually open all the record then it is getting saved in backend but I don't want that manual process I want that all the legacy(old) record should get updated with empty field data automatically. I tried using scheduled job but it was not working.

This is the client Script I have used for storing empty fields data in a string type field.

 

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();

       
        // 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.');
            }
        });

    });

}
9 REPLIES 9

@abhaysingh98 

the script is checking if the field has value in it

gr[i] == ''

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

Yes it is checking the fields that has value in it but the issue is that I only want those fields which are visible on the form not all the available fields on the form.

@abhaysingh98 

how can you determine that in background script for old records?

I don't think you can

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

Is there any way we can reload all the record once because if we reload it then my on load script will update the field automatically but the issue is that I don't want to open all the records manually and then reload it.

@abhaysingh98 

Hope you are doing good.

Did my reply answer your question?

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