Search code examples
xpagesssjs

alert in SSJS Library


I have a function in SSJS Library. I would like get Client Side alert in Library. What i tried was not worked. I think I miss something. :(

function docAlive()
{
    try 
    {
        var otherDoc:NotesDocument= null;
        if (funcDoc.getItemValueString("DocUNID")!="")
        {
            var otherDoc:NotesDocument = dbKontak.getDocumentByUNID(funcDoc.getItemValueString("DocUNID"))
            if (otherDoc==null)
            {
              hataKod = "10001";
              hataMsg = "There is no document :( Created One";
              print (hataKod +": "+hataMsg);
              view.postScript("alert('"+hataKod + " - " +hataMsg+"');");
            }
        }
        return otherDoc;
    }
    catch (e) 
    {
        e.toString();
    }    
}

Solution

  • view.postScript() will trigger a client-side alert, but as Tim Tripcony mentions, not in all events. And the alert will only be triggered after the function and any other code for the partial refresh has completed. At that point the HTML to trigger the (Client-Side) JavaScript alert will be posted back to the browser and the browser will action it.

    If you want to throw an error back to the browser, I would strongly recommend XPages OpenLog Logger (and not just because I contribute and support it on OpenNTF). openLogBean.addError(e) will log the error to OpenLog and post an error message back to the browser.

    The message is passed to the server using facesMessage.addMessage(), as documented here http://www.intec.co.uk/returning-error-messages-from-ssjs-through-the-facescontext/. I believe there are additional options for managing different message levels (e.g. WARNING, CONFIRMATION). FacesMessage is a standard Java (in this case, JSF) construct, so the documentation for it on the web is valid for XPages as well.