Question
· Oct 10, 2024

How to Switch Namespaces and Access %SYS Namespace in an OpenAPI-Generated Classmethod?

Hello everyone,

I'm working with a class generated by OpenAPI in InterSystems IRIS, specifically impl.cls. In one of the classmethods of this class, I need to switch to the %SYS namespace to access system-level functionalities and then switch back to the original namespace.

I tried using the following commands:

new $namespace
set $namespace = “%SYS”

However, these give an error when executed. Is there a proper way to change namespaces programmatically within a classmethod and gain access to the %SYS namespace? Any best practices or example code snippets would be greatly appreciated!

Thank you!

Discussion (5)3
Log in or sign up to continue

Yes, you can do that, but I have another recommendation.

You generally want to be careful with what you do in %SYS, and you might not want a user to have permission to access that stuff all the time. You could create a new security role that gives the right permissions to access whatever you're accessing, then assign it in that method, run the code that needs it, and remove it. So let's say your new security role is called MyRole:

set $ROLES = MyRole
set oldns = $NAMESPACE
new $NAMESPACE
set $NAMESPACE = "%SYS"
// Do your stuff here.
new $NAMESPACE
set $NAMESPACE = oldns
new $ROLES

The $ROLES special variable is used to manage roles that are added and removed programatically during the execution of code, but does not affect roles assigned to the user in the management portal.