Monday, 3 December 2012

iNotes Customization: Perform Server side check before sending emails

This blog is dedicated to Lotus iNotes 8.5 Customization to ensure that some Server side checks can be performed before emails is sent out.. The iNotes Customization documentation has certain examples but not complete to demonstrate this exact scenario..

Special thanks to Eric W. Spencer, IBM for providing wonderful insight for the same.

Steps to follow:

1. Setup Forms85_x.nsf: Instructions on creating the Extension Forms File are in the Lotus iNotes Administrator documentation. The topic can be found in Domino and Notes info center here.

2. Edit Custom_JS_Lite Subform in Forms85_x.nsf

Setup a flag "hAgentSend" in each new Email Document that is created. The starting code is there to pick the current UI Form ids and then create an Input element and add it to current Mail form.

function Custom_Scene_PostLoad_Lite( s_SceneName, bInEditMode ){
    if ((s_SceneName || '').split(':')[1] == 'EdT' && s_SceneName.indexOf('$new') !== -1 ) {
        var oMailInfo = EKc.prototype.EYl[s_SceneName];

            var oElem = AAA.EcK.getElementById(s_SceneName.split(':')[0]);

            var sContentId = oElem.getAttribute('com_ibm_dwa_ui_mailinfo_contentid');

            var oContent = AAA.Fkb().getContent(sContentId);

        // Get the current UI document
        var oMainDoc = AAA.EcK;
        // Get the current backend document ID
        var sDocUnid = oContent.sId;
        // Get the current UI form
        var oForm = oMainDoc.getElementById( 'e-pageui-' + sDocUnid ) || {};
   
        var oInput = oMainDoc.createElement('INPUT');
        oInput.setAttribute('name','hAgentSend');
        oInput.setAttribute('id','hAgentSend');
        oInput.setAttribute('type','hidden');
        oForm.appendChild(oInput);

    }
}

Now, set the code to call an Agent from server using AJAX and enable Mail send functionality only in Callback function of AJAX response:

function Custom_Scene_PreSubmit_Lite( s_SceneName, bInEditMode ){
    if  (s_SceneName == "memo") {

        // Get the current backend document ID
        var oContent = AAA.Fkb().EdI.get('T').oPrev;
        var sDocUnid = AAA.Fkb().EdI.get('T').oPrev.sId;

        // Get the current UI document
        var oMainDoc = AAA.EcK;

        // Get the current UI form
        var oForm = oMainDoc.getElementById( 'e-pageui-' + sDocUnid );

        // If the agent is sending the memo, just send
        if (oForm["hAgentSend"]) {
            if (oForm["hAgentSend"].value == "true")
                return true;
        }
   
        // do any checking here and set up xhr to run agent..............
        url= window.location.protocol + "//" + window.location.hostname + "/inotes/forms85_x.nsf/agCheckGroupAccess?OpenAgent";
http_request = false;
if (window.XMLHttpRequest) { // Mozilla
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
    http_request.overrideMimeType('text/xml');
}
} else if (window.ActiveXObject) { // IE
try {
    http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
    http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert('Cannot create XMLHTTP instance');
return false;
}

        //setting mail send on AJAX Call
        http_request.onreadystatechange  = function()
{
    if(http_request.readyState  == 4)
    {
        if(http_request.status  == 200) {    // success

            var resp = http_request.responseText;
    if(resp.indexOf("true") !== -1)
        bOkayToSend = true;
            // Get response info from agent and do any necessary processing
            // Decide if it's okay to send the memo
            // ...

            if (bOkayToSend) {

                // okay to send the memo
           
                // Get the current backend document ID
                var sDocUnid = AAA.Fkb().EdI.get('T').oPrev.sId;
           
                // Get the current UI document
                var oMainDoc = AAA.EcK;
           
                // Get the current UI form
                var oForm = oMainDoc.getElementById( 'e-pageui-' + sDocUnid );
           
                // Set the flag that says memo is being sent
                oForm["hAgentSend"].value = true;
               
                // Send the memo
                setTimeout("AAA.DSq.ELU(null, 'e-actions-mailedit', 'EbK', {sAction: 'a-send'})", 1000);
            }
            else {
                // put up some kind of error message
                alert("invalid email");
            }
        }
    }
}
       
        //Send AJAX Request
        http_request.open('POST', url, true);
        http_request.send();
       
        // Return false to prevent email from getting sent now
        return false;
    }

    return true;
}

The agent should return a value of "true" to enable the mail send.. Here is the code for the smallest agent:

Option Public
Option Declare

Sub Initialize
   
    MsgBox "Running Agent: iNotes Mail Send"
   
    Print "true"
   
End Sub

Reference Documents:

1. http://www-10.lotus.com/ldd/dominowiki.nsf/dx/The_basics_of_iNotes_customization

2. http://www-10.lotus.com/ldd/dominowiki.nsf/dx/manipulating-data-in-inotes-lite-forms

If required I can publish the modified Forms85_x.nsf here as well.. Please let me know.