1- # Cheat sheet
2- Some most encountered COS expressions
1+ # Caché ObjectScript Quick Reference
2+ A list of some common ObjectScript expressions
33
44## Object/SQL Basics
55
@@ -14,6 +14,7 @@ Some most encountered COS expressions
1414| Save an object | ` Set status = object.%Save() ` |
1515| Retrieve the ID of a saved object | ` Set id = object.%Id() ` |
1616| Retrieve the OID of a saved object | ` Set oid = object.%Oid() ` |
17+ | Determine if an object was modified | ` Set variable = object.%IsModified() ` |
1718| Validate an object without saving | ` Set status = object.%ValidateObject() ` |
1819| Validate a property without saving | ` Set status = ##class(package.class).PropertyIsValid(object.Property) ` |
1920| Print status after error | ` Do $system.Status.DisplayError(status) ` <br >` Zwrite status ` (v2012.2+) |
@@ -25,6 +26,7 @@ Some most encountered COS expressions
2526| Clone an object | ` Set clonedObject = object.%ConstructClone() ` |
2627| Write a property | ` Write object.property ` |
2728| Set a property | ` Set object.property = value ` |
29+ | Write a class parameter | ` Write ##class(package.class).#PARAMETER ` |
2830| Set a serial (embedded) property | ` Set object.property.embeddedProperty = value ` |
2931| Link two objects | ` Set object1.referenceProperty = object2 ` |
3032| Populate a class | ` Do ##class(package.class).Populate(count, verbose) ` |
@@ -35,22 +37,25 @@ Some most encountered COS expressions
3537| Find classname of an object | ` Write $classname(oref) ` |
3638| Start the SQL shell | ` Do $system.SQL.Shell() ` |
3739| Test a class query | ` Do ##class(%ResultSet).RunQuery(class, query) ` |
40+ | Declare a variable's type for Studio Assist | ` #dim object as package.class ` |
3841
3942## ObjectScript Commands
4043
4144| Command | Description |
4245| -------------------------------| -------------------------------------------------------------------------------------|
4346| ` Write ` | Display text strings, value of variable or expression. |
47+ | ` Zwrite ` | Display array, list string, bit string.
4448| ` Set ` | Set value of variable. |
4549| ` Do ` | Execute method, procedure, or routine. |
4650| ` Quit ` or ` Return ` (v2013.1) | Terminate method, procedure, or routine. Optionally return value to calling method. |
51+ | ` Continue ` | Stop current loop iteration, and continue looping. |
4752| ` Halt ` | Stop Caché process and close Terminal. |
4853| ` Kill ` | Destroy variable(s). |
4954| ` If {} ElseIf {} Else {} ` | Evaluate conditions and branch. |
5055| ` For {} ` , ` While {} ` , ` Do {} While ` | Execute block of code repeatedly. |
5156| ` Try {} Catch {} ` , ` Throw ` | Handle errors. |
5257
53- ## ObjectScript Functions
58+ ## ObjectScript Date/Time Functions and Special Variables
5459
5560| Action | Code |
5661| --------------------------------------------------| -------------------------------------------------------------|
@@ -60,13 +65,64 @@ Some most encountered COS expressions
6065| Time conversion (internal → external) | ` Set variable = $zt(internalTime, format) ` |
6166| Display current internal date/time string | ` Write $horolog ` |
6267| Display UTC date/time string | ` Write $ztimestamp ` |
63- | Display length of a string | ` Write $length(string) ` |
68+
69+ ## ObjectScript Branching Functions
70+
71+ | Action | Code |
72+ | --------------------------------------------------| -------------------------------------------------------------|
73+ | Display result for value of expression | ` Write $case(expression, value1:result1, value2:result2, …, :resultN) ` |
74+ | Display result for first true condition | ` Write $select(condition1:result1, condition2:result2, …, 1:resultN) ` |
75+
76+ ## ObjectScript String Functions
77+
78+ | Action | Code |
79+ | --------------------------------------------------| -------------------------------------------------------------|
80+ | Display substring extracted from string | ` Write $extract(string, start, end) ` |
81+ | Display right-justified string within width characters | ` Write $justify(string, width) ` |
82+ | Display length of string | ` Write $length(string) ` |
83+ | Display number of delimited pieces in string | ` Write $length(string, delimiter) ` |
84+ | Display piece from delimited string | ` Write $piece(string, delimiter, pieceNumber) ` |
85+ | Set piece into delimited string | ` Set $piece(string, delimiter, pieceNumber) = piece ` |
86+ | Display string after replacing substring | ` Write $replace(string, subString, replaceString) ` |
87+ | Display reversed string | ` Write $reverse(string) ` |
88+ | Display string after replacing characters | ` Write $translate(string, searchChars, replaceChars) ` |
6489| Build a list | ` Set listString = $listbuild(list items, separated by comma) ` |
6590| Retrieve an item from a list | ` Set variable = $list(listString, position) ` |
91+ | Put item into list string | ` Set $list(listString, position) = substring ` |
6692| Display the length of a list | ` Write $listlength(listString) ` |
93+
94+ ## ObjectScript Existence Functions
95+
96+ | Action | Code |
97+ | --------------------------------------------------| -------------------------------------------------------------|
98+ | Check if variable exists | ` Write $data(variable) ` |
99+ | Return value of variable, or default If undefined | ` Write $get(variable, default) ` |
100+ | Return next valid subscript in array | ` Write $order(array(subscript)) ` |
101+
102+ ## Additional ObjectScript Functions
103+
104+ | Action | Code |
105+ | --------------------------------------------------| -------------------------------------------------------------|
106+ | Increment ^global by increment | ` $increment(^global, increment) ` <br > ` $sequence(^global, increment) ` |
107+ | Match a regular expression | ` Set matches = $match(string, regularexpression) ` |
67108| Display random integer from start to start+count | ` Write $random(count) + start ` |
68- | Check if variable exists | ` Write $data(variable) ` |
69- | Return value of variable, or "" If undefined | ` Write $get(variable) ` |
109+
110+ ## ObjectScript Special Variables
111+
112+ | Action | Code |
113+ | --------------------------------------------------| -------------------------------------------------------------|
114+ | Display process ID | ` Write $job ` |
115+ | Display current namespace | ` Write $namespace ` |
116+ | Change current namespace | ` Set $namespace = newnamespace ` |
117+ | Display username | ` Write $username ` |
118+ | Display roles | ` Write $roles ` |
119+
120+ ## Utilities
121+
122+ | Action | Code |
123+ | --------------------------------------------------| -------------------------------------------------------------|
124+ | Change current namespace | ` Do ^%CD ` <br > ` zn "newnamespace" ` |
125+ | Display a ^global | ` Do ^%G ` <br > ` zwrite ^global ` |
70126
71127## Collections
72128
0 commit comments