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 Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/

View solution in original post

7 REPLIES 7

Voona Rohila
Mega Patron
Mega Patron

Hi @Arjun Reddy Yer 

 

Try below logic.

var SG = new GlideRecord('sn_customerservice_case_its');
SG.addNotNullQuery('incident');
SG.query();
var count = 0;
while (SG.next()) {
    if (SG.incident.business_service) {
        SG.business_service = SG.incident.business_service;
        //SG.update();
        count++;
        gs.print('Updated Case: ' + SG.number + ' → Business Service: ' + SG.incident.business_service.name);
    }
}

 Please replace 'SG' with proper variable name.

 

You can also replace the query to 'incident.business_serviceISNOTEMPTY^business_serviceISNOTEMPTY' and remove the if statement inside the while.


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

who has the sn_customerservice_case_its table in their instance to test? Must come with some plugin/app.

Yes but that's a sample code which I have provided.

 


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

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 Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/