Create your own (user) sys_id - ServiceNow Community
Mark Roethof
Tera Patron
Tera Patron

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

 

What is shared in this article, might not be best practice. Just sharing this to explore possibilities, find openings in ServiceNow, and have a mindset that your imagination is your limitation.

 

Hi there,

 

Just for fun this time, how to create your own (User) sys_id. I noticed there wasn't a Community article on this topic yet, so decided to write an article again.
Note: I just used sys_user as an example, though you could apply this in multiple places.

Case

When creating new records, ServiceNow automatically generates a unique sys_id. Though at some rare places in the system you'll notice there isn't actually a 32-character sys_id used but just a string. What the?!

So could we do this ourselves? For example on a User record, with your own firstname.lastname or your nickname? Yes, you can!

How to

You only have to apply setNewGuidValue. Note this will only work for new records, not for updating records.
I also applied autoSysFields. Just because I wanted to apply a custom Created date, so this would be my actual date of birth 🙂.

 

var gr = new GlideRecord('sys_user');
gr.initialize();
gr.setValue('user_name', 'mark.ragavan');
gr.setValue('first_name', 'Mark');
gr.setValue('last_name', 'Ragavan');
gr.setValue('sys_created_on', '1983-05-01 12:30:00');
gr.setNewGuidValue('mark.ragavan');
gr.autoSysFields();
gr.insert();

 

Result

A User record has been created with the above code. Have a look at the XML to verify that your custom sys_id has been applied.

 

find_real_file.png

---

And that's actually it! So easy to apply.

 

C

If this content helped you, I would appreciate it if you hit bookmark or mark it as helpful.

 

Interested in more Articles, Blogs, Videos, Podcasts, Share projects I shared/participated in?
- Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

 

Kind regards,


Mark Roethof

ServiceNow Technical Consultant @ Paphos Group
---

LinkedIn

Comments
Omkar Mone
Mega Sage

This is stunning Mark.

 

Thanks for this information, it's very helpful 🙂

 

Cheers.

Harish KM
Kilo Patron
Kilo Patron

Awesome Mark 🙂

PriyankaSadhwni
Tera Expert

Is there a way to delete or update such record(CI) where sys_id is some other value.?
Not able to achieve same with fix script

var gr = new GlideRecord('cmdb_ci_win_server');
gr.addEncodedQuery('nameSTARTSWITHGabc');
gr.query();
gs.log(gr.getRowCount());
gs.log(gr.getDisplayName());
gs.log(gr.sys_id.toString()); // this is coming undefined


if (gr.next()) {
    gs.log("Record is found to delete");
    
    //gr.deleteRecord();
  gr.setNewGuidValue('c7a5d7fd873e4550e67dc8c80cbb35e3');
    gr.update();
    gr.setWorkflow(false);
    gr.autoSysFields(false);

Allen Andreas
Administrator
Administrator

Hi,

the setNewGuidValue method appears to only be applicable when inserting a new record. It isn't used to overwrite what is already there.

Please mark reply as Helpful, if applicable. Thanks!

ShivarajG
Tera Contributor

It works perfectly in scoped applications also

 

var guid = gs.generateGUID(); // this will generate a new id
var hrCase = new GlideRecord('sn_hr_core_case');
hrCase.initialize();
hrCase.setNewGuidValue(guid);
---- set all other required fields.
var createdId = hrCase.insert();
 
// createdId and guid both will be same
Version history
Last update:
‎07-26-2024 01:07 AM
Updated by:
Contributors