Home > Domino Tips > Developer > LotusScript > LotusScript 101 - Custom Functions
Domino Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

LOTUSSCRIPT

LotusScript 101 - Custom Functions


Kevin Ford
06.11.2001
Rating: -3.08- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


I see a lot of Notes/Domino developers that use LotusScript but don't understand or use customized Functions. This is a real shame, because it is one of the most powerful tools that we have when writing modular code. It is, in fact, required to define modular code. Instead of declaring global variables and calling SubRoutines to modify these variables, create a function that you can pass a value to and will return a value when it is done doing what it is supposed to.

Here's the format for the custom function... (I am only going to talk about the simplest implementation.)

Function FunctionName( Parameters ) as ReturnType
Statements
Statements
End Function

At any time during a function, you can make the function return a value by simply treating the FunctionName like a variable. For example, the statement in parenthesis (FunctionName = "Red") will make the function return the value "Red" to the Call statement that called the function in the first place.

For the purpose of this example, My hypothetical function will prorate a salary over two time periods with two pay rates. It will accept three variables; CurrentPayRate, NewPayRate, IncreaseMonth. It will return the prorated salary. Obviously, you could do a lot more sophisticated calculations, but this should get the point across.

BELOW IS THE CODE FOR THE FUNCTION ITSELF

Code

Function Prorate( CurrentPayRate As Double, NewPayRate As Double, IncreaseMonth As Integer ) As Double
	'---- BEGIN simple error checking (Return 0 by using "Prorate = 0"
	If Not Isnumeric(CurrentPayRate) Or Not Isnumeric(NewPayRate) Or Not Isnumeric(IncreaseMonth) Then
		Msgbox "You must supply a number for each field", 0, "Invalid Values"
		Prorate = 0
		Exit Function
	Elseif CurrentPayRate = 0 Or NewPayRate = 0 Or IncreaseMonth = 0 Then
		Msgbox "You must supply a number for each field", 0, "Invalid Values"
		Prorate = 0
		Exit Function
	End If
	'---- END simple error checking
	'---- BEGIN Calculations on variables that were passed to function ----
	CurrentMonthlyRate = (CurrentPayRate / 12)
	NewMonthlyRate = (NewPayRate / 12)
	CurrentDollars = (CurrentMonthlyRate * (IncreaseMonth-1) )
	NewDollars = ( NewMonthlyRate * (12 - (IncreaseMonth-1) ) )
	'---- END Calculations on variables that were passed to function ----
	Prorate = (CurrentDollars + NewDollars)
End Function


BELOW IS THE CODE FOR THE STATEMENTS THAT CALL THE FUNCTION
-----------------------------------------------------------
Sub Click(Source As Button)
	Set workspace = New NotesUIWorkspace	
	Set uidoc = workspace.CurrentDocument
	
	currentPayRate = Cdbl(uidoc.FieldGetText("currentPayRate"))
	newPayRate = Cdbl(uidoc.FieldGetText("newPayRate"))
	increaseMonth = Cint(uidoc.FieldGetText("increaseMonth"))
	
	NewSalary = ProRate( currentPayRate, newPayRate, increaseMonth )
	If NewSalary > 0 Then
		Msgbox Cstr( NewSalary )
	End If
End Sub

For the above example, I created a form with three fields (named in Click event above). I then created a button marked ProRate. From any LotusScript editing pane for the button, simply typing the word "function functionName" (without quotes) and pressing enter will create a new event in that object and you can begin working immediately. After creating the function as above, I simply modified the click event of the button to call the ProRate function with the proper parameters (Data Types are of course critical). I declared all of my variables in the Global Declarations not as a matter of good practice but just to keep the code smaller for this example.

Using a function for a single use as in the button example is not very effective, but if you can do it, you can start creating your own functions that you can call iteratively and save yourself a lot of time in future programming. It would also allow you to create much more compact code.

An example might be to pass a back-end NotesDocument object to a function that validates and returns a boolean value of whether or not the document passed the test. Roughly, it would look like this... Function ValidateDoc( doc_in_question as NotesDocument ) As Integer

	ValidateDoc = 0
	if doc_in_question is nothing then
		Exit Function
	End If
	If not doc_in_question.HasItem("ImportantFieldName") then
		Exit Function
	End If
	'---- My two criteria passed, so I assume success and set to 1 then return
	ValidateDoc = 1
End Function

It would be called from any line of LotusScript like this.

if ValidateDoc( MyDoc ) = False then
	MsgBox "It Failed"
Else
	MsgBox "It Worked"
End If

Rate this Tip
To rate tips, you must be a member of SearchDomino.com.
Register now to start rating these tips. Log in if you are already a member.




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
LotusScript
LotusScript agent parses ACL to Microsoft Notepad
LotusScript finds the first occurrence of a string from the right
Clear Recent Contacts view and prevent repopulation in Lotus Notes 8.x
Search Microsoft Active Directory with LotusScript
Three steps to trap and handle save conflicts with LotusScript
Troubleshoot agents by displaying LotusScript variables online
LotusScript sorts lists alphabetically
LotusScript code rebuilds corrupted busytime.nsf file
Soft-code item names to facilitate LotusScript management
LotusScript agent automates selective mail file replication

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



Domino & Lotus Notes Security Solutions: Authentication, Antispam, Encryption and Antivirus
HomeTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersDomino IT Downloads
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 1999 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts