The CreatorCon Call for Content is officially open! Get started here.

Background script - fill empty field with value from other table

Miko_aj Buczek
Tera Contributor

Hello

 

I have a task to fill field "short_description" (on every incident, where short_description is empty) on "Incident" table with "producer" field name from table "sc_item_produced_record", by using background script, but im new in scripting and i stucked.

Short description from incident must be filled with this field

Miko_ajBuczek_0-1669283632892.png

I started like this, but i have no clue how to go on

Miko_ajBuczek_1-1669284439180.png

Please, assist.

Regards

1 ACCEPTED SOLUTION

Mohith Devatte
Tera Sage
Tera Sage

Hello @Miko_aj Buczek ,

So current is an object which you can use when you are writing the same script in business rule .

But in your case it is background script so current object is not accessible here so what you can do is for testing purpose copy the sys_id of a sample incident record like below 

 

1)Go to incident table and search for your incident record and do a right click on the incident number and click on copy sys_id(see below screenshot)

Screenshot 2022-11-24 at 16.01.18.png

 

 

Then come to your back ground script and paste your incident sys_id in the place of current.sys_id;

var gr = new GlideRecord('incident');

gr.addEncodedQuery('short_descriptionISEMPTY');
gr.addQuery('sys_id','your_incident_sys_id');
gr.query();
if(gr.next())

{

var pr = new GlideRecord('sc_item_produced_record');

pr.addQuery('task', gr.sys_id);

pr.query();

if(pr.next()) {

gr.short_description = pr.producer.name.toString();

gr.update();

}

}

HOPE THIS HELPS 

Mark my answer correct if this helps you 

Thanks

 

 

View solution in original post

7 REPLIES 7

@Miko_aj Buczek 

As per new community platform you can mark multiple responses as correct.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Mohith Devatte
Tera Sage
Tera Sage

Hello @Miko_aj Buczek ,

So current is an object which you can use when you are writing the same script in business rule .

But in your case it is background script so current object is not accessible here so what you can do is for testing purpose copy the sys_id of a sample incident record like below 

 

1)Go to incident table and search for your incident record and do a right click on the incident number and click on copy sys_id(see below screenshot)

Screenshot 2022-11-24 at 16.01.18.png

 

 

Then come to your back ground script and paste your incident sys_id in the place of current.sys_id;

var gr = new GlideRecord('incident');

gr.addEncodedQuery('short_descriptionISEMPTY');
gr.addQuery('sys_id','your_incident_sys_id');
gr.query();
if(gr.next())

{

var pr = new GlideRecord('sc_item_produced_record');

pr.addQuery('task', gr.sys_id);

pr.query();

if(pr.next()) {

gr.short_description = pr.producer.name.toString();

gr.update();

}

}

HOPE THIS HELPS 

Mark my answer correct if this helps you 

Thanks

 

 

Nivedita9
Tera Contributor

Hi,
Can you amend the line in your code:

prod.addQuery('task',gr.sys_id);.
it should be : 
prod.producer.getDisplayValue() not current.producer.name.
 
Amended code:

var grInc= new GlideRecord('incident');

grInc.addEncodedQuery('short_descriptionISEMPTY');

grInc.query();

while(grInc.next())

{

var prod = new GlideRecord('sc_item_produced_record');

prod.addQuery('task', gr.sys_id);

prod.query();

if(prod.next()) {

grInc.short_description = prod.producer.name;

grInc.update();

}

}

Note: Current refers to current table. (Where you have written your script).