How to get sys_id of Script in code.

Geoffrey Bishop
Tera Contributor

Hello, ServiceNow Gurus,

Say I have a Business Rule, and that Business Rule calls into a Script Include, and that Script Include writes a message to the System Log...

Is it possible for the script include to figure out, at runtime, its own sys_id?  And include that in the log statement?

I know that I can manually log the sys_id by writing my own log function and passing in the "hard-coded" sys_id, but that's not what I want.  I want for the script itself to magically determine the sys_id of the container it lives in.  Is that possible?  And if so, how please?

Thank you!

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

no you cannot get the sysId of the BR automatically and log it.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

8 REPLIES 8

@Geoffrey Bishop 

Did my reply answer your question?

If my response helped please close the thread by marking appropriate response as correct so that it benefits future readers.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Pavankumar_1
Mega Patron

Hi,

No we can't retrieve the business rule sys_id from script include we don't have such method.

Assumption:

for example we have object to get sysid of business rule but for the same script include we have multiple  business rules which one we will get from that.

 

Hope you got my point.

Please Mark Correct/helpful if applicable, Thanks!! 

 

Regards

Pavankumar

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

I just want the sys_id of the container, the Script Include record itself.

matthewch1
Tera Expert

You can get it by purposely throwing and catching an error, then extracting it from the stack trace.

function getThisScriptIncludeSysID() {
  try {
    undefinedObject.purposelyFail();
  } catch (e) {
    var m = e.stack.match(/sys_script_include\.([0-9a-f]{32})\.script/);
    if (m) return m[1];
  }
}