Search code examples
javascriptlotus-noteslotus-dominolotusscript

Directly Getting View Information/Records in Lotus Notes in Javascript


Basically I need to access some records or data from a view in lotus notes itself. I can't use use the @DBLookup since our target is not to refresh the form. I know that using AJAX it is possible though I still haven't tried AJAX yet if you have a detailed tutorial please share it here.

My main question is basically any other easier way to access these records in the view? Directly coding in the javascript part of a field. Thanks a lot.

Basically @DbLookup = "?" Javascript (Not AJAX).


Solution

  • I would do like Torsten is suggesting, create a Lotusscript agent that performs the lookup and returns a JSON object with the data. You then make an Ajax-call to that agent from your webpage using Javascript or (even easier) jQuery.

    I posted some code on my blog a while back. It is doing something similar, but instead of performing a view lookup, it retrieves the values of a specific document based on a document ID. You can find the code and a more detailed explanation here: http://blog.texasswede.com/code-snippet-jquery/

    This is the jQuery code:

    function loadNotesFields(docunid) {
        var notesfieldname = "";
        $.ajax({
            url: "/database.nsf/ajax_GetNotesFieldFields?OpenAgent", 
            data: {"NotesUNID":docunid},
            cache: false
        }).done(function(data) {
            $('input[notesfield]').each(function() {
                notesfieldname = $(this).attr("notesfield");
                $(this).val(data[notesfieldname]);
            });
        });
    }
    

    And this is the Lotusscript code:

    Dim urldata List as String
    Sub Initialize
        Dim session As New NotesSession
        Dim webform As NotesDocument
        Dim db As NotesDatabase
        Dim doc As NotesDocument
        Dim urlstring As String
        Dim urlarr As Variant
        Dim urlvaluename As Variant
        Dim i As Integer
        Dim json As String
    
        Set webform = session.DocumentContext
        '*** Remove leading "OpenAgent" from Query_String
        urlstring = StrRight(webform.Query_String_Decoded(0),"&")
        '*** Create list of arguments passed to agent
        urlarr = Split(urlstring,"&")
        For i = LBound(urlarr) To UBound(urlarr)
            urlvaluename = Split(urlarr(i),"=")
            urldata(urlvaluename(0)) = urlvaluename(1)
        Next
        Set thisdb = session.CurrentDatabase
        '*** Create content header for return data
        Print "content-type: application/json"
        '*** Get Notes document baed on NotesUIND argument
        Set doc = db.GetDocumentByUNID(urldata("NotesUNID"))
        '*** Build JSON for all fields in document except $fields
        json = "{" + Chr$(13)
        ForAll item In doc.Items
            If Left$(item.Name,1)<>"$" Then
                json = json + |"| + item.Name + |":"| + item.Text + |",|+ Chr$(13)
            End If
        End ForAll
        '*** Remove trailing comma and line break
        json = Left$(json,Len(json)-2)  
        json = json + "}"
        '*** Return JSON
        Print json  
    End Sub