- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2025 12:50 PM
required help @Vasantharajan N @Ankur Bawiskar @RaghavSh
As I need to fetch the business_service values from the history list and as well as from the related incident.
As the "business_service" value is missing in the records.
By using fix script need to get the missing "business_service" in to the field.
tried with below script but not working
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-08-2025 08:37 AM
Try below code:
var caseGR = new GlideRecord('sn_customerservice_case_its');
caseGR.addQuery('business_service', '');
caseGR.addNotNullQuery('incident');
caseGR.query();
var count = 0;
while (caseGR.next()) {
var incGR = new GlideRecord('incident');
if (incGR.get(caseGR.incident)) {
if (!caseGR.business_service && incGR.business_service) {
caseGR.business_service = incGR.business_service;
caseGR.update();
count++;
gs.print('Updated Case: ' + caseGR.number + ' Business Service from Incident: ' + incGR.business_service.name);
} else if (!caseGR.business_service) {
var histGR = new GlideRecord('sys_audit');
histGR.addQuery('documentkey', incGR.sys_id);
histGR.addQuery('fieldname', 'business_service');
histGR.orderByDesc('sys_created_on');
histGR.query();
if (histGR.next() && histGR.oldvalue) {
caseGR.business_service = histGR.oldvalue;
caseGR.update();
count++;
gs.print('Updated Case: ' + caseGR.number + ' Business Service from History: ' + histGR.oldvalue);
}
}
}
}
gs.print('Total updated records: ' + count);
You should query your case table, filter only records where business_service is empty and a related incident exists, then for each record get the incident (via incident.get()), check if the incident business_service field has a value and if so assign it to the case and update the record, and if not then query the sys_audit table for the incident business_service change history (with documentkey = incident.sys_id and fieldname = 'business_service'-ordered by sys_created_on descending) use the latest oldvalue to update the case and then update....
If you found my response helpful, please mark it as ‘Accept as Solution’ and ‘Helpful’. This helps other community members find the right answer more easily and supports the community.
Kaushal Kumar Jha - ServiceNow Technical Consultant - ServiceNow Class of Legends 2025
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-08-2025 10:27 AM
Way too much logic for a simple update. And querying sys_audit is very like to cause performance degradation. @Arjun Reddy Yer script has been verified independently.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-10-2025 09:31 AM
I agree with @WillieW , Querying audit table is not recomended and we don't have to query incident table too because it's already a reference field and all the details can be simply dot-walked.
Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP