Fiona Griffiths · Jun 27, 2019

Is it possible to save a class in another namespace?


Quick question... Is it possible to save a class in another namespace. If you're saving a global you can just use an extended reference, but is it possible to achieve the same when saving a class object?  Obviously the class will be defined in the other namespaces too.

I want to do it from an Ensemble SOAP Service. I guess you could switch namespaces, but I was a bit worried about doing that from Ensemble process.


0 501
Discussion (7)3
Log in or sign up to continue

Does your suggestion just switch namespace? 

We will be receiving events to a central SOAP service for a number of 'sites' on the same machine in different namespaces. They share a common database, but some globals are not mapped to the central database namespace so as to separate out some settings and they also need to take responsibility for resolving data issues for the patients belonging to their area.  

The SOAP service will upload events automatically for individual patients, but each 'site' only wishes to see events related to their patients and manually resolve 'rejections' for their area.  I think in explaining the case, I have thought of another way to do it. I think I will save the object centrally and just save a namespace specific global which points to ones for their area, then I can also create an index by 'site'.  I think that should do it.  

thanks for your suggestions!

Thanks for sharing this.  I would be interested in seeing some of the data structures you decided on with dummy data.

I would typically use a percent (%) global if I wanted a global to be accessible across multiple namespaces but I don't usually mix percent globals and class storage definitions.

Hi Danny

Thanks, but neither of those would work. There are multiple possible namespaces I might want to save the class to.  Each with their own version of the global (i.e. containing different records)

Yes it does contain a child class too, so that needs to be considered.

Any of the following is possible, but this will only work if you want to keep the data in the two namespaces for that class exactly the same : 

  • Map the globals that are used by the persistent class to the other namespace.


  • Change the global names in the Class Storage definition (DataLocation, IDLocation, IndexLocation, StreamLocation) and add a namespace reference to it, like this :
Storage Default
<Data name="customerDefaultData">

Be careful if properties are pointing to other persistent classes as well !

This is possible:

Class MyClass As %Persistent {

Method SaveToNs(Namespace = {$namespace}) As %Status
  new $namespace = ""
  set $namespace = Namespace
  quit:'##class(%Dictionary.CompiledClass).%ExistsId(..%ClassName(1)) $$$ERROR($$$GeneralError, "Class is undefined in: " _ Namespace)
  set sc = ..%Save()
  quit sc




If you need to save your class in more than 1 namespace, and you are using studio, you may do it automatically with studio hooks.

This is done in that way:
1. You create your own source control class which inherit from %Studio.SourceControlBase
2. you put your code in that class. For example you may use OnAfterSave method to run on all namespaces that your class need to be saved, loop on this list (except the current one of course) and save + compile your class (programmatically) in each namespace.