lotus-noteslotus-dominolotusscriptlotus

Whats the simpelst way to split multiple paramters with query_string?


Im calling an agent with a URL like this:

http://myserver/db.nsf/testagent?openagent&param1=ONE&param2=TWO

The agent 'testagent' should get the value of paramter as fast as possible. I read that 'query_string' should do that, but the code gets very long, when I really want ONLY to get the values ONE and TWO. Is there a more simpler / faster way to get these values?

My Lotus Script code looks like:

Dim s As New NotesSession
Dim db As NotesDatabase
Dim dc As NotesDocumentCollection
Dim doc As NotesDocument
Dim arg As String, p1 As Long

arg = s.DocumentContext.Query_String(0)
p1 = InStr(arg, "&")

If p1 = 0 Then
    Print "ERROR: No arugment."
    Exit Sub
Else
    arg = LCase(Mid$(arg, p1 + 1))
    ' (more code): Split again the ARG to & (more code)
    ' (more code): Split the values again to =, after that fill the 2 element as param 1 (ONE) and param 2 (TWO)
End If

Solution

  • You have the answer already in the title of your question: just split the string...

    In addition I usually use lists for this kind of tasks as they are easy and fast to access. I‘d take that out as a separate function to be able to reuse the code and make everything better readable:

    Function GetQuerystringParameters( strQueryString as String ) as Variant
      Dim varParams as Variant
      Dim lstrParams List as String
      varParams = Split( strQueryString, "&")
      Forall strParam in varParams
        If Instr(strParam, "=") > 0 then
          lstrParams( strtoken( strParam, "=", 1))= strtoken( strParam, "=", 2)
        End If
      End ForAll
      GetQuerystringParameters = lstrParams
    End Function
    

    Now you can simply get the right parameter and Parameters need not even be in the right order:

    Dim urlParams as Variant
    Dim yourArg as String
    urlParams = GetQuerystringParameters( arg )
    If isElement( urlParams( "param1" ) ) then
      yourArg = urlParams( "param1" )
    Else
      Print "Error: no param1"
      Exit Sub
    End If
    

    Hope that helps.

    Edit: I would probably even make a class from this to further reduce code and make it even better reusable:

    Class UrlParameters
      Private lstrParams List as String
      Sub New( strQueryString as String )
        Call Me.GetParameters( strQueryString )
      End Sub
      Private Sub GetParameters( strQs as String )
        Dim varParams as Variant
        varParams = Split( strQs, "&")
        Forall strParam in varParams
          If Instr(strParam, "=") > 0 then
            Me.lstrParams( strtoken( strParam, "=", 1)) = strtoken( strParam, "=", 2)
          End If
        End ForAll
      End Sub
      Function getUrlParameter( strParam as String ) as String
        If Iselement(Me.lstrParams(strParam)) then
          getUrlParameter = Me.lstrParams(strParam)
        Else
          GetUrlParameter = ""
        End If
      End Function
    End Class
    

    You could use this class like that:

    Dim myParams as UrlParameters
    Set myParams = New UrlParameters( arg )
    param1 = myParams.getUrlParameter("param1")
    param2 = myParams.getUrlParameter("param2")