Help in fix script as need to fetch values in the field

Arjun Reddy Yer
Mega Sage

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

var SG = new GlideRecord('sn_customerservice_case_its');
SG.addNotNullQuery('incident');
SG.query();
var count = 0;
while (SG.next()) {
   var incGR = new GlideRecord('incident');
   if (incGR.get(SG.incident) && incGR.business_service) {
       SG.business_service = incGR.business_service;
       //SG.update();
       count++;
       gs.print('Updated Case: ' + SG.number + ' → Business Service: ' + incGR.business_service.name);
   }
}
need to fetch in the below mentioned field either from history list or from related incident which is having "business_service" 
ArjunReddyYer_0-1762548100889.pngArjunReddyYer_1-1762548543837.png

 

 
1 ACCEPTED SOLUTION

kaushal_snow
Giga Sage

@Arjun Reddy Yer ,

 

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.

 

 

Thanks and Regards,
Kaushal Kumar Jha - ServiceNow Technical Consultant - ServiceNow Class of Legends 2025

View solution in original post

6 REPLIES 6

@kaushal_snow 

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.

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