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

Community Alums
Not applicable

Hi @Miko_aj Buczek , 

 

You are trying this in a background script, hence current.sys_id will not give anything. Try this:

 

var gr = new GlideRecord('incident');

gr.addEncodedQuery('short_descriptionISEMPTY');

gr.query();

while(gr.next())

{

var prod = new GlideRecord('sc_item_produced_record');

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

prod.query();

if(prod.next()) {

gr.short_description = prod.producer.name;

gr.update();

}

}

 

Please mark my response as helpful/correct if this helps

 

Jhansi Kollipar
Tera Expert

Hi @Miko_aj Buczek ,

 

Please try the below code,

var gr=new GlideRecord('incident');

gr.addEncodedQuery('short_descriptionISEMPTY');

gr.query();

while(gr.next()){

var prod=new GlideRecord('sc_item_produced_record');

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

prod.query();

if(prod.next()){

gr.short_description=prod.producer.name;

gr.update();

}

Mark this as helpful if this helps.

 

Regards,

Jhansi.

 

 

Sagar Pagar
Tera Patron

Hi  @Miko_aj Buczek,

Try this updated scripts -

 

var sagar = new GlideRecord("incident");
sagar.addQuery("short_descriptionISEMPTY");
sagar.query();
gs.info(sagar.getRowCount());
while (sagar.next()) {
	// gs.info(sagar.number);

	var pagar = new GlideRecord("sc_item_produced_record");
	pagar.addQuery("task.sys_id", sagar.getUniqueValue());
	pagar.query();
	// gs.info(pagar.getRowCount());
	if (pagar.next()) {
		// gs.info(pagar.task.number);

		sagar.short_description = pagar.short_description;
		sagar.setWorkflow(false); // disable all business rules during this script execution
		sagar.autoSysFields(false); // disable updating system fields.
		sagar.update();
	}
}

Thanks,

Sagar Pagar

The world works with ServiceNow

Ankur Bawiskar
Tera Patron
Tera Patron

@Miko_aj Buczek 

try this optimized code

1) always wrap code inside function

2) always use try catch block for exception handling

(function(){
	try{
		var gr=new GlideRecord('incident');
		gr.addEncodedQuery('short_descriptionISEMPTY');
		gr.query();
		while(gr.next()){
			var prod = new GlideRecord('sc_item_produced_record');
			if(prod.get('task',gr.getUniqueValue())){
				gr.short_description = prd.producer.name;
				gr.setWorkflow(false); // to avoid BR from triggering
				gr.updateMultiple();
			}
		}
	}
	catch(ex){
		gs.info(ex);
	}
})();

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