Cache ObjectScript Cheat Sheet
Cache ObjectScript Cheat Sheet
Object/SQL Basics
• Call a class method do ##class(package.class).method(arguments)
set variable = ##class(package.class).method(arguments)
• Call an instance method do oref.method(arguments)
set variable = oref.method(arguments)
• Create a new object set oref = ##class(package.class).%New()
• Open an existing object set oref = ##class(package.class).%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(package.class).PropertyIsValid(oref.Property)
• Print the status after an error do $system.Status.DisplayError(status)
• Remove an object set oref = ""
• Delete an existing object set status = ##class(package.class).%DeleteId(id)
• Delete all saved objects do ##class(package.class).%DeleteExtent()
do ##class(package.class).%KillExtent()
• Write a property write oref.property
• Write the id of a saved object write oref.%Id()
• Set a property set oref.property = value
• Set a serial (embedded) property set oref.property.embeddedproperty = value
• Link two objects set oref1.property = oref2
• Populate a class do ##class(package.class).Populate(count, verbose)
• Remove all objects in memory kill
• Clone an object set clonedOref = oref.%ConstructClone()
• List all objects in memory do $system.OBJ.ShowObjects() ; pass “d” for details
• Display all properties of an object do $system.OBJ.Dump(oref)
• Start the SQL shell do $system.SQL.Shell()
• 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 listOref.Insert(value)
do oref.listProperty.Insert(value)
• Insert an element into a list do listOref.SetAt(value,position)
do oref.listProperty.SetAt(value,position)
• Remove an element from a list do listOref.RemoveAt(position)
do oref.listProperty.RemoveAt(position)
• Display an element of a list write listOref.GetAt(position)
write oref.listProperty.GetAt(position)
• Display the size of a list write listOref.Count()
write oref.listProperty.Count()
• Clear all the elements of a list do listOref.Clear()
do oref.listProperty.Clear()
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 arrayOref.SetAt(value,key)
do oref.arrayProperty.SetAt(value,key)
• Remove an element from an array do arrayOref.RemoveAt(key)
do oref.arrayProperty.RemoveAt(key)
• Display an element of an array write arrayOref.GetAt(key)
do oref.arrayProperty.GetAt(key)
• Display the size of an array write arrayOref.Count()
do oref.arrayProperty.Count()
• Clear all elements of an array do arrayOref.Clear()
do oref.arrayProperty.Clear()
Relationships
• Insert a child object into its parent do parentOref.childProperty.Insert(childOref)
• Link a child object to its parent object set childOref.parentProperty = parentOref
• Insert a many object into its one do oneOref.manyProperty.Insert(manyOref)
• Link a many object to its one object set manyOref.OneProperty = OneOref
• Write a property of a child object write parentOref.childProperty.GetAt(position).property
• Write a property of a many object write oneOref.manyProperty.GetAt(position).property
• Display the count of child/many objects write parentOref.childProperty.Count()
write oneOref.manyProperty.Count()
• Clear the child/many objects do parentOref.childProperty.Clear()
do oneOref.manyProperty.Clear()
Streams
• Create a new stream set streamOref=##class(%Stream.GlobalCharacter).%New()
set streamOref=##class(%Stream.GlobalBinary).%New()
or use methods below on a stream property
• Add text to a stream do streamOref.Write(text)
do oref.streamProperty.Write(text)
• Add a line of text to a stream do streamOref.WriteLine(text)
do oref.streamProperty.WriteLine(text)
• Read len characters of text from a stream write streamOref.Read(len)
write oref.streamProperty.Read(len)
• Read a line of text from a stream write streamOref.ReadLine(len)
write oref.streamProperty.ReadLine(len)
• Go to the beginning of a stream do streamOref.Rewind()
do oref.streamProperty.Rewind()
• Go to the end of a stream, for appending do streamOref.MoveToEnd()
do oref.streamProperty.MoveToEnd()
• Clear a stream do streamOref.Clear()
do oref.streamProperty.Clear()
• Display the length of a stream write streamOref.Size
write oref.streamProperty.Size
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 $system.SYS.Horolog()
• Display UTC date/time string write $system.SYS.TimeStamp()
• 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)
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