- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Friday
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
Saturday
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 Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Friday
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Saturday
who has the sn_customerservice_case_its table in their instance to test? Must come with some plugin/app.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
5 hours ago
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Saturday
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 Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/
