In this example, we'll create a simple find
method using information about the currently selected AOT node.
When the script methods are called, they are created with an instance of an Editor
class. This class contains, among other information, the tree node of the method we are editing. Like this, for example:
\Data Dictionary\Tables\ConFMSVehServiceTable\Methods\method1
The following code uses this to get the table name. Again, add this to the EditorScripts
class:
public void contoso_method_find(Editor editor) { str path = editor.path(); TreeNode treeNode; TableName tableName; str prefix = strRep(" ", 4); #TreeNodeSysNodeType treeNode = path ? TreeNode::findNode(path) : null; if (treeNode) { treeNode = treeNode.AOTparent(); if(treeNode) treeNode = treeNode.AOTparent(); if (!treeNode) return; if (treeNode.treeNodeType().id() == #NT_DBTABLE) { tableName = treeNode.treeNodeName(); } } if(tableName == "") return; editor.insertLines( strFmt("public static %1 find(%2 _id, boolean _forUpdate = false)\n", tableName, "RefRecId")); editor.insertLines("{\n"); editor.insertLines(prefix + strFmt("%1 table;\n\n", tableName)); editor.insertLines(prefix + "table.selectForUpdate(_forUpdate);\n"); editor.insertLines(prefix + strFmt("select table where table.%2 == _id;\n", tableName, "RecId")); editor.insertLines(prefix + "return table;\n"); editor.insertLines("}"); }
We can extend this further by interrogating the table's metadata, using DictTable
to find the keys in the primary key.
Finally, you might wonder how the scripts work where you just type the script name. We can do this using the following steps:
Rename the method to
template_method_find
.On the
ConFMSVehicleTable
table, create a new method.Delete the content of the method, type
find
, and press Tab. We'll see that it creates afind
method for us.
We have broken our naming convention by dropping the prefix, and doing this can cause some issue when you perform an upgrade. And, in this particular case, it could also be affected by cumulative updates or service packs.