Content Type
Job Aid
Duration
varies
Level
Introductory
Use this handy reference to find the syntax of some common InterSystems ObjectScript commands, functions, variables, and expressions.
You can also download a PDF version of this page.
Content Type
Job Aid
Duration
varies
Level
Introductory
Use this handy reference to find the syntax of some common InterSystems ObjectScript commands, functions, variables, and expressions.
You can also download a PDF version of this page.
Note: In the examples below, words in italics are placeholders for actual values or variables.
| Action | Code |
|---|---|
| Call a class method. Note: Place a period before each pass-by-reference argument. |
Begin code: do ##class(package.class).method(arguments) set variable = ##class(package.class).method(arguments) End code. |
| Call an instance method. Note: Place a period before each pass-by-reference argument. |
Begin code: do object.method(arguments) set variable = object.method(arguments) End code. |
| Write or set a property. | Begin code: write object.property set object.property = value End code.
|
| Set a serial (embedded) property. | Begin code: set object.property.embeddedProperty = value End code.
|
| Retrieve a stored property value of an object directly. | Begin code: set variable = ##class(package.class).PropertyGetStored(id) End code.
|
| Determine if a property value exists in index. | Begin code: set exists = ##class(package.class).IndexNameExists(value, .id) End code. |
| Write a class parameter. | Begin code: write ..#PARAMETER write ##class(package.class).#PARAMETER End code.
|
Note: In the examples below, words in italics are placeholders for actual values or variables.
| Action | Code |
|---|---|
| Create a new object. | Begin code: set object = ##class(package.class).%New() End code.
|
| Open an existing object by ID. | Begin code: set object = ##class(package.class).%OpenId(id, concurrency, .status) End code.
|
| Open an existing object by unique index value. | Begin code: set object = ##class(package.class).IndexNameOpen(value, concurrency, .status) End code.
|
| Close an object (remove from process). | set object = "" |
| Save an object. | Begin code: set status = object.%Save() End code.
|
| Retrieve the ID of a saved object. | set id = object.%Id() |
| Reload stored properties of object. | do object.%Reload() |
| Display all properties of an object. | Begin code: do $system.OBJ.Dump(object) zwrite object End code. |
| List all objects in process. | do $system.OBJ.ShowObjects() |
| Determine if an object was modified in memory. Note: returns 1 (true), 0 (false) |
if object.%IsModified() |
| Link two objects. | Begin code: set object1.referenceProperty = object2 End code.
|
| Delete an existing object by ID. | Begin code: set status = ##class(package.class).%DeleteId(id) End code.
|
| Delete an existing object by unique index value. | Begin code: set status = ##class(package.class).IndexNameDelete(value) End code. |
| Delete all saved objects of a class. | Begin code: do ##class(package.class).%DeleteExtent() do ##class(package.class).%KillExtent() End code. |
| Populate a class with new objects. | Begin code: do ##class(package.class).Populate(count, verbose) End code. |
| Clone an object. | Begin code: set clonedObject = object.%ConstructClone() End code. |
| Determine if variable is an object reference. Note: returns 1 (true), 0 (false). |
Begin code: if $isobject(variable) End code. |
| Find classname of an object. | set variable = $classname(oref) |
| Retrieve the OID of a saved object. | set oid = object.%Oid() |
Note: In the examples below, words in italics are placeholders for actual values or variables.
| Action | Code |
|---|---|
| Validate an object without saving. | set status = object.%ValidateObject() |
| Validate a property without saving. | Begin code: set status = ##class(package.class).PropertyIsValid(object.Property) End code. |
| Print status after error. | Begin code: do $system.Status.DisplayError(status) write $system.Status.GetErrorText(status) End code. |
| Convert status into exception. | Begin code: set ex = ##class(%Exception.StatusException).CreateFromStatus(status) End code. |
Note: In the examples below, words in italics are placeholders for actual values or variables.
| Action | Code |
|---|---|
| Declare a variable's type for IDE code completion. | #dim object as package.class |
| Start the SQL shell. | do $system.SQL.Shell() |
| Check SQL privileges. Note: returns 1 (true), 0 (false) |
if $system.SQL.Security.CheckPrivilege() |
| Command | Purpose |
|---|---|
Continue |
Stop current loop iteration, continue looping. |
Do |
Execute method, procedure, or routine. |
For {}, While {}, Do {} While |
Execute block of code repeatedly. |
Halt |
Stop process and close the Terminal. |
If {} ElseIf {} Else {} |
Evaluate conditions and branch. |
Kill |
Destroy variable(s). Remove all objects in process. |
Quit, Return |
Terminate method, procedure, or routine. Optionally return value to calling method. Terminate loops. |
Set |
Set value of variable. |
Try {} Catch {}, Throw |
Handle errors. |
Write |
Display text strings, value of variable or expression. |
ZWrite |
Display array, list, bit string, JSON object/array (v2019.1+). |
Note: In the examples below, words in italics are placeholders for actual values or variables.
| Action | Command |
|---|---|
| Date conversion (internal to external) | $zdate(internalDate, format) |
| Date conversion (external to internal) | $zdateh("mm/dd/yyyy") |
| Time conversion (internal to external) | $ztime(internalTime, format) |
| Time conversion (external to internal) | $ztimeh("hh:mm:ss") |
| Current local date/time string | $horolog |
| Current UTC date/time string | $ztimestamp |
Note: In the examples below, words in italics are placeholders for actual values or variables.
| Action | Command |
|---|---|
| Return result for value of expression. | $case(expression, value1:result1, value2:result2, …, :result) |
| Return result for first true condition. | $select(condition1:result1, condition2:result2, …, 1:resultN) |
Note: In the examples below, words in italics are placeholders for actual values or variables.
| Action | Command |
|---|---|
| Extract characters from string. | $extract(string, start, end) |
| Right-justify string within width characters. | $justify(string, width) |
| Retrieve length of string. | $length(string) |
| Replace/remove substring in string. | $replace(string, searchString, replaceString) |
| Replace/remove characters in string. | $replace(string, searchChars, replaceChars) |
| Reverse a string. | $reverse(string) |
Note: In the examples below, words in italics are placeholders for actual values or variables.
| Action | Command |
|---|---|
| Build a list. | set list = $listbuild(substrings separated by comma) |
| Retrieve substring from list. | $list(list, position) |
| Put substring into list. | set $list(list, position) = substring |
| Retrieve number of substrings in list. | $listlength(list) |
| Convert a delimited string into a list. | set list = $listFromString(string, delimiter) |
| Convert a list into a delimited string. | set string = $listToString(list, delimiter) |
| Retrieve piece from delimited string. | $piece(string, delimiter, pieceNumber) |
| Set piece into delimited string. | set $piece(string, delimiter, pieceNumber) = piece |
| Retrieve number of delimited pieces in string. | $length(string, delimiter) |
Note: In the examples below, words in italics are placeholders for actual values or variables.
| Action | Command |
|---|---|
| Determine if variable exists. | $data(variable) |
| Return value of existing variable, or default. | $get(variable, default) |
| Return next valid subscript in sparse array. | $order(array(subscript)) |
Note: In the examples below, words in italics are placeholders for actual values or variables.
| Action | Command |
|---|---|
| Increment ^global by 1 (or increment). | $increment(^global, increment) $sequence(^global) |
| Match a regular expression. | $match(string, regularexpression) |
| Generate a random integer. Note: start through (start+count-1) |
$random(count) + start |
| Action | Command |
|---|---|
| Process ID | $job |
| Current namespace | $namespace |
| Security roles | $roles |
| Security username | $username |
Note: In the examples below, words in italics are placeholders for actual values.
| Action | Command |
|---|---|
| Change namespace. | Begin code: set $namespace = "namespace" do ^%CD znspace "namespace" End code. |
| Display a global. | Begin code: do ^%G zwrite global End code.
|
Note: In the examples below, words in italics are placeholders for actual values or variables.
| Action | Command |
|---|---|
| Create a new standalone list. | set listObject=##class(%ListOfDataTypes).%New() |
| Work with a list property. | Use methods below on a list collection property. |
| Insert an element at the end of a list. | Begin code: do listObject.Insert(value) do object.listProperty.Insert(value) End code. |
| Insert an element into a list. | Begin code: do listObject.SetAt(value, position) do object.listProperty.SetAt(value, position) End code. |
| Remove an element from a list. | Begin code: do listObject.RemoveAt(position) do object.listProperty.RemoveAt(position) End code. |
| Retrieve an element of a list. | Begin code: set variable = listObject.GetAt(position) set variable = object.listProperty.GetAt(position) End code. |
| Retrieve the size of a list. | Begin code: set variable = listObject.Count() set variable = object.listProperty.Count() End code. |
| Clear all the elements of a list. | Begin code: do listObject.Clear() do object.listProperty.Clear() End code. |
Note: In the examples below, words in italics are placeholders for actual values or variables.
| Action | Command |
|---|---|
| Create a new standalone array. | set arrayObject=##class(%ArrayOfDataTypes).%New() |
| Work with an array property. | Use methods below on an array collection property. |
| Insert an element into an array. | Begin code: do arrayObject.SetAt(value, key) do object.arrayProperty.SetAt(value, key) End code. |
| Remove an element from an array. | Begin code: do arrayObject.RemoveAt(key) do object.arrayProperty.RemoveAt(key) End code. |
| Retrieve an element of an array. | Begin code: set variable = arrayObject.GetAt(key) set variable = object.arrayProperty.GetAt(key) End code. |
| Retrieve next key and its element. Note: place a period before the pass-by-reference key argument. |
Begin code: set variable = arrayObject.GetNext(.key) set variable = object.arrayProperty.GetNext(.key) End code. |
| Retrieve the size of an array. | Begin code: set variable = arrayObject.Count() set variable = object.arrayProperty.Count() End code. |
| Clear all elements of an array. | Begin code: do arrayObject.Clear() do object.arrayProperty.Clear() End code.
|
Note: In the examples below, words in italics are placeholders for actual values or variables.
| Action | Command |
|---|---|
| Parent-to-children object linking | Begin code: do parentObject.childRefProperty.Insert(childObject) set childObject.parentRefProperty = parentObject End code. |
| One-to-many object linking | Begin code: do oneObject.manyRefProperty.Insert(manyObject) set manyObject.oneRefProperty = oneObject End code. |
| Retrieve a property of a child object. | Begin code: set variable = parentObject.childRefProperty.GetAt(position).property End code.
|
| Retrieve a property of a many object. | Begin code: set variable = oneObject.manyRefProperty.GetAt(position).property End code.
|
| Retrieve next key. | Begin code: set key = parentObject.childRefProperty.Next(key) set key = oneObject.manyRefProperty.Next(key) End code.
|
| Retrieve the count of child/many objects. | Begin code: set variable = parentObject.childRefProperty.Count() set variable = oneObject.manyRefProperty.Count() End code.
|
| Open a many/child object directly. | Begin code: set object = ##class(package.class).IDKEYOpen(parentID, childsub End code.
|
| Retrieve the id of a child object. | Begin code: set status = ##class(package.class).IDKEYExists(parentID, childsub, .childID) End code.
|
| Clear the child/many objects. | Begin code: do parentObject.childRefProperty.Clear() do oneObject.manyRefProperty.Clear() End code.
|
Note: In the examples below, words in italics are placeholders for actual values or variables.
| Action | Command |
|---|---|
| Create a new stream. | Begin code: set streamObject=##class(%Stream.GlobalCharacter).%New() set streamObject=##class(%Stream.GlobalBinary).%New() End code. (or use methods below on a stream property) |
| Add text to a stream. | Begin code: do streamObject.Write(text) do object.streamProperty.Write(text) End code.
|
| Add a line of text to a stream. | Begin code: do streamObject.WriteLine(text) do object.streamProperty.WriteLine(text) End code.
|
| Read len characters of text from a stream. | Begin code: write streamObject.Read(len) write object.streamProperty.Read(len) End code.
|
| Read a line of text from a stream. | Begin code: write streamObject.ReadLine(len) write object.streamProperty.ReadLine(len) End code.
|
| Go to the beginning of a stream. | Begin code: do streamObject.Rewind() do object.streamProperty.Rewind() End code.
|
| Go to the end of a stream, for appending. | Begin code: do streamObject.MoveToEnd() do object.streamProperty.MoveToEnd() End code.
|
| Clear a stream. | Begin code: do streamObject.Clear() do object.streamProperty.Clear() End code.
|
| Display the length of a stream. | Begin code: write streamObject.Size write object.streamProperty.Size End code.
|
Note: In the examples below, words in italics are placeholders for actual values or variables.
| Action | Command |
|---|---|
| Assert equality. | do $$$AssertEquals(value1, value2, message) |
| Assert inequality. | do $$$AssertNotEquals(value1, value2, message) |
| Assert status is OK. | do $$$AssertStatusOK(status, message) |
| Assert status is not OK. | do $$$AssertStatusNotOK(status, message) |
| Assert condition is true. | do $$$AssertTrue(condition, message) |
| Assert condition is not true. | do $$$AssertNotTrue(condition, message) |
| Log that assertion was skipped. | do $$$AssertSkipped(message) |
| Log message. | do $$$LogMessage(message)
|
Note: In the examples below, words in italics are placeholders for actual values or variables.
| Action | Command |
|---|---|
| Return a good status. | return $$$OK |
| Return an error status. | return $$$ERROR($$$GeneralError, message) |
| Check if status is good. | if $$$ISOK(status) |
| Check if status is an error. | if $$$ISERR(status) |
| Throw an exception if status is an error. | $$$ThrowOnError(status) |
| Return a null object reference. | return $$$NULLOREF |
| Place a new line within a string. | write string1_$$$NL_string2 |
Continue learning about ObjectScript with the following resources:
Having an issue with the learning site? Want to provide feedback on a course? Contact us by emailing online.training@intersystems.com.
Please include all relevant information, including the page or course you are having trouble with, and any necessary screenshots.