Using Background script I need to create 10 incident records in incident table?

Lucky18
Kilo Contributor

And also out of 10 records 5 records caller id should be one user and the other 5 records should be another user.

As I am new to service now please let me know to code it

Thanks in advnace!

1 ACCEPTED SOLUTION

Community Alums
Not applicable

Hi Shankar,

I have used the following script and the records are created.

 

for(var i = 0 ; i < 5 ; i++)
{
var gr = new GlideRecord('incident');
gr.initialize();
gr.caller_id = '62826bf03710200044e0bfc8bcbe5df1';
gr.insert();
}
for(var i = 0 ; i < 5 ; i++)
{
var gr = new GlideRecord('incident');
gr.initialize();
gr.caller_id = 'a8f98bb0eb32010045e1a5115206fe3a';
gr.insert();
}

 

If the above code doesn't work for you, check for ACLs that might prevent you writing on the field. I ran the above code and records got created. See the attachment.

find_real_file.png

View solution in original post

13 REPLIES 13

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Something like this?

var callerSysId = '0a826bf03710200044e0bfc8bcbe5d7a,be82abf03710200044e0bfc8bcbe5d1c';
callerSysId = callerSysId.split(',');

for(var i = 0; i < callerSysId.length; i++) {
	var loopInt = 5;

	for(var j = 0; j < loopInt; j++) {
		var grIncident = new GlideRecord('incident');
		grIncident.newRecord();
		grIncident.setValue('caller_id', callerSysId);
		grIncident.insert();
	}
}

If my answer helped you in any way, please then mark it as helpful.

Please mark this answer as correct if it solves your problem. This will help others who are looking for a similar solution. Also marking this answer as correct takes the post of the unsolved list.
Thanks.

Kind regards,
Mark

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

this works as intended (ie, caller is populated) when Line 10 is updated to:

grIncident.setValue('caller_id', callerSysId[i]);

Lucky18
Kilo Contributor

Yeah its working but the caller id was empty in incident table of all 10 records

That's because the SYS ID Mark Ragavan has used are just sample Sys ID. You need to add the SYS ID of the users you want to create the incidents for. So go to the user for which you want to create incident, copy the SYS ID and substitute the SYS ID in below line:

 

var callerSysId = '0a826bf03710200044e0bfc8bcbe5d7a,be82abf03710200044e0bfc8bcbe5d1c';