ObjectScript Cheat Sheet
Object/SQL Basics
• Call a class method do ##class([Link]).method(arguments)
set variable = ##class([Link]).method(arguments)
• Call an instance method do [Link](arguments)
set variable = [Link](arguments)
• Create a new object set oref = ##class([Link]).%New()
• Open an existing object set oref = ##class([Link]).%OpenId(id)
• Save an object set status = oref.%Save()
• Validate an object without saving set status = oref.%ValidateObject()
• Validate a property without saving set status =
##class([Link]).PropertyIsValid([Link])
• Print the status after an error do $[Link](status)
• Remove an object set oref = ""
• Delete an existing object set status = ##class([Link]).%DeleteId(id)
• Delete all saved objects do ##class([Link]).%DeleteExtent()
do ##class([Link]).%KillExtent()
• Write a property write [Link]
• Write the id of a saved object write oref.%Id()
• Set a property set [Link] = value
• Set a serial (embedded) property set [Link] = value
• Link two objects set [Link] = oref2
• Populate a class do ##class([Link]).Populate(count, verbose)
• Remove all objects in memory kill
• Clone an object set clonedOref = oref.%ConstructClone()
• List all objects in memory do $[Link]() ; pass “d” for details
• Display all properties of an object do $[Link](oref)
• Start the SQL shell do $[Link]()
• Test a class query do ##class(%ResultSet).RunQuery(class, query)
List Collections
• Create a new standalone list set listOref=##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 do [Link](value)
do [Link](value)
• Insert an element into a list do [Link](value,position)
do [Link](value,position)
• Remove an element from a list do [Link](position)
do [Link](position)
• Display an element of a list write [Link](position)
write [Link](position)
• Display the size of a list write [Link]()
write [Link]()
• Clear all the elements of a list do [Link]()
do [Link]()
ObjectScript Cheat Sheet
Array Collections
• Create a new standalone array set arrayOref=##class(%ArrayOfDataTypes).%New()
• Work with an array property Use methods below on an array collection property
• Insert an element into an array do [Link](value,key)
do [Link](value,key)
• Remove an element from an array do [Link](key)
do [Link](key)
• Display an element of an array write [Link](key)
do [Link](key)
• Display the size of an array write [Link]()
do [Link]()
• Clear all elements of an array do [Link]()
do [Link]()
Relationships
• Insert a child object into its parent do [Link](childOref)
• Link a child object to its parent object set [Link] = parentOref
• Insert a many object into its one do [Link](manyOref)
• Link a many object to its one object set [Link] = OneOref
• Write a property of a child object write [Link](position).property
• Write a property of a many object write [Link](position).property
• Display the count of child/many objects write [Link]()
write [Link]()
• Clear the child/many objects do [Link]()
do [Link]()
Streams
• Create a new stream set streamOref=##class(%[Link]).%New()
set streamOref=##class(%[Link]).%New()
or use methods below on a stream property
• Add text to a stream do [Link](text)
do [Link](text)
• Add a line of text to a stream do [Link](text)
do [Link](text)
• Read len characters of text from a stream write [Link](len)
write [Link](len)
• Read a line of text from a stream write [Link](len)
write [Link](len)
• Go to the beginning of a stream do [Link]()
do [Link]()
• Go to the end of a stream, for appending do [Link]()
do [Link]()
• Clear a stream do [Link]()
do [Link]()
• Display the length of a stream write [Link]
write [Link]
ObjectScript Cheat Sheet
ObjectScript Commands
• Write - write a variable or global reference
• Set – set a variable or global
• Do – execute a function or method (see specific syntax examples throughout)
• Quit – exit a loop or condition
• Halt – exit terminal
• Kill – kill a specific variable by name (e.g. kill myvar) or kill ALL VARIABLES by entering an argumentless kill
• If {} ElseIf {} Else {}
• For {}
• While {}
• Do {} While
ObjectScript Functions
• Date conversion (external → internal) set variable = $zdh(“mm/dd/yyyy”)
• Date conversion (internal → external) set variable = $zd(internalDate, format)
• Time conversion (external → internal) set variable = $zth(“hh:mm:ss”)
• Time conversion (internal → external) set variable = $zt(internalTime, format)
• Display internal date/time string write $horolog
write $[Link]()
• Display UTC date/time string write $[Link]()
• Display length of a string write $length(string)
• Build a list set listString = $listbuild(list items, separated by comma)
• Retrieve an item from a list set variable = $list(listString, position)
• Display the length of a list write $listlength(listString)
• Display random number from start to count write $random(count) + start
• Check if variable exists write $data(variable)
• Return value of variable, or "" if undefined write $get(variable)
Unit Testing Assertion Macros
• Assert equality do $$$AssertEquals(value1, value2, message)
• Assert inequality do $$$AssertNotEquals(value1, value2, message)
• Assert status is OK do $$$AssertStatusOK(status, message)
• Assert status isn't OK do $$$AssertStatusNotOK(status, message)
• Assert condition is true do $$$AssertTrue(condition, message)
• Assert condition isn't true do $$$AssertNotTrue(condition, message)
Other Macros
• Return a good status quit $$$OK
• Return an error status quit $$$ERROR($$$GeneralError, message)
• Check if status is good if $$$ISOK(status)
• Check if status is an error if $$$ISERR(status)
• Return a null object reference quit $$$NULLOREF
• Place a new line within a string write string1_$$$NL_string2
Useful Routines
• Change namespace do ^%CD
• Display a global do ^%G
do ^%r (for Caché globals using $list format storage)
zwrite global