logo

Running Two WQS Agents, Where One is Conditional on the Other

A thought occurred to me yesterday. We all know you can run multiple WQS agents and that you can make them conditional using @If statements, right? What I didn't know was whether you could have two agents run but only have the second agent run depending on the outcome of the first. Imagine the code below:

ScreenShot001

 

In the example shown, does the server decide which agents to run and then just run them or does it run the first and then enter the @If to decide whether to run the second? That's what I didn't know. Well, it turns out it's the latter case. Agent One runs and sets a field on the document called "runSecondAgent" to a value of 1. This trigger Agent Two. If in Agent One you stop the code from setting that field value then Agent Two doesn't run. Just what I was hoping would happen.

Something I found out from testing this out is that you can't "print" to the browser from consecutive agents. Anything you print from the first is lost if a second agent runs. Even if that second agent doesn't print anything.

How did I end up wondering this in the first place?

It came about because I wanted to use the iText PDF library to create and send out an invoice once a document was saved. The Form already has a WSQ agent written in LotusScript. To generate the PDF I need to run some Java. Therein lied the problem. Depending on certain logic in the main LotusScript WQS agent I need to run some Java. The concept above solves this.

It's a bit of a messy solution though. Makes me wonder if there's isn't another way?

An Alternative, Which I'd Tried First, But Which Failed

Before I got to wondering about the conditional agents I had tried using LS2J. Ever heard of it? It allows you to call JavaScript code from within LotusScript. I've used it successfully in the past to resize images from LotusScript using Julian's code. It's all very clever, but in this case doesn't seem much use as I can't find a way to pass a handle to the document context in to the Java class used.

I did all the usual LS2J stuff to create an object of the class called PDFInvoice, the code for which lived in a Java Code Library in the database and looked something like this:

   1: import lotus.domino.*;
   2:  
   3: public class PDFInvoice extends AgentBase{
   4:     Database db;
   5:     
   6:     public PDFInvoice (String docUnid) throws NotesException    {    
   7:         try { 
   8:             Session s = getSession();
   9:             //Can't get past the above line        
  10:         } catch ( Exception e ){
  11:             e.printStackTrace();
  12:         }
  13:     }
  14: }

It fails on line 8 though when you try and create a session object. Any reference to "s" after that returns a "null pointer".

Is there no way in LS2J to have the Java that's called operate on the current document (or any Notes objects for that matter)? As I understand it you can only pass primitive data-types as arguments. However, I was hoping to pass the document's ID over and somehow get to it that way. If I can't open a session though, how would that work?

I'm starting to wonder how good an idea it would be to just write all Agents in Java from now on. Seems to me there's little LotusScript can do that Java can't. Whereas the opposite is not true at all.

Comments

  1. Hey Jake - what about just calling the Java agent from the first WQS LS agent and passing in the document context that way. I used this to solve an almost identical scenario as yours with iText.

  2. Scott already posted my intended proposal. If you -just- want to operate on a document, and not return anything, there's no reason to use LS2J..

    --

    The reason I stick with LS is because of the horrible Java-editor/debugger.

    • avatar
    • Jake Howlett
    • Thu 17 Jul 2008 05:25 AM

    Thanks guys. Like an idiot I didn't think of that.

    I presume you mean like so:

    Set agentTwo = db.GetAgent("(wqs Agent Two)")

    agentTwo.Run( doc.UniversalID )

    Would this work as expected though?

    We're passing it the ID of the document currently in context rather than the document itself. Would it be able to find the document if this were the first save? Would all operations on the document be reflected in the code that runs in Agent One after it's called Agent Two.

    Jake

  3. That's the caveat - I don't think you can work on the document in Agent 2 through the context. You would have to first make a save in Agent 1 in order to have some kind of handle to grab in Agent 2 through the Agent ParameterID. You could always remove the backend document in Agent 1 afterwards I guess.

    Having said that are we sure even with the formula solution that some kind of backend save has not been performed after the first agent run? If not then the solution you came up with might be faster.

  4. LS2J only works when you are using pure java classes (i.e. sans notes java classes) in your code. This is an architectural limitation and the moment you try to access notes backend classes (via LS2J call), you are likely to end up with "Cannot create a session from an agent" error.

  5. @Jake:

    agentTwo.Run( doc.NoteID )

    Then in Java:

    Document doc = db.getDocumentByID( agent.getParameterDocID() );

    See the example in the Run method of Agent in the Designer documentation.

    --

    When running the agents from WQO, I wonder if the domino server saves the document inbetween consecutive agents, or if they somehow share the same session.

    If they share the same session, maybe the same could be achieved using agent.run..?

    • avatar
    • Jake Howlett
    • Thu 17 Jul 2008 03:36 PM

    I guess there's only way one to find out. I'll report back tomorrow...

Your Comments

Name:
E-mail:
(optional)
Website:
(optional)
Comment:


About This Page

Written by Jake Howlett on Thu 17 Jul 2008

Share This Page

# ( ) '

Comments

The most recent comments added:

Skip to the comments or add your own.

You can subscribe to an individual RSS feed of comments on this entry.

Let's Get Social


About This Website

CodeStore is all about web development. Concentrating on Lotus Domino, ASP.NET, Flex, SharePoint and all things internet.

Your host is Jake Howlett who runs his own web development company called Rockall Design and is always on the lookout for new and interesting work to do.

You can find me on Twitter and on Linked In.

Read more about this site »

Elsewhere

Here are the external links posted on the same day.

More links are available in the archive »

More Content