how can I update legacy(old) records from server side script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2025 12:57 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2025 01:04 AM
To update all legacy records with the empty field data automatically, you can use a background script in ServiceNow. This script will iterate through all the records and update the empty_fields field without the need for manual intervention. Here's an example of how you can achieve this:
- Create a Script Include to handle the logic for fetching empty fields:
var EmptyFieldsUtil = Class.create();
EmptyFieldsUtil.prototype = {
initialize: function() {},
getEmptyFields: function(recordSysId) {
var gr = new GlideRecord('your_table_name'); // Replace with your table name
if (gr.get(recordSysId)) {
var emptyFields = [];
gr.getFields().forEach(function(field) {
if (gr[field.getName()].nil()) {
emptyFields.push(field.getName());
}
});
return emptyFields.join(', ');
}
return '';
},
type: 'EmptyFieldsUtil'
};
- Create a Background Script to update all records:
updateRecords();
function updateRecords() {
try {
var gr = new GlideRecord('your_table_name'); // Replace with your table name
gr.query();
while (gr.next()) {
var emptyFields = new EmptyFieldsUtil().getEmptyFields(gr.sys_id);
gr.empty_fields = emptyFields;
gr.update();
}
} catch (ex) {
gs.info(ex);
}
}
This background script will go through each record in your specified table, determine the empty fields using the EmptyFieldsUtil Script Include, and update the empty_fields field accordingly.
Make sure to test this script in a development or test environment before running it in production to ensure it works as expected and doesn't impact performance.
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-13-2025 03:15 AM
Hi @Ankur Bawiskar ,
Getting this error while executing the code in background script.
JavaException: com.glide.script.fencing.MethodNotAllowedException: Function getFields is not allowed in scope x_roho_rwd
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2025 03:21 AM
you never told you are in scoped app
getFields() won't work in scoped app
try this
var EmptyFieldsUtil = Class.create();
EmptyFieldsUtil.prototype = {
initialize: function() {},
getEmptyFields: function(recordSysId) {
var gr = new GlideRecord('your_table_name'); // Replace with your table name
if (gr.get(recordSysId)) {
var emptyFields = [];
for (var i in gr) {
if (gr[i] == '' && !i.toString().startsWith('sys'))
emptyFields.push(i.toString());
}
return emptyFields.join(', ');
}
return '';
},
type: 'EmptyFieldsUtil'
};
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-13-2025 03:57 AM - edited 01-13-2025 04:02 AM
Hi @Ankur Bawiskar ,
I have to show only those fields that are visible on the form and are empty your from your script I am getting all the fields that are there in the form.
I already have a script include and client script that are setting this field value when form is loaded now the issue is that I have to load all the tickets manually so is there any way I can load all the legacy records so that it will update automatically.