How To Get The Current URL From A Server Side Script

How To Get The Current URL From The Server in ServiceNow Getting the current URL from the server side in ServiceNow can be extremely useful in a variety of contexts, such as when you’re designing …


Buy The "ServiceNow Developer's Manual" Now

We've packed over a decade of ServiceNow Development advice, into a single e-book.

Buy It Now
Stripe Climate Badge

The Snowball will contribute 5% of your purchase to remove CO₂ from the atmosphere.

How To Get The Current URL From The Server in ServiceNow

Getting the current URL from the server side in ServiceNow can be extremely useful in a variety of contexts, such as when you’re designing custom workflows, integrating with external systems, or even for simple debugging purposes. In this article, we’ll explore the importance of obtaining the current URL and delve into multiple methods to do so, particularly focusing on the use of gs.getProperty('glide.servlet.uri').

Why is it Important to Get the Current URL?

  1. Debugging: When trying to troubleshoot a specific page or flow, it helps to know which URL the server is currently processing. This can be especially helpful in environments with many custom modules or integrations.
  2. Context-aware processing: Sometimes, a script or business rule might need to alter its behavior based on which page or module is being accessed. Knowing the current URL can provide this context.
  3. Redirections and Navigations: If you’re building custom navigation flows, you might need the current URL to redirect users back to their original page after they complete an action elsewhere.
  4. Integrations: When integrating with external systems, it’s often necessary to send the current ServiceNow page’s URL for reference or callbacks.

Methods to Get the Current URL

1. Using gs.getProperty('glide.servlet.uri')

This method is one of the most straightforward ways to obtain the current URL from the server-side in ServiceNow.

var currentURL = gs.getProperty('glide.servlet.uri');
gs.info(currentURL);

However, do note that gs.getProperty('glide.servlet.uri') will give you the base URL of your ServiceNow instance. If you’re looking to get the complete URL, including any parameters or additional paths, you’ll need to combine it with other methods or properties.

2. Using current.getLink()

For those working within the context of a business rule or a script include associated with a particular record, the getLink() method provides an easy way to get the URL of the current record.

var recordLink = current.getLink();
gs.info(recordLink);

This method is particularly handy when you want to provide direct links to specific records in email notifications or external integrations.

Conclusion

Understanding the context of a server-side action or workflow is crucial in many ServiceNow customizations and integrations, and the current URL is often a big part of that context. Whether you’re debugging, building custom navigations, or integrating with other platforms, the methods outlined above should equip you to fetch the current URL efficiently.

Remember to choose the method that fits your specific context and always test your scripts in a sub-production instance before deploying them to production. Happy coding!



What Do You Think About This Article?

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Product Image

Carolyn just purchased A ServiceNow Developer’s Manual 10 Hours ago from Washington DC.

Buy Now
0
Would love your thoughts, please comment.x
()
x