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

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

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

Page 1: 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

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

Topics

• Formula Language Basics

• Comparisons to LotusScript

• ND6 Advances

• Typical Uses of the Formula Language

• List Processing

• Agent construction using the Formula Language

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

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

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

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– …

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

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

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

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

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

Basics: Operators

• Arithmetic: +, -, *, /

• Assignment: :=

• Comparisons: =, >, <, !=

• Logical: &, !, |

• List concatenation and subscripting: :, [#]

• Unary: +, -

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

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

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

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

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

Comparisons to LotusScript

• In general, the Formula Language executes more quickly than LotusScript

• 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

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

Comparison: Setting Field Values

• Formula Language@SetField(myField; “Some Value”)

• LotusScriptSub Initialize

' No comments to save spaceDim ws As New NotesUIWorkspaceDim db As NotesDatabaseDim docUI As NotesUIDocumentDim doc As NotesDocument

Set docUI =ws.CurrentDocumentSet doc = docUI.Documentdoc.myField = "Some Value"Call doc.Save(True,True)

End Sub

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

Comparison: Handling Notes Names

• Formula Language@Prompt([OK]; "The Common Name is...";

@Name([CN]; @UserName))

• LotusScript

%INCLUDE "LSCONST.LSS“

Sub Initialize' No comments to save spaceDim sn As New NotesSessionDim nmUser As NotesNameDim jcCommon As StringDim jnBoxStyle As LongDim jnAnswer As Integer

Set nmUser = sn.CreateName(sn.UserName)jcCommon = nmUser.Common

jnBoxStyle = MB_OK + MB_ICONINFORMATIONjnAnswer = Messagebox(jcCommon, jnBoxStyle,"The Common Name is...")

End Sub

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

Comparison: Retrieving External Data with the Formula Language

REM "Use fields in Profile doc";

jcODBCSource := @GetProfileField("GPF"; "cODBCSource");

jcODBCSourceID := @GetProfileField("GPF"; "cODBCSourceID");

jpODBCPassword := @GetProfileField("GPF"; "pODBCPassword");

jcODBCTable := @GetProfileField("GPF"; "cODBCTable");

jcKey := "Key";

REM "Here’s the lookup itself:";

jcValue := @DbLookup( "ODBC" : "NoCache" ; jcODBCSource ; jcODBCSourceID ; jpODBCPassword ; jcODBCTable ; "SomeFld" : "null" ; "KeyFld" ; jcKey );

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

Comparison: Retrieving External Data with LotusScript

Option PublicOption DeclareUselsx "*LSXODBC"

Sub Initialize' Not functional - example only' Limited commentsDim session As New NotesSessionDim db As NotesDatabaseDim docGPF As NotesDocumentDim conSQL As New ODBCConnectionDim qrySQL As New ODBCQueryDim rsltSQL As New ODBCResultSetDim jcODBCSource As StringDim jcODBCSourceID As StringDim jcpODBCPassword As StringDim jcODBCTable As StringDim jcQuery As StringSet db = session.CurrentDatabaseSet docGPF = db.GetProfileDocument("GPF")

jcODBCSource = docGPF.cODBCSource(0)jcODBCSourceID = docGPF.cODBCSourceID(0)jcpODBCPassword = docGPF.pODBCPassword(0)jcODBCTable = docGPF.cODBCTable(0)jcQuery = "SELECT mySQL.SomeFld where keyfield=key"' Create the ODBC ConnectionIf Not conSQL.ConnectTo( jcODBCSource, jcODBCSourceID, jcpODBCPassword) Then

Messagebox "Could not connect to source"Exit Sub

End IfSet qrySQL.Connection = conSQLSet rsltSQL.Query = qrySQLqrySQL.SQL = jcQueryrsltSQL.ExecuteIf rsltSQL.IsResultSetAvailable Then

' Do some processing stuffEnd If' Clean up - close the connectionIf conSQL.IsConnected Then conSQL.Disconnect

End Sub

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

ND6 Advances

• Autocomplete functionality

• Order of execution

• Iterative functionality (well, sort of)

• Nested Assignments and Reassignment of variables

• As always, new @Functions and @Commands

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

ND6: 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

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

ND6: Order of Execution

• Certain @Commands always execute last, especially in prior releases

• Some examples of new @Commands– Deleting documents:

• ND6: @Command([Clear]) executes in place• @Command([EditClear]) executes last

– Running Agents?• ND6: @Command([RunAgent]) executes in place• @Command([ToolsRunMacro]) executes last

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

ND6: Iterative Statements

• ND6 added three new iterative @Functions:– @DoWhile

– @While

– @For

• @DoWhile 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

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

ND6: Assignments

• The FIELD keyword can now be nested– No longer has to be the first item in a statementFIELD myCN := @Name([CN]; (FIELD myNN := @UserName))

@Prompt([Ok]; "The Common Name is..."; myCN);

• Variables can be reassignedjcMyName := @UserName;

jcMyName := @Name([CN]; jcMyName);

@Prompt([Ok]; "The Common Name is..."; jcMyName)

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

Typical Uses of the Formula Language

• Agents

• Field Events – Default Value

– Input Translation

– Input Validation

• Getting user input– @Prompt()

– @DialogBox()

• Keyword Fields– @DbColumn()

– @DbLookup()

• List Processing• View Selection

Conditions• View Column

Formulas

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

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

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

List Processing: Some Useful @Commands

• @Contains()• @DbColumn()• @DbLookup()• @Elements()• @Explode()• @Implode()• @IsMember()• @IsNotMember()

• @Member() • @Replace()• @ReplaceSubString()• @Subset()• @Sum()• @Transform()• @Trim()• @Unique()

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

List Processing: Working with Lists

• Retrieve specific elements– Retrieves the last element in the list

@Subset(jcList; -1)• Replace specific elements

– Replaces “Second” with “2”jcList := "First" : "Second" : "Third" : "Last";jcList := @Replace(jcList; "Second"; "2")

• Combine (concatenate) lists– This code prepends “First” to the list

jcList := "Second" : "Third" : "Last";jcList2 := "First" : jcList

• Perform math– Sums the total sales in the list

jnTotalSales := @Sum(jnList)

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

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

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

Agent Coding: Fields of Interest

• cDocStatus: The status of the document, i.e., New, Approved, Denied

• nmApprover: The Notes name of the approver• cDocHistory: List of actions on the document• cAgentHistory: List of agents run• dAgentHistory: Dates of agents• nReminders: The number of notifications

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

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 nReminder counter

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

Agent Coding: Chunk #1

REM {Send notification to the Approver};

SELECT Form = "FRQ" & cDocStatus = "New";

REM {Set reminder counter - used for escalations};jnReminders := @If(@IsAvailable(nReminders); nReminders + 1; 1) ;@SetField("nReminders"; jnReminders) ;

REM {Add document history};jcDocHistory := "Notification #" + @Trim(@Text(jnReminders)) + " sent to " + @Name([Abbreviate]; nmApprover) + " on " +

@Text(@Now;"S2") ;@SetField("cDocHistory"; cDocHistory : jcDocHistory) ;

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

Agent Coding: Chunk #2

REM {Add agent history} ;

REM {First, make sure the fields exist};

FIELD dAgentHistory := dAgentHistory ;

FIELD cLastAgent := cLastAgent ;

@SetField("dAgentHistory";

@If(dAgentHistory = ""; @Now; dAgentHistory : @Now));

jcLastAgent := "Send Notifications";

@SetField("cAgentHistory"; @If(cAgentHistory = ""; jcLastAgent;

cAgentHistory : jcLastAgent));

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

Agent Coding: Chunk #3

REM {Send the notification};

jcSendTo :=@Name([Abbreviate]; nmApprover);

jcSendTo := "[email protected]";

jcCC := "" ;

jcSubject := "Requisition for " + @Name([Abbreviate]; nmRequester) ;

jcBody := "Please review the Requisition for " +

@Name([Abbreviate]; nmRequester) + ". Click the doclink at the bottom of this message!" ;

@MailSend(jcSendTo; jcCC; jcBCC; jcSubject; jcBody;

""; [IncludeDoclink])

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

Wrapping it up

• In this short time, we have:– Covered the basics– Compared the Formula Language to

LotusScript– Learned about new features in ND6– Learned about List Processing– Wrote an Agent using the Formula

Language

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

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 [email protected].