- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2023 11:32 AM
Hi All,
I am hoping someone can help me with my feeble attempts in creating a background script. I started thinking my information was going to have to come from 2 different tables, but then discovered that we have an m2m table that contains the case records along with the reason records. I need to show all duplicates based on the question, then the records that contain the question with a specific sys_id needs to be updated to a different question with a different sys_id so we can delete the three questions with the sys_id that are not the right ones for those cases. Below is a screen shot showing the table and conditions that will need to be used and I would like gs.info included to show the records that will get this update.
The script below is my feeble attemp of a script that works and I would like to see the record ids if possible. Thank you ahead of time for any help on this script.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-27-2023 01:10 PM
OK Annette, a couple things here.
First, I've started creating my background script as Fix Script instead. That way you don't lose the transitory nature of a background script, and also get the bonuses of the script editor.
Second, you should always try to put script like this within their own function (just in case). I took your script and revamped it how I think you intended it to be. Note that if you run this as is, and records are found - you are going to update the records! You can // (ie comment) out the gr.update() to prevent that from happening though, and just have it 'print' the results to you to verify.
AnnetteKBGScript();
function AnnetteKBGScript() {
var gr = new GlideRecord('x_mtbr_ebs_shareit_shareit_decision_reasons'); //table
//Filter for the state of vermont; paired as field name and value
gr.addQuery('shareit_cases', 'Vermont');
//Filter for the specific reason(s); paired as field name and question
gr.addQuery('reason', '83fe5364c31df5d03646468dc001315d').addOrCondition('reason', '9dbe1f64c31df5d03646468dc00131b9').addOrCondition('reason', 'b4432a3dc3e1b1d03646468dc001313d');
//83fe5364c31df5d03646468dc001315d - rights of survivorship question
//9dbe1f64c31df5d03646468dc00131b9 - beneficiary designations question
//b4432a3dc3e1b1d03646468dc001313d - our determination question
//Then execute filter(s) or only return the record with the conditions listed in the queries
gr.query();
/*Now we will iterate over the data and implement the business logic which is to replace the decision reasons above
with the same decision reason, but one with a different sys_id*/
while (gr.next()) {
gs.print('Record sys_id: ' + gr.sys_id); //Display records on screen that meet these conditions
if (gr.reason == '83fe5364c31df5d03646468dc001315d') { //2023 decison reason sys_id
gs.print('Record sys_id: ' + gr.sys_id + ' - rights of survivorship question');
gr.setValue('shareit_decision_reasons.reason', '0258c3a01b454d10b1082060604bcbeb'); //2022 decision reason sys_id
} else if (gr.reason == 'b4432a3dc3e1b1d03646468dc001313d') { //2023 decison reason sys_id
gs.print('Record sys_id: ' + gr.sys_id + ' - beneficiary designations question');
gr.setValue('shareit_decision_reasons.reason', 'b24847601b454d10b1082060604bcb73'); //2022 decision reason sys_id
} else if (gr.reason == '9dbe1f64c31df5d03646468dc00131b9') { //2023 decison reason sys_id
gs.print('Record sys_id: ' + gr.sys_id + ' - our determination question');
gr.setValue('shareit_decision_reasons.reason', '635843601b454d10b1082060604bcbd1'); //2022 decision reason sys_id
}
gr.update();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2023 01:02 PM
Unless there's some reason I'm missing, you don't need to use GlideAggregate,
And I don't understand what this means: "I need to show all duplicates based on the question, then the records that contain the question with a specific sys_id needs to be updated to a different question with a different sys_id so we can delete the three questions with the sys_id that are not the right ones for those cases. "
Are you simply saying that a Reference field needs to be updated from one sys_id to another? You don't mean you actually want to change the sys_id of a record, right?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-27-2023 09:37 AM
Hi Shane,
So I have an m2m table referencing two other tables. I agree the GlideAggregate isn't correct, it is what I had at the time.
So, what I am saying is I have a reason with sys id -
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-27-2023 01:10 PM
OK Annette, a couple things here.
First, I've started creating my background script as Fix Script instead. That way you don't lose the transitory nature of a background script, and also get the bonuses of the script editor.
Second, you should always try to put script like this within their own function (just in case). I took your script and revamped it how I think you intended it to be. Note that if you run this as is, and records are found - you are going to update the records! You can // (ie comment) out the gr.update() to prevent that from happening though, and just have it 'print' the results to you to verify.
AnnetteKBGScript();
function AnnetteKBGScript() {
var gr = new GlideRecord('x_mtbr_ebs_shareit_shareit_decision_reasons'); //table
//Filter for the state of vermont; paired as field name and value
gr.addQuery('shareit_cases', 'Vermont');
//Filter for the specific reason(s); paired as field name and question
gr.addQuery('reason', '83fe5364c31df5d03646468dc001315d').addOrCondition('reason', '9dbe1f64c31df5d03646468dc00131b9').addOrCondition('reason', 'b4432a3dc3e1b1d03646468dc001313d');
//83fe5364c31df5d03646468dc001315d - rights of survivorship question
//9dbe1f64c31df5d03646468dc00131b9 - beneficiary designations question
//b4432a3dc3e1b1d03646468dc001313d - our determination question
//Then execute filter(s) or only return the record with the conditions listed in the queries
gr.query();
/*Now we will iterate over the data and implement the business logic which is to replace the decision reasons above
with the same decision reason, but one with a different sys_id*/
while (gr.next()) {
gs.print('Record sys_id: ' + gr.sys_id); //Display records on screen that meet these conditions
if (gr.reason == '83fe5364c31df5d03646468dc001315d') { //2023 decison reason sys_id
gs.print('Record sys_id: ' + gr.sys_id + ' - rights of survivorship question');
gr.setValue('shareit_decision_reasons.reason', '0258c3a01b454d10b1082060604bcbeb'); //2022 decision reason sys_id
} else if (gr.reason == 'b4432a3dc3e1b1d03646468dc001313d') { //2023 decison reason sys_id
gs.print('Record sys_id: ' + gr.sys_id + ' - beneficiary designations question');
gr.setValue('shareit_decision_reasons.reason', 'b24847601b454d10b1082060604bcb73'); //2022 decision reason sys_id
} else if (gr.reason == '9dbe1f64c31df5d03646468dc00131b9') { //2023 decison reason sys_id
gs.print('Record sys_id: ' + gr.sys_id + ' - our determination question');
gr.setValue('shareit_decision_reasons.reason', '635843601b454d10b1082060604bcbd1'); //2022 decision reason sys_id
}
gr.update();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2023 02:02 PM
As stated by Shane J above, use for GlideAggregate is not appropriate in you script.
https://developer.servicenow.com/dev.do#!/reference/api/utah/server_legacy/c_GlideAggregateAPI
Here's a script that may help in what you are trying to do, I used a m2m table in my instance that has records. PA dashboards can have the same Tabs
var dup = new GlideRecord('pa_m2m_dashboard_tabs'); //table
//dup.orderBy('dashboard'); //field name
dup.groupBy('dashboard'); //field name
dup.query();
while(dup.next()) {
var dup1 = new GlideRecord('pa_m2m_dashboard_tabs'); //table
dup1.addQuery('tab', dup.tab);//field name
dup1.query();
var dupCount = dup1.getRowCount();
if (dupCount > 1) {
gs.info("Dashboard = " + dup.dashboard.getDisplayValue() + ", tab = " + dup.tab.getDisplayValue());
gs.info("Found " + dup1.getRowCount() + " records for tab: " + dup.tab.getDisplayValue());
while(dup1.next())
gs.info(dup1.dashboard.getDisplayValue() + ": " + dup1.tab.getDisplayValue());
}
}
Change table and field name to match what you are looking for.