SBM ModScript Reference → SBM ModScript Samples → Sample One: Read/Write Fields
/* ModScript Example: ReadWriteFields.tscm --------------------------------------- This script reads from a field and writes its value to another field. Requirements: This script relies on Shell.Item, which only exists in pre-transition, post-transition, pre-state, and post-state contexts.*/ // MODIFY THESE CONSTANTS FOR YOUR DATABASE // ---------------------------------------- // Names of the fields we will work with add_global_const( "title", "FLDNAME_SRC" ); // add a global constant called "FLDNAME_SRC" with the value "title". →The keyword "global" could also be used, but then the value is not constant add_global_const( "description", "FLDNAME_DEST" ); // add a global constant called "FLDNAME_DEST" with the value "description". // Read from one field, write back to another field def ReadWriteFields() { var fldValue = Variant(); // since fields can have lots of different data types, →we can only get their value as a Variant. When passing a var as an output-parameter →to a function that takes a Variant, we need to ensure we create a Variant to pass in. // Read from the source field if ( !( Shell.Item().GetFieldValue( FLDNAME_SRC, fldValue ) )) { // Error finding the field or reading from it Ext.LogErrorMsg( "Cannot read from field \"" + FLDNAME_SRC + "\"" ); // ModScript can concat values with + or &&&, but + will add if both the left and right →values can be cast to numbers, where-as &&& will always concatenate return; } // Write to the destination field if ( !( Shell.Item().SetFieldValue( FLDNAME_DEST, fldValue ) )) { // Error finding the field or writing to it Ext.LogErrorMsg( "Cannot write to field \"" &&& FLDNAME_DEST &&& "\"" ); // ModScript can use backslash to escape double quotes return; } } // Find the item being transitioned if ( Ext.ShellHasProp( "Item" ) ) { ReadWriteFields(); } else { // There is no current item, so write a message to the event viewer Ext.LogErrorMsg( "ModScript error: Shell.Item does not exist." ); }
Copyright © 2007–2017 Serena Software, Inc. All rights reserved.