Article [3] : Understanding ServiceNow Scripting: Learning Through CAD Questions

Mohammed8
Giga 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 questions. Instead of just getting the answers, I decided to explore them and wanted to share what I learned

 

1) What debugging method you use in the server side scripting in a scoped application?

  • gs.addInfoMessage()
  • gs.debuglog()
  • gs.info
  • gs.log

Answer: 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

gs.log()

⚠️Avoid

Not recommended in scoped app

gs.debuglog()

No

Function does not exist

 

It is easy to assume the answer is gs.debuglog() as it sounds like the right choice. So instead of guessing, let us run each option in Background Scripts inside a scoped app and see what happens.

 

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

 

2) Which of the following methods can be used in a Business Rule?

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

 

Answer: gs.hasRole()

 

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

 

3) Which of the following methods prints a message on a blue background to the top of the current form by default?

  • 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 answers, 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,

Mohammed Zakir

0 REPLIES 0