Issue with the background script

Pooja Khatri
Tera Contributor

I am trying to update my work notes from the background script for condition where impact is null and then impact is set to 5 but my worknotes are not getting updated 

 

Why is the Background script not working ?

Background Script : 

 

My work notes are not getting updatd with the below script

var gr = new GlideRecord('incident');
gr.addEncodedQuery('active=true^impact=NULL');

var text = 'See the following kb article for more info: ';
var url = '[code]<a href="' + 'https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0867584' + '" target="_blank">' + 'Knowledge Article View - Employee Support Center' + '</a>[/code]';
gr.query();

while (gr.next()) {
if (gr.priority == '5') {
gr.impact = '5';
gr.urgency = '3';
gr.work_notes= text + url;
gr.setWorkflow(false);
gr.update();
}

}

6 REPLIES 6

Anurag Tripathi
Mega Patron
Mega Patron

Hi,

You wont be able to update worknotes if you are using gr.setWorkflow(false);

Remove that and then it will update work notes too, but then you open yourself to the risk of triggering other BRs

-Anurag

Hi @Anurag Tripathi - tes post removing I am able to update the worknotes but  it will trigger the Business rules , any other way I can handle this scenario ?

trying to split update in 2 

 

var gr = new GlideRecord('incident');
gr.addEncodedQuery('active=true^impact=NULL');
var text = 'See the following kb article for more info: ';
var url = '[code]<a href="' + 'https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0867584' + '" target="_blank">' + 'Knowledge Article View - Employee Support Center' + '</a>[/code]';
gr.query();
while (gr.next()) {
if (gr.priority == '5') {
gr.setWorkflow(false);
gr.impact = '5';
gr.urgency = '3';
gr.update();
gr.setWorkflow(true);
gr.work_notes= text + url;
gr.update();
}

}
-Anurag

Community Alums
Not applicable

Hi @Pooja Khatri ,

I tried your script in my PDI and it works for me please check below script 

var gr = new GlideRecord('incident');
// gr.addEncodedQuery('active=true^impact=NULL');
gr.addEncodedQuery('active=true^impact=3');
var text = 'See the following kb article for more info: ';
var url = '[code]<a href="' + 'https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0867584' + '" target="_blank">' + 'Knowledge Article View - Employee Support Center' + '</a>[/code]';
gr.query();

if (gr.next()) {
    if (gr.priority == '5') {
        gs.print("Num = " + gr.number);
        gr.impact = '5';
        gr.urgency = '3';
		gr.short_description = "Test1q23";
		gr.work_notes = text + url;
        gr.update();
    }

}

Result 

SarthakKashyap_0-1717150162904.png

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards 

Sarthak