Can sys_id be retrieved before record is saved in a UI Action?

SHUNAKAMINE
Tera Contributor

Hi all,

I have a question about retrieving sys_id before a record is saved.

In a server-side UI Action, I am using code like this:

(function executeAction(current, previous) {
gs.info('UI ACTION START: sys_id=' + current.getUniqueValue());
})();

When this UI Action is executed before the record is saved (for example, on a new record form),
current.getUniqueValue() returns null / undefined.

My question is:
Is it expected behavior that sys_id cannot be retrieved before the record is saved,
even though attachments added before save appear to be stored in sys_attachment
with a table_sys_id value?

In other words:
• Does ServiceNow assign a real sys_id only at insert time?
• Are attachments added before saving associated with a temporary or placeholder sys_id?

I would appreciate clarification on how sys_id assignment works internally
and whether there is any supported way to reliably obtain a record's sys_id
before the first save.

2 REPLIES 2

Tanushree Maiti
Giga Patron

Hi @SHUNAKAMINE 

 

1. Yes. When using .insert()  ServiceNow generates the 32-character sys_id at the moment of insertion. Record is  not fully committed until .insert() is executed.

 

2. When a user uploads an attachment to a record producer before submitting, the attachment is created in the sys_attachment table. It is associated with a temporary table_sys_id .

 

3. While you can predict or generate a sys_id earlier using scripting methods like setNewGuid() the record does not exist in the database until the .insert() method is called.

Refer: insert record with given sys_id 

 

Still I will suggest you to go for automatic sys_id assignation. don't go using the script.

 

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti

Nishant8
Tera Sage

Hello @SHUNAKAMINE, It depends on hows the new form is launched

- if record is just initialize() then sys_id will be Null. In this case sys_id will be generated when record is inserted.

- if newRecord() is used then you can see sys_id available. This is the method used when there is an attachment available. Since sys_id is available, attachement in the sys_attachement will have a reference to this sys_id

 

Regards,

Nishant