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.

Fix script to insert record according to condition

Priya75
Tera Contributor

Hi All,

 

I want to write a fix script to insert new records using existing values.

I have a table:- u_test 

inside that there are 4 attributes: - 

PatchingStartEndWeek (reference field)
Sat 01:00 -4:0014:00:0018:00:00first week
Fri 02:00 -5:0015:00:0016:00:00first week
Sun 00:00 -03:0015:00:0020:00:00first week

For weeks I have:- [week2, week3, week4] (reference value)

with each week i want to create new records with same patching, start and end .

I want to make new records with same values Just week needs to be changed to third week. So i need to insert 3 records with same start, end and patching Just need to update week field without updating the actual record.

5 REPLIES 5

Community Alums
Not applicable

Hi @Priya75 ,

You can refer below code 

var gr = new GlideRecord('u_test'); // your table name
gr.addQuery('patching', 'Sat 01:00 -4:00');
gr.query();
if(gr.next()){
	gr.week = <sys_id>; // sys_id of 3rd week record if this is reference field
	gr.update();
}

 

Please reach me out if you need anything. 

 

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

 

Thanks and Regards 

Sarthak

I don't want to update the record, I want to create new records with same attribute with different week.

Community Alums
Not applicable

Hi @Priya75 ,

In that case please refer below script 

var gr = new GlideRecord('u_test'); // your table name

gr.initialize();

gr.patching ='Sat 01:00 -4:00';

gr.week = <sys_id>; // sys_id of 3rd week record if this is reference field

gr.insert();

 

Please reach me out if you need anything. 

 

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

Thanks and Regards 

Sarthak

 

 

 

I don't want to hardcode it ,it should go to each record and then create a new record.