Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

BackGround Script

Annette Kitzmil
Tera Guru

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.

 

var dup = new GlideAggregate('x_mtbr_ebs_shareit_shareit_decision_reasons'); //table
dup.groupBy('reason'); //field name
dup.query();
while(dup.next()) {
    var dup1 = new GlideRecord('x_mtbr_ebs_shareit_shareit_decision_reasons'); //table
    dup1.addQuery('reason', dup.reason);//field name
    dup1.query();
    dup1.next();
    while(dup1.next())
        gs.info(dup1);
}

 

AnnetteKitzmil_0-1700508407472.png

 

1 ACCEPTED SOLUTION

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();
    }
}

 

View solution in original post

5 REPLIES 5

Thank you for the script, you made my day.

I am waiting for your reply and till then I will help my brother writing his 750 words essay assignment and when I am done with it, I will come back here.