Learning to love the Formula Language Try it

  • Slides: 31
Download presentation
Learning to love the Formula Language Try it, you’ll like it! Steve Kern, CLP

Learning to love the Formula Language Try it, you’ll like it! Steve Kern, CLP

Topics • • • Formula Language Basics Comparisons to Lotus. Script ND 6 Advances

Topics • • • Formula Language Basics Comparisons to Lotus. Script ND 6 Advances Typical Uses of the Formula Language List Processing Agent construction using the Formula Language

Formula Language Basics • Definition of a formula: – A collection of statements that

Formula Language Basics • Definition of a formula: – A collection of statements that take an action or evaluate to a result – Formulas can contain constants, fields, keywords, operators, @Functions, @Commands and variables • It’s really just one formula that executes as a single line of code (that’s why it’s fast!) – Regardless of length or complexity – Regardless of what appear to be multiple lines • Three primary constructs: – @Functions, @Commands, Keywords

Basics: @Functions • Basic Syntax: @Function(arguments) • Most return a value and many have

Basics: @Functions • Basic Syntax: @Function(arguments) • Most return a value and many have side effects – Side effects include retrieving a list of values, and launching a prompt box or a dialog box • Numerous types of @Functions, including: – – – Arithmetic, String Document information Logical, Branching and Iterative List processing …

Basics: @Commands • Basic Syntax: @Command([keyword]; arguments) • Work only in the User Interface

Basics: @Commands • Basic Syntax: @Command([keyword]; arguments) • Work only in the User Interface • Do not return a value • Provide access to menu commands, such as File, Import • Many do not work with a Web browser

Basics: Keywords • There are five keywords – DEFAULT and FIELD set field values

Basics: Keywords • There are five keywords – DEFAULT and FIELD set field values – ENVIRONMENT reads and writes values to the Notes preferences file (notes. ini) – REM adds comments for Formulas – SELECT selects documents • Except for FIELD, keywords must be listed first on a line

Basics: Operators • • • Arithmetic: +, -, *, / Assignment: : = Comparisons:

Basics: Operators • • • Arithmetic: +, -, *, / Assignment: : = Comparisons: =, >, <, != Logical: &, !, | List concatenation and subscripting: : , [#] Unary: +, -

Basics: Syntax Rules • • • Semicolons must separate each statement Order of operations

Basics: Syntax Rules • • • Semicolons must separate each statement Order of operations can be set using parentheses Very few formatting rules Spaces are required after keywords Formulas are not case sensitive, BUT by convention: – All caps are used for keywords (FIELD, DEFAULT) – Mixed case for @Functions and @Commands

Basics: Limitations • • Scope is limited to the current formula Complex logic can

Basics: Limitations • • Scope is limited to the current formula Complex logic can be difficult to code Cannot pass parameters or call with parameters One formula cannot call another No ability to repeatedly loop through documents No subroutines Some events don’t accept Formulas No debugger

Comparisons to Lotus. Script • In general, the Formula Language executes more quickly than

Comparisons to Lotus. Script • In general, the Formula Language executes more quickly than Lotus. Script • Coding with the Formula Language is usually much simpler and requires less code • Three examples: – Setting field values – Handling of Notes Names – Retrieving external data

Comparison: Setting Field Values • Formula Language @Set. Field(my. Field; “Some Value”) • Lotus.

Comparison: Setting Field Values • Formula Language @Set. Field(my. Field; “Some Value”) • Lotus. Script Sub Initialize ' No comments to save space Dim ws As New Notes. UIWorkspace Dim db As Notes. Database Dim doc. UI As Notes. UIDocument Dim doc As Notes. Document Set doc. UI =ws. Current. Document Set doc = doc. UI. Document doc. my. Field = "Some Value" Call doc. Save(True, True) End Sub

Comparison: Handling Notes Names • Formula Language • Lotus. Script @Prompt([OK]; "The Common Name

Comparison: Handling Notes Names • Formula Language • Lotus. Script @Prompt([OK]; "The Common Name is. . . "; @Name([CN]; @User. Name)) %INCLUDE "LSCONST. LSS“ Sub Initialize ' No comments to save space Dim sn As New Notes. Session Dim nm. User As Notes. Name Dim jc. Common As String Dim jn. Box. Style As Long Dim jn. Answer As Integer Set nm. User = sn. Create. Name(sn. User. Name) jc. Common = nm. User. Common jn. Box. Style = MB_OK + MB_ICONINFORMATION jn. Answer = Messagebox(jc. Common, jn. Box. Style, "The Common Name is. . . ") End Sub

Comparison: Retrieving External Data with the Formula Language REM "Use fields in Profile doc";

Comparison: Retrieving External Data with the Formula Language REM "Use fields in Profile doc"; jc. ODBCSource : = @Get. Profile. Field("GPF"; "c. ODBCSource"); jc. ODBCSource. ID : = @Get. Profile. Field("GPF"; "c. ODBCSource. ID"); jp. ODBCPassword : = @Get. Profile. Field("GPF"; "p. ODBCPassword"); jc. ODBCTable : = @Get. Profile. Field("GPF"; "c. ODBCTable"); jc. Key : = "Key"; REM "Here’s the lookup itself: "; jc. Value : = @Db. Lookup( "ODBC" : "No. Cache" ; jc. ODBCSource. ID ; jp. ODBCPassword ; jc. ODBCTable ; "Some. Fld" : "null" ; "Key. Fld" ; jc. Key );

Comparison: Retrieving External Data with Lotus. Script Option Public Option Declare Uselsx "*LSXODBC" Sub

Comparison: Retrieving External Data with Lotus. Script Option Public Option Declare Uselsx "*LSXODBC" Sub Initialize ' Not functional - example only ' Limited comments Dim session As New Notes. Session Dim db As Notes. Database Dim doc. GPF As Notes. Document Dim con. SQL As New ODBCConnection Dim qry. SQL As New ODBCQuery Dim rslt. SQL As New ODBCResult. Set Dim jc. ODBCSource As String Dim jc. ODBCSource. ID As String Dim jcp. ODBCPassword As String Dim jc. ODBCTable As String Dim jc. Query As String Set db = session. Current. Database Set doc. GPF = db. Get. Profile. Document("GPF") jc. ODBCSource = doc. GPF. c. ODBCSource(0) jc. ODBCSource. ID = doc. GPF. c. ODBCSource. ID(0) jcp. ODBCPassword = doc. GPF. p. ODBCPassword(0) jc. ODBCTable = doc. GPF. c. ODBCTable(0) jc. Query = "SELECT my. SQL. Some. Fld where keyfield=key" ' Create the ODBC Connection If Not con. SQL. Connect. To( jc. ODBCSource, jc. ODBCSource. ID, jcp. ODBCPassword) Then Messagebox "Could not connect to source" Exit Sub End If Set qry. SQL. Connection = con. SQL Set rslt. SQL. Query = qry. SQL = jc. Query rslt. SQL. Execute If rslt. SQL. Is. Result. Set. Available Then ' Do some processing stuff End If ' Clean up - close the connection If con. SQL. Is. Connected Then con. SQL. Disconnect End Sub

ND 6 Advances • • Autocomplete functionality Order of execution Iterative functionality (well, sort

ND 6 Advances • • Autocomplete functionality Order of execution Iterative functionality (well, sort of) Nested Assignments and Reassignment of variables • As always, new @Functions and @Commands

ND 6: Autocomplete • A VERY welcome addition! – Works for the Formula Language,

ND 6: Autocomplete • A VERY welcome addition! – Works for the Formula Language, and – For Scripting languages • As you begin typing, a window moves through matching commands; <enter> selects • Autocomplete also displays syntax – particularly useful for complex @Functions

ND 6: Order of Execution • Certain @Commands always execute last, especially in prior

ND 6: Order of Execution • Certain @Commands always execute last, especially in prior releases • Some examples of new @Commands – Deleting documents: • ND 6: @Command([Clear]) executes in place • @Command([Edit. Clear]) executes last – Running Agents? • ND 6: @Command([Run. Agent]) executes in place • @Command([Tools. Run. Macro]) executes last

ND 6: Iterative Statements • ND 6 added three new iterative @Functions: – @Do.

ND 6: Iterative Statements • ND 6 added three new iterative @Functions: – @Do. While – @For • @Do. While and @While execute a block of statements while a condition you set is true • @For executes a block of statements and increments a counter against an exit condition

ND 6: Assignments • The FIELD keyword can now be nested – No longer

ND 6: Assignments • The FIELD keyword can now be nested – No longer has to be the first item in a statement FIELD my. CN : = @Name([CN]; (FIELD my. NN : = @User. Name)) @Prompt([Ok]; "The Common Name is. . . "; my. CN); • Variables can be reassigned jc. My. Name : = @User. Name; jc. My. Name : = @Name([CN]; jc. My. Name); @Prompt([Ok]; "The Common Name is. . . "; jc. My. Name)

Typical Uses of the Formula Language • Agents • Field Events – Default Value

Typical Uses of the Formula Language • Agents • Field Events – Default Value – Input Translation – Input Validation • Getting user input – @Prompt() – @Dialog. Box() • Keyword Fields – @Db. Column() – @Db. Lookup() • List Processing • View Selection Conditions • View Column Formulas

List Processing • Lists are collections of like data items • Lists are similar

List Processing • Lists are collections of like data items • Lists are similar to arrays • Lists can contain numbers, text, time-dates, Notes names, etc. • Unlike a list, arrays can contain only scalar values – no time-date values or Notes names

List Processing: Some Useful @Commands • • @Contains() @Db. Column() @Db. Lookup() @Elements() @Explode()

List Processing: Some Useful @Commands • • @Contains() @Db. Column() @Db. Lookup() @Elements() @Explode() @Implode() @Is. Member() @Is. Not. Member() • • @Member() @Replace. Sub. String() @Subset() @Sum() @Transform() @Trim() @Unique()

List Processing: Working with Lists • • Retrieve specific elements – Retrieves the last

List Processing: Working with Lists • • Retrieve specific elements – Retrieves the last element in the list @Subset(jc. List; -1) Replace specific elements – Replaces “Second” with “ 2” jc. List : = "First" : "Second" : "Third" : "Last"; jc. List : = @Replace(jc. List; "Second"; "2") Combine (concatenate) lists – This code prepends “First” to the list jc. List : = "Second" : "Third" : "Last"; jc. List 2 : = "First" : jc. List Perform math – Sums the total sales in the list jn. Total. Sales : = @Sum(jn. List)

Using the Formula Language to Coding an Agent • Objectives of this exercise: –

Using the Formula Language to Coding an Agent • Objectives of this exercise: – Build a simple agent to automate workflow for an approval form – Send mail to the approver with a link to the document to be approved – Send daily reminders – Log actions of the agent on each document

Agent Coding: Fields of Interest • c. Doc. Status: The status of the document,

Agent Coding: Fields of Interest • c. Doc. Status: The status of the document, i. e. , New, Approved, Denied • nm. Approver: The Notes name of the approver • c. Doc. History: List of actions on the document • c. Agent. History: List of agents run • d. Agent. History: Dates of agents • n. Reminders: The number of notifications

Agent Coding: How it Works • The Agent runs on the server • Schedule:

Agent Coding: How it Works • The Agent runs on the server • Schedule: daily, and not on weekends • The Agent runs on all approval documents with a status of “New” • It sends an email with a doclink to the approver and increments the n. Reminder counter

Agent Coding: Chunk #1 REM {Send notification to the Approver}; SELECT Form = "FRQ"

Agent Coding: Chunk #1 REM {Send notification to the Approver}; SELECT Form = "FRQ" & c. Doc. Status = "New"; REM {Set reminder counter - used for escalations}; jn. Reminders : = @If(@Is. Available(n. Reminders); n. Reminders + 1; 1) ; @Set. Field("n. Reminders"; jn. Reminders) ; REM {Add document history}; jc. Doc. History : = "Notification #" + @Trim(@Text(jn. Reminders)) + " sent to " + @Name([Abbreviate]; nm. Approver) + " on " + @Text(@Now; "S 2") ; @Set. Field("c. Doc. History"; c. Doc. History : jc. Doc. History) ;

Agent Coding: Chunk #2 REM {Add agent history} ; REM {First, make sure the

Agent Coding: Chunk #2 REM {Add agent history} ; REM {First, make sure the fields exist}; FIELD d. Agent. History : = d. Agent. History ; FIELD c. Last. Agent : = c. Last. Agent ; @Set. Field("d. Agent. History"; @If(d. Agent. History = ""; @Now; d. Agent. History : @Now)); jc. Last. Agent : = "Send Notifications"; @Set. Field("c. Agent. History"; @If(c. Agent. History = ""; jc. Last. Agent; c. Agent. History : jc. Last. Agent));

Agent Coding: Chunk #3 REM {Send the notification}; jc. Send. To : =@Name([Abbreviate]; nm.

Agent Coding: Chunk #3 REM {Send the notification}; jc. Send. To : =@Name([Abbreviate]; nm. Approver); jc. Send. To : = "Stevekern@columbus. rr. com"; jc. CC : = "" ; jc. Subject : = "Requisition for " + @Name([Abbreviate]; nm. Requester) ; jc. Body : = "Please review the Requisition for " + @Name([Abbreviate]; nm. Requester) + ". Click the doclink at the bottom of this message!" ; @Mail. Send(jc. Send. To; jc. CC; jc. BCC; jc. Subject; jc. Body; ""; [Include. Doclink])

Wrapping it up • In this short time, we have: – Covered the basics

Wrapping it up • In this short time, we have: – Covered the basics – Compared the Formula Language to Lotus. Script – Learned about new features in ND 6 – Learned about List Processing – Wrote an Agent using the Formula Language

Questions? Submit your questions now by clicking on the “Ask a Question” button in

Questions? Submit your questions now by clicking on the “Ask a Question” button in the bottom left corner of your presentation screen. Thank you! You can send additional questions to Steve Kern via editor@searchdomino. com.