We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

Article [3] : Understanding ServiceNow Scripting: Learning Through Practice

Mohammed8
Tera Sage

The purpose of learning is not just to get the right answer, but to explore and understand things deeply. While preparing for the CAD certification, I came across a few interesting scripting topics. I decided to explore them and wanted to share what I learned

 

Debugging method used in the server-side scripting in a scoped application

  • gs.error()
  • gs.debug()
  • gs.info()

For Scoped Applications:

 

Method

Works?

Explanation

gs.info()

Yes

Recommended and supported

gs.debug()

Yes

Debug-level logging

gs.error()

Yes

Error logging

 

 

Mohammed8_0-1766673832891.png

 

 

Validation:

 

Mohammed8_1-1766673832892.png

 

Now checking for debug and debuglog option:

 

Mohammed8_2-1766673832896.png 

Validation:

 

Mohammed8_3-1766673832899.png

 

-----------------------------------------------------------------------------------------------------------------------------------

 

How to identify methods that can be used in a Business Rule:

  • gs.hasRole()
  • g_user.hasRole()
  • gs.hasRoleExactly()
  • g_user.hasRoleExactly()

 

Trick: for Business Rule (server side) = gs.  and Client side = g_

 

Method

Works in Business Rule?

Explanation

gs.hasRole('admin')

Yes

Available for server side

gs.hasRoleExactly('admin')

No

Not available on server side

g_user.hasRole('admin')

No

Client-side only (UI).

g_user.hasRoleExactly('admin')

No

Client-side only (UI).

 

Validation:

 

Mohammed8_4-1766673832902.png

 

Mohammed8_6-1766674095743.png

 

----------------------------------------------------------------------------------------------------------------------------------------

 

Which are valid Methods that prints a message?

  • g_form.showFieldMessage()
  • g_form.addInfoMessage()
  • g_form.addInfoMsg()
  • g_form.showFieldMsg()

 

Answer: g_form.addInfoMessage()

 

Method

Valid

Notes

g_form.addInfoMessage()

Yes

✔️Correct answer

g_form.showFieldMessage()

No

throws  “not a function”

g_form.addInfoMsg()

No

throws  “not a function”

g_form.showFieldMsg()

Yes

Inline field messages

 

The question is straight forward, but check the syntax difference for Field and Info Message, there is subtle change which can cause confusion if question is reversed asked about field message.

 

----------------------------------------------------------------------------------------------------------------------------------------------

 

Important ServiceNow document links:

https://www.servicenow.com/docs/bundle/zurich-api-reference/page/app-store/dev_portal/API_reference/...

https://www.servicenow.com/docs/bundle/zurich-api-reference/page/script/general-scripting/reference/...


If you found this article helpful, please give it a thumbs-up. If you notice any discrepancy in the article, feel free to correct it, I am always happy to improve. Also, if you have faced tricky ServiceNow scripting questions in the CAD exam you can share it here so others can benefit too.

 

Thanks and Regards,

Mohamed Zakir

1 REPLY 1

Mohammed8
Tera Sage

Just cleared CAD certification (30-12-2025), More Important topics discussed below

 

Which a Business Rule returns the sys_id of the currently logged in user. (confusing option, easily identified with thorough practice)

  •  g_form.getUserID()
  •  gs.getUserSysID()
  •  gs.getUserID()
  •  g_form.getUserSysID()

 

Method

Valid API?

Notes

gs.getUserID()

Correct answer

gs.getUserSysID()

Not a real GlideSystem function/API            

g_form.getUserID()

Works only in Client Side

g_form.getUserSysID()

Not a real function/API

 

Validation: Answer: gs.getUserID()

 

Mohammed8_0-1767082035853.png

 

-----------------------------------------------------------------------------------------------------------------------------------------------

 

Following methods are useful in Access Control scripts? (Important topic from E-book)

  •  g_user.hasRole() and current.isNewRecord()
  •  gs.hasRole() and current.isNewRecord()
  •  g_user.hasRole() and current.isNew()
  •  gs.getUserRole() and current.isNew()

 

Answer: gs.hasRole() and current.isNewRecord()

 

Reason: Explained in CAD E-book, for Yokohama version of book you can find -> Module 5: Controlling Access. Topic: Access Control Scripting