Triggering an agent from JavaScript

How do I start an agent running from JavaScript.  My web form has a hotspot button which launches a pop up window.  Within the pop up window the user can select a value from a drop down list.  When they click on OK button on the pop up window the value selected is written to a field on the original form (using Javascript), and I also want to manipulate half a dozen other fields in the main window.  This is complicated by the fact the the other fields are all multiple value fields and the value selected on the pop up box determines which values to manipulate in the multiple value fields.  I figure I could do this when the OK button is pressed on the pop up window using JavaScript, but as my Javascript is not too hot I am looking to trigger an agent to do the manipulation instead.

I'd appreciate ideas on whether triggering the agent is the best approach or whether I should concentrate on doing it all in OK hotspot button on the pop up Window.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Java Script will be triggered in the client side. This method is faster than any method.

If ur running Agent, it will take sometime.

to run an agent:

What are the calculations ur doing?
StuartCalamAuthor Commented:

I have six multiple value fields each with the same number of entries. What I want to do is get the value selected by the user in the pop up box, and delete the corresponding values in the other fields.


Field 1 = Red : White : Blue :Green
Field 2 = Monday : Tuesday : Wednesday :Thursday
Field 3 = Dog : Cat : Fish : Bird.

On the pop up box the user can select the colour.  If they select Blue for example, then I need code which will delete Wednesday and Fish from the other fields on the main document.
I know only Lotus Script and I am not much aware of JScript.

Lets wait whether other experts can give answer for it or not.

If u r looking for a LScript, let me know. I will write it for you.

OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

This is much more easilyhandled by js.  However, we need to know what the field type is of the three fields -- checkboxes, plain text, list boxes... what?

The reason this is important is that each field type has a different way of accessing the multiple values.  Text fields will have a separator -- usually comma, but we need to know what specifically.  Manipulating it involves splitting it into an array, finding the appropriate element, and removing it.  SELECT field (list box) and checkboxes are already arrays, and it is a matter fo finding the right array elements, and urning off a flag that indicates it is selected.

Of course, with selects/checkboxes, I'm not sure  whether you want to manipulate what is turned on, or what is avaialble as a choice. You need to let us know that, too!
StuartCalamAuthor Commented:
Thanks for your help.

Not sure how well I can use Lotusscript as I need to do validation etc in the button before passing the values through?
Qwaletee is here.
And don't bother from now. he can handle it easily. Just give the info he is asking for.
Implement Java Script.
Best of luck.
StuartCalamAuthor Commented:

whoops - cross comments!

The fields I want to manipulate are (computed) text and numeric fields.  All are multiple value fields, using the New Line option to separate values.

I don't want to manipulate any options available to users or anything like that.  Just delete the corresponding values from the other fields depending on the value selected in the pop up window

Ah, computed field!  SO, effectively, there is no matching field on the main form, because computed fields are turned to HTML text, not HTML fields.

Is the form in edit mode, read more, or are both possible?
Why can't we have an editable field(Hidden) and have all the values in computed field and delete values from editable fields and append new values to computed values?

Need to know how computed fields are going to have values. May be using @DbLookup/@DbColumn.

If it is the case, then assign the same to editable fields and append.

I think this logic may work.
StuartCalamAuthor Commented:

Form is always in edit mode when this is being done.  The computed fields are computed to themselves so no lookups or anything are being done to get there values.  They are being set somewhere else on the form but that is tomorrows headache!
What you will need to do then is a bit tricky.  Here's why.

Let's say, in addition to the computed list fields you mention, you have a COMMENTS field (or other editable field), and the button that launches the po-up.

User changes comments, then clicks the pop-up.

Problem #1 if we just run an agent directly: What of the main document window was a new document and never saved before?  There is nothing for the agent to run agianst!

Problme #2: Let's say the document was previously saved.  We can't have the pop-up ignore the main window and run an agent -- what would update the main window with new values?  It would not "know" an agent ran, and would stay where it is!

Problem #3: Let's say you have the pop-up pass control back to the main window, and somehow have  the pop-up cause the main window t run the agent.  if all it does is change the URL, then any changes to the COMMENTS field would be lost.

Here's what I suggest to get around all of this.  When the user clicks the "pop-up button," instead of loading the pop-up directly, what the form should do is submit itself, save the changes, and come back with the pop-up screen.  When the user makes a selection in te pop-up screen, it submits itself, which makes teh changes on the original document, and teh window loads again with the original document once more, now modified of course.

Does this course of action make sense to you?
StuartCalamAuthor Commented:

Tricky indeed!!
We can certainly save the document both before and after the pop up box opens if that helps.  Or are you saying we would not have a second window opening for the pop up box?  If possible I would rather keep the pop up box as the users are keen on it.

I can get the value from the pop up box to the main document quite easily so is it possible to have another flag field on the main document?  This could be hidden editable field and when the pop up box value is entered the flag is set.  I could then have the script in the onChange event of the flag field?  

Well, yes you could do that... I will restate this just to make sure we both mean the same thing.

You will place a flag field, type text, NOT HIDDEN on the form, but with HTML attributes set to TYPE=HIDDEN.  The pop-up window, when it closes, will set a value for this HTML hidden flag input field, so that when the document is submitted, the server cde knows what to do.  The pop-up window will also force the main window to submit itself when closes.

So, the pop-up close sequence is:
1) set hidden field value of main
2) submit main
3) close self (pop-up)

Server shoudl then, if it sees the hidden field filled in, do the "reove" processing from teh three hidden fields, clear the value of teh hidden editable field, and redisplay the document again in edit mode.

Is that all correct?
StuartCalamAuthor Commented:

Yes - Sounds good to me; although why can we not hide the flag field that we want to set?
If you hide it in Notes, it doesn't go over to teh web browser at all, and there is therefore no way for your browser-side script to pass a value through it.

Here's thenext problem: Open database properties. Look for the setting, "Use Javascript when generating pages"  Is it on or off?

If it is off, we have an easier time of it.  If it is on, there might be a few bumps on the orad.  Do not turn it on or off unles you understand the full consequences of it.

Here's why it makes a difference.

When yuo have this value OFF, any submission of a form is treated the same, no matter how it occurs.  When you have it on, Notes adds some Javascript so it can determine which button or link or image you clicked to do the submit... and we won't be clicking any of them in our pop-up-driven submit.
StuartCalamAuthor Commented:

Great news. Option is turned ON.
Would have been easier if it wasually.  t is late, I'm tired, please post tomorrow so I'll get a reminder e-Mail to work on this.
StuartCalamAuthor Commented:
ok - thanks for all your help with this...
OK, this is what we will do.

Let's say that your three fields are named F_COLOR, F_DAY, and F_ANIMAL.  Move them to the top of the form.  Make them EDITABLE, visible, and allow multiple values.  HTML attributes should be "TYPE=HIDDEN" for all three.

F_COLOR should have a translation formula of @Trim(F_COLOR) -- that will remove empty values when we turn selected item from pop-up to blank.  Similar for other two fields.

Wheer you used to have the three fields, create three computed text blocks.  The formula for the first should be F_COLOR, the second and third should be F_DAY and F_ANIMAL. In other words, these will be non-editable displays of the current field values.

Let the user press the button to start the popup, same as you have it now, with the same exxact code.  So, no trip to the server to make a popup.

But, at close, it needs to run code something like the following:

var userChoice = .... 'set to value of user's choice on the popup
var form = opener.document.forms[0];
var colors = form.F_COLOR.value.split(","); //turns the editable field into an array
var days = form.F_DAY.value.split(",");
var animals = form.F_ANIMAL.value.split(",");
for(i=0;i<colors.length;i++) {
   if (colors(i)==userChoice) {
      //null out the deleted entry and equivalent position in other arrays is also nulled out
      colors(i) = "";
      days(i) = "";
      animals(i) = "";
      form.F_COLOR.value = colors.join(","); //if it was red,green,blue and green is removed, it becomes red,,blue, which the @Trim turns to red,blue
      form.F_DAY.value = days.join(",");
      form.F_ANIMAL.value = animals.join(",");

The form.submit() will send it to the server.  Since there is no "click value" for the Domino Javascript processing to work with, it will use the default form action of just saving the document; you should put in a $$Return that redisplays the document.  The @Trim removes the "nulled" field value.  The computed text picks up the changed value and displays it as read only.

I'm not quite sure I have all the pieces worked out, but it is a start.  And, the JS may be a little off.
StuartCalamAuthor Commented:

I'm not having much joy I'm afraid.  Heres what I have...

var form = opener.document.forms[0];
      var srNumbers = form.srSpareNo.value.split(",");  //turns the editable field into an array
      var srDescriptions = form.srSpareDesc.value.split(",");
      var srQuantitys = form.srQty.value.split(",");
      var srPrices = form.srPrice.value.split(",");
      var srLocations = form.srLocation.value.split(",");
      var srRemarks = form.srRe.value.split(",");
      //need to get just number part of selected value;
     for(var k=0;k<spNumber.length;k++) {         // For each option inNames field
      if (spNumber.options[k].selected == true) {        // ifselected
            var srKey = window.opener.document.createElement("Option");
            srKey.text = spNumber.options[k].text;
            var srKeySplit = srKey.text.split("-");
            var srKeySelection = window.opener.document.createElement("Option");
            srKeySelection.text = srKeySplit[0];

       for(var i=0;i<srNumbers.length;i++) {    
                 if (srNumbers(i)==srKeySelection) {
                 //null out the deleted entry and equivalent position in other arrays is also nulled out
                 srNumbers(i).value = "";
                 srDescriptions(i).value = "";
                 srQuantitys(i).value = "";
                 srPrices(i).value = "";
                 srLocations(i).value = "";
                 srRemarks(i).value = "";
                  form.srSpareNo.value = srNumbers.join(",");
                  form.srSpareDesc.value = srDescriptions.join(",");
                  form.srQty.value = srQuantitys.join(",");
                  form.srPrice.value = srPrices.join(",");
                  form.srLocation.value = srLocations.join(",");
                  form.srRe.value = srRemarks.join(",");

This is in the hotspot button on the pop up window.  The alert shows that I have getting the value (I think) but immediately after the alert I am getting a 'Function Expected' error message.  

Any ideas what I am doing wrong?

StuartCalamAuthor Commented:
sorry - should mention that spNumber is the name of the field on the pop up window.  That 'split' code is there as I only want what is to the left of the '-' in the value selected.
Put in a few lines like:
alert("Got here #1/#2...");

So we can pinpoint where the failure is.  You don;t have to put one at every line... once you figure out a smaller range, you can sprinkle them into the right place to find the exact line (i.e., the line immediately after a working alert and before a non-working one)
StuartCalamAuthor Commented:
Hello again.

Getting there.  The reason the previous error ocured was because the following...
   if (srNumbers(i)==srKeySelection) {
      srNumbers(i).value = "";
Should of been...  
   if (srNumbers(i)==srKeySelection) {
      srNumbers(i) = "";

So now values is getting removed from the liston the main document.  However, the @Trim translation forumla is not working (not removing the spare comma's).  I am also uinsure about the form.submit.  I already have a $$Return field on my main document and when the pop up box does the submit the value in this field is followied and an error is generated (due to the spare comma's)
The field containing the comma-separated values should be set to be:
1) Text
2) Allow multiple values
3) Use comma as both input separator and output separator
4) Be computed, with @Trim is formula, or editable, with @Trim as input translation
StuartCalamAuthor Commented:
Think I am getting there...

I didn't think we could use computed fields as the Javascript would not be able to retrieve the values.  I of course would much prefer the fields to be computed.  The TYPE=Hidden does not work on editable fields as the HTML code generates a textarea tag for the field.  Type is not an attribute of this tag.  
You are correct, computed fields are not sent as fields, so Javascript can't access them.  However, we have created a pair of Domino representations of each value: A field (F_xxxx) and a computed for display value.

HTML allows two types of text fields: regular inputs, and textareas.  Domino normally generates INPUTs, not TEXTAREAs.  Two exceptions:

1) Rich text fields -- you should be using plain text
2) Plain text fields that allow multiple values and use new line as separator.  Designate comma as input and outpit separator, and do not use new line or blank line.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Lotus IBM

From novice to tech pro — start learning today.