Creating an script library

I have created a function Response Time as follows:


Public Function responsetime( StartDate As String, EndDate As String) As Double

Dim w As New NotesUIWorkspace	

Dim session As New NotesSession	

Dim db As NotesDatabase	

Dim uidoc As NotesUIDocument

Dim i As Integer, ctr As Integer

Dim dt1 As NotesDateTime

Dim busdays As Variant

Dim no_of_bussdays As Integer

Dim holidaylist (1 To 15) As String

Dim dtEnd As NotesDateTime

Dim dtStart As NotesDateTime

Dim dtTempHoliday As NotesDateTime

Dim lstHolidays List As String

Dim viewIT As NotesView

Dim entry As NotesViewEntry

Dim selString As String

Dim monstart As NotesDateTime

Dim monend As NotesDateTime 

Dim monlunchstart As NotesDateTime

Dim monlunchend As NotesDateTime

Dim fristart As NotesDateTime

Dim friend As NotesDateTime 

Dim frilunchstart As NotesDateTime

Dim frilunchend As NotesDateTime

Dim starttime As NotesDateTime

Dim endtime As NotesDateTime 

Dim lunchstarttime As NotesDateTime

Dim lunchendtime As NotesDateTime

Dim fridays As Variant

Dim no_of_firdays As Integer

Dim time_weekdays As Double

Dim time_fridays As Double

Dim time_startday As Double

Dim time_endday As Double





Set db = session.CurrentDatabase	

Set uidoc = w.CurrentDocument

'Getting Timings from “Timings” View*************************************************

Set viewIT = db.GetView("Timings")

selstring = uidoc.FieldgetText("x_fd_location")

Set entry = viewIT.Getentrybykey(selstring)



Set monstart = New NotesDateTime(entry.ColumnValues(1))

Set monend = New NotesDateTime(entry.ColumnValues(2))

Set monlunchstart = New NotesDateTime(entry.ColumnValues(3))

Set monlunchend = New NotesDateTime(entry.ColumnValues(4))

Set fristart = New NotesDateTime(entry.ColumnValues(5))

Set friend = New NotesDateTime(entry.ColumnValues(6))

Set frilunchstart = New NotesDateTime(entry.ColumnValues(7))

Set frilunchend = New NotesDateTime(entry.ColumnValues(8))

'Setting Holidays*****************************************************************

lstHolidays("02/05/2007") = "Kashmir Day"

lstHolidays("03/23/2007") = "Pakistan Day"

lstHolidays("05/01/2007") = "Labor Day"

lstHolidays("08/14/2007") = "Independence Day"

lstHolidays("11/09/2007") = "Iqbal's Day"

lstHolidays("12/25/2007") = "Quaid's Day"

'Calculating time for Business days exculding start date and end date***********************

'Calculating days exclucding sundays and saturdays

Set dtStart =  New NotesDateTime(StartDate)

Set dtEnd =  New NotesDateTime(EndDate )



busdays = Evaluate({@BusinessDays([ } & Cdat(dtStart.DateOnly) & {]; [} & Cdat(dtEnd.DateOnly) & {]; 1:7)})

no_of_bussdays= Cint(busdays(0))

'Count num of holidays and subtract it from business days

Forall h In lstHolidays

	Set dtTempHoliday = New NotesDateTime( Listtag(h) )

	If (dtTempHoliday.TimeDifferenceDouble( dtStart ) >= 0) _

	And (dtEnd.TimeDifferenceDouble( dtTempHoliday ) >= 0) Then

        ' Holiday is in this range

		no_of_bussdays = no_of_bussdays - 1

	End If

End Forall

'Count no of fridays and subtract it from business days as fridays have different timings

fridays = Evaluate({@BusinessDays([ } & Cdat(dtStart.DateOnly) & {]; [} & Cdat(dtEnd.DateOnly) & {]; 1:2:3:4:5:7)})

no_of_fridays = Cint(fridays(0))

’ if user request day is friday then subtract 1 from no of fridays

If(Weekday(dtstart.DateOnly) = 6) Then

	no_of_fridays = no_of_fridays-1

End If

’ if IT request day is friday then subtract 1 from no of fridays

If(Weekday(dtend.DateOnly) = 6) Then

	no_of_fridays = no_of_fridays-1

End If

’ calculate time for days between start and end date excluding fridays

time_weekdays = (no_of_bussdays - no_of_fridays-2) *( ((Cdat(monend.TimeOnly) - Cdat(monstart.TimeOnly)) - (Cdat(monlunchend.TimeOnly) - Cdat(monlunchstart.TimeOnly))) * 24)

'calculate time for fridays between start and end dates

time_fridays = (no_of_fridays) *( ((Cdat(friend.TimeOnly) - Cdat(fristart.TimeOnly)) - (Cdat(frilunchend.TimeOnly) - Cdat(frilunchstart.TimeOnly))) * 24)

‘calculate time for start day and end day’***********************************************

If(Not(Weekday(dtstart.DateOnly) = 6)) Then

	Set starttime = New NotesDateTime(monstart.TimeOnly)

	Set endtime = New NotesDateTime(monend.TimeOnly)

	Set lunchstarttime = New NotesDateTime(monlunchstart.TimeOnly)

	Set lunchendtime = New NotesDateTime(monlunchend.TimeOnly)

	

Else

	

	Set starttime = New NotesDateTime(fristart.TimeOnly)

	Set endtime = New NotesDateTime(friend.TimeOnly)

	Set lunchstarttime = New NotesDateTime(frilunchstart.TimeOnly)

	Set lunchendtime = New NotesDateTime(frilunchend.TimeOnly)

	

End If

’ START DAY

’ if start time is less than official start time then add whole day time

If (Cdat(dtstart.TimeOnly)<Cdat(starttime.TimeOnly)) Then

	time_startday = (Cdat(starttime.TimeOnly) - Cdat(endtime.TimeOnly)) * 24

	

Else

’ if start time is greater than official end time then add nothing

	If (Cdat(dtstart.TimeOnly)>Cdat(endtime.TimeOnly)) Then

		time_startday = 0

		

	Else

’ if start time is between official timings then add calculated time

		If( (Cdat(dtstart.TimeOnly)  >= Cdat(starttime.TimeOnly)) And  ( Cdat(dtstart.TimeOnly)<=Cdat(endtime.TimeOnly) )  ) Then

			time_startday = ( Cdat(endtime.TimeOnly) - Cdat(dtstart.TimeOnly) ) * 24								

			

		End If

		

	End If

	

End If

'Call uidoc.FieldSetText(“a”,Cstr(time_startday))

’ subtracting lunch time

’ if start time is between lunch time then subtract the calculated time

If( (Cdat(dtstart.TimeOnly)  >= Cdat(lunchstarttime.TimeOnly)) And  ( Cdat(dtstart.TimeOnly)<=Cdat(lunchendtime.TimeOnly) )  ) Then

	time_startday = time_startday - (( Cdat(lunchendtime.TimeOnly) - Cdat(dtstart.TimeOnly) ) * 24)

’ if start time is before lunch time then subtract whole lunch time

Else 		

	If( Cdat(dtstart.TimeOnly)  < Cdat(lunchstarttime.TimeOnly))  Then

		time_startday = time_startday - (( Cdat(lunchendtime.TimeOnly) - Cdat(lunchstarttime.TimeOnly) ) * 24)

		

	End If 

End If

'END DAY

' if end time is greater than official end time then add whole day time

If (Cdat(dtend.TimeOnly)>Cdat(endtime.TimeOnly)) Then

	time_endday = (Cdat(starttime.TimeOnly) - Cdat(endtime.TimeOnly)) * 24

	

Else

’ if end time is less than official start time then add nothing

	If (Cdat(dtend.TimeOnly)<Cdat(starttime.TimeOnly)) Then

		time_endday = 0

		

	Else

’ if end time is between official timings then add calculated time

		If( (Cdat(dtend.TimeOnly)  >= Cdat(starttime.TimeOnly)) And  ( Cdat(dtend.TimeOnly)<=Cdat(endtime.TimeOnly) )  ) Then

			time_endday = ( Cdat(dtend.TimeOnly) - Cdat(starttime.TimeOnly) ) * 24				

			

		End If			

	End If		

End If

’ subtracting lunch time

’ if end time is between lunch time then subtract calculated time

If( (Cdat(dtend.TimeOnly)  >= Cdat(lunchstarttime.TimeOnly)) And  ( Cdat(dtend.TimeOnly)<=Cdat(lunchendtime.TimeOnly) )  ) Then

	time_endday = time_endday - (( Cdat(dtend.TimeOnly) - Cdat(lunchstarttime.TimeOnly) ) * 24)

’ if end time is after lunch time then subtract whole lunch time

Else 

	If( Cdat(dtend.TimeOnly)  > Cdat(lunchendtime.TimeOnly) ) Then

		time_endday = time_endday - (( Cdat(lunchendtime.TimeOnly) - Cdat(lunchstarttime.TimeOnly) ) * 24)

		

		

	End If 

End If

’ Return the calculated response time in hours******************************************

responsetime = time_weekdays + time_fridays + time_startday + time_endday

End Function


It works fine if i paste this function in the Globals but when i create a script library for this function and call the function as

i = responsetime (uidoc.FieldGetText(“date1”),uidoc.FieldGetText(“date2”))

then it gives error on this line.

any tips??

Regards,

Aisha Ishrat.

Subject: Creating an script library

What was the error?

Did you include the script library in your code? (Use “scriptLibrary” - within your options)

http://www.stevecastledine.com

Subject: RE: Creating an script library

when i add following line in options

Use “Response Time”, it gives error

(Globals):(options):1:Error loading USE or USELSX module: Response Time

Subject: RE: Creating an script library

Then

  1. The script library does not exist in that database

  2. You have mistyped - maybe a space is not used in the Library name

I suggest you read on using script librarys in the developer help database - lots of good info there

Subject: RE: Creating an script library

In either case, Notes might not allow the agent to save. But I think she is able to run the agent and getting the error during runtime. Hence “compilation” would be a good idea before executing the agent.

Subject: RE: Creating an script library

Try this way:

Add your function to script library

Call the function in your agent and add Use “your scriptlibrary” in options of your agent and finally save it.

If you are unable yo save the agent, comment the function in agent and save it. Open it again uncomment the function and save it.

From notes menu, Compile the script (Tools —> Recompile All Lotus script).

Now try running your code again.

Subject: Creating an script library

Resolved.

Well the issue is that initially I created script library as follows:

From the Design pane, expand Shared Code and select Script Libraries.A list of script libraries displays in the Design list.and from there I clicked “New script library”.thats it and i use it in my form then it gives the error i mentioned.

Now i create it as follows:

  1. Choose Create - Design - Script Library from the f orm on which i want to use script library.

  2. Choose one of the following:

LotusScript Library

Javascript Library

Java Library

The Programmer’s pane displays the empty script in the Work pane.

  1. To name the library, choose Edit - Properties.

Each library in a database must have a unique name. This is true for multilingual databases as well; you cannot differentiate between multiple script libraries of the same name using the $Language field of the design note.

  1. Enter the script in the Script pane.

For LotusScript, first select an existing script; it can be an (Options), (Declarations), Initialize, Terminate, or user script and either:

Enter new or change existing code for the script.

To write your own LotusScript script, replace the title of the script following the Sub or Function statements. For example, to write a script called Once, change Sub Initialize to Sub Once.

The editor automatically creates a new script using this new title.

  1. Choose File - Save, then File - Close to save and close the library.

Its working.

Regards,

Aisha Ishrat.