Manually copying a URL might not be the best way for your script to redirect to a page in NetSuite. Thankfully, NetSuite provides a better and more reliable way to handle URLs.

Getting URLs

Every page in NetSuite has its own URL (“Uniform Resource Locator”). Whenever you load a record in your browser, a new URL is being accessed behind the scenes.

As developers, we sometimes need to direct a user to a particular record URL. For example, if we create a field with a direct link to a record, we’ll need our script to keep track of what that record URL is. There are really two ways to handle this situation, with one of them being notably superior than the other in our estimation.

The Common Way

Of course, it would seem common sense to simply copy the URL from the address bar at the top of the browser. It is even possible to replace your instance’s URL prefix with “system” to make that URL generically accessible between Sandbox and Production accounts (e.g., system.netsuite.com/app/center/card.nl).

But hard-coding a particular value in a script isn’t the most ideal path. If something ever changes with that URL, that script’s functionality will no longer work. It’s often not possible to future-proof our scripts entirely, but we should take up a better option if it is available.

The Better Way

NetSuite, foreseeing the risk behind hard-coding URLs, decided to create a module to “address” this. Using this method, we are able to avoid the risk of hard-coded values and retrieve the URL dynamically instead.

The URL Module

One of NetSuite’s modules—the URL module—allows us to do this with the resolveRecord method.

define(['N/url'], function (url) {
    // The rest of your script goes in here...
});

After importing the module and creating our basic file structure, we are ready to resolve a record to a URL.

const salesOrderUrl = url.resolveRecord({
    recordType: 'salesorder',
    recordId: 106,
    isEditMode: true
});

There are three required parameters for the resolveRecord method.

  1. recordType: The type of the record. You may also use NetSuite’s record.Type enum if desired (e.g., “record.Type.SALES_ORDER”).
  2. recordId: The internal ID of the record. You can always get the internal ID by locating it in the address bar.
  1. isEditMode: Set this value to a boolean to choose whether the record is in view or edit mode when the user is directed to the page.

There is also an option to pass parameters into the URL. This is a more advanced tool that can be used to create powerful automations. We’ll likely cover this concept in a future tutorial.

Conclusion

URLs are a crucial component behind NetSuite. When we are able to retrieve record URLs dynamically, we are insulating our scripts with a layer of future-proofing protection. Were you aware of this SuiteScript method? Let us know in the comment section if you find this helpful! We also welcome you to subscribe to our mailing list to receive our latest SuiteScript tutorials in your inbox!

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