Declaring a variable in a class that a routine will recognize

Primary tabs

Hello all,

I'm new to the Intersystems development world and I've been trying to figure out how to make a REST API using ObjectScript. I found a tutorial that I've been following that has been great and I'm trying to translate what I've learned to live data.

My question is "Is there a way I can set a variable inside of a class so when I call a routine that variable will be recognized?" I'm capable of setting the variable in a terminal session and then calling the routine and everything will be work great! I will get the data I want returned. I'm just not capable of getting this to translate when I'm calling the routine from a class.

Here is my code: 

 ClassMethod GetSites() As %Status
{
SET %response.ContentType = "application/json"
SET returnObject = {}
SET returnMessage = "Sites have returned successfully"
SET returnObject.message = returnMessage
//
SET = "^"
DO SITES^BGOQRPT(.SITES,"")
SET key=$ORDER(SITES(""),-1)
SET sitesArray = []
FOR i=1:1:key {
SET sitesObject = {}
SET sitesObject.id = i
SET sitesObject.siteNumber = $P(SITES(i),U,1)
SET sitesObject.siteName = $P(SITES(i),U,2)
DO sitesArray.%Push(sitesObject) }
SET returnObject.sites = sitesArray WRITE returnObject.%ToJSON()
QUIT $$$OK
}

Replies

Hi Jordan,

Check out the docs on private/public variables here:

Callable User-defined Code Modules

Variables

You can pass the variable to the subroutine as a parameter or declare it a public variable - my guess it passing it as a parameter would be the "safer" option.

Thank you for the reply it helped me learn more about public and private variable in intersystems. I decided to go with passing it as a parameter as the easiest route.