How to create a record by specifying the sys_id

miyu
Tera Guru

Is there a way to create a record in GlideRecord by specifying a sys_id?
For example, I would like to create a record with sys_id "12345678901234567890123456789012" in a script.
Is it possible?

1 ACCEPTED SOLUTION

Weird
Mega Sage

You can try using setNewGuidValue() to set the sys_id to what you want.
For example:

var inc = new GlideRecord('incident');
inc.initialize();
inc.short_description = "Testing";
inc.setNewGuidValue("12345678901234567890123456789012");
inc.insert();


Read more in the docs for GlideRecord
GlideRecord | ServiceNow Developers

View solution in original post

4 REPLIES 4

Weird
Mega Sage

You can try using setNewGuidValue() to set the sys_id to what you want.
For example:

var inc = new GlideRecord('incident');
inc.initialize();
inc.short_description = "Testing";
inc.setNewGuidValue("12345678901234567890123456789012");
inc.insert();


Read more in the docs for GlideRecord
GlideRecord | ServiceNow Developers

Kalyani Jangam1
Mega Sage
Mega Sage

Try below code

var gr = new GlideRecord('incident');
gr.initialize();
gr.description = 'Test123";
gr.setNewGuidValue("12345678901234567890123456789012");
gr.insert();

Ankur Bawiskar
Tera Patron
Tera Patron

@miyu 

try this

setNewGuidValue() is used to set sysId on record

gs.generateGUID() -> will generate new random sysId

var rec = new GlideRecord('incident');
rec.initialize();
rec.setValue('short_description', 'Testing');
rec.setNewGuidValue(gs.generateGUID());
rec.insert();

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

Dhruv Chandan
Giga Guru

Hi,

 

Providing a custom string as a sys_id is not a best practice as that sys_id may already be tagged to a record.

 

The best method would be to use "gs.generateGUID" which will always generate a unique string of sys_id to be consumed.

 

Hope this helps.

 

Regards,
Dhruv