title: XPages event code executing twice rather then once
body: <p>Here’s what I want to do:</p>
  
  <p>On an XPage I want to have an edit box control that calls some server-side code (Java bean or SSJS) when the user presses enter.  The main use case for this is that this page will be run from an an iPad and a bluetooth barcode scanner is attached.  This scanner emulates a keyboard.  The user will tap into the edit box.  They will then scan with the scanner and the barcode will be entered along with an “enter” key.  The edit box needs to be immediately cleared for the next scan and the value of the scan needs to be processed. The processing will do various backend things to a java bean and then return a message to be displayed on the page. Basically one of these:
  1. Item not found, 2. Item is available and assigned, 3. Item is already assigned but you can have it anyway 4. Item Unavailable</p>
  
  <p>I have code for this and overall the core code is working fine. I’ve normally used the onchange event but since I had this problem I moved to the onkeypress event. That was as much an attempt to get it to run in IE.  Again the goal is only for the iPad/Safari, but I’d love to get it working on desktop browsers as well.  I’m currently testing only with Chome.  </p>
  
  <p>In the CSJS piece of the edit box I have this code:</p>
  
  <pre><code>var e = dojo.fixEvent(thisEvent);
  if (e.keyCode == dojo.keys.ENTER) {
  return true;
  } else {
  return false;
  }
  </code></pre>
  
  <p>The thinking is that I only want to call the SSJS on Enter.</p>
  
  <p>Here’s the SSJS code that clears the edit box, processes the value and then sets a couple viewScope variables.</p>
  
  <pre><code>// Get the value off the page and put it in to the key variable
  var key:String = getComponent("scanBox").getValue();
  // clear the scanbox so it's ready for the next scan.
  getComponent("scanBox").setValue("");
  //Managed Bean to hold the last scan value - since the scanbox is cleared we do want to show the value
  Scan.setLastScan(key);
  
  // Managed Bean to process the item.
  AddToJob.processItem(key);
  
  viewScope.put("vsShowAssignPanel", true);
  viewScope.put("vsShowMessagePanel", false);
  </code></pre>
  
  <p>The SSJS code is set to partially update the main content of the page.  I’m using bootstrap and am trying to refresh everything on the page except a top and bottom nav bar.  The editbox itself IS in the partial refresh zone since I’m clearing the value after the scan.</p>
  
  <p>Finally here’s the problem:</p>
  
  <p>The SSJS code of the button is being run TWICE. Not all for some reason but maybe 90% of the time.  I’m not clicking twice.  The way I test is by using Chrome on my desktop and I paste a value into the field and press enter once on the keyboard.</p>
  
  <p>If I’m trying to scan in and assign an Item with a barcode.  It first runs fine.  the it updates the objects and saves a field to the backend NotesDocument to mark it assigned.  Just want I want.  But the second run through hits and it’s deterring that the item is ALREADY assigned and giving me back the wrong message.</p>
  
  <p>After a lot of trial and error I’m fairly certain that something in the JSF lifecycle is causing this to run twice. Why I have no idea.  I have no idea how to get it once.</p>
  
  <p>The only good workaround that I’ve found so far is in the “AddToJob.processItem” method.  “AddToJob” is a managed bean in the viewScope.  In there I store and keep the last barCode.  Then I do a check to see of the value is the same.  If so then I assume it’s on the second run and stop processing.</p>
  
  <p>I’ve used this basic concept of calling code in the on change event of an edit control for years.  But that was inside XPages Mobile Controls and sometimes things do behave a little differently in there. I’m trying to re-write this app to use Bootstrap rather then XPages Mobile controls.</p>
  
  <p>Below is the full XML markup of the edit control if that helps.</p>
  
  <pre><code>&lt;xp:inputText id="scanBox" styleClass="newTarget"&gt;
  &lt;xp:this.attrs&gt;
      &lt;xp:attr name="placeholder" value="Tap to Scan..." /&gt;
      &lt;xp:attr name="autocorrect" value="off" /&gt;
  &lt;/xp:this.attrs&gt;
  &lt;xp:this.dojoAttributes&gt;
      &lt;xp:dojoAttribute name="autocorrect" value="off" /&gt;
  &lt;/xp:this.dojoAttributes&gt;
  &lt;xp:eventHandler event="onkeypress" submit="true" refreshMode="partial" refreshId="mainPanel"&gt;
  &lt;xp:this.script&gt;&lt;![CDATA[var e = dojo.fixEvent(thisEvent);
  if (e.keyCode == dojo.keys.ENTER) {
  return true;
  } else {
  return false;
  }]]&gt;&lt;/xp:this.script&gt;
  &lt;xp:this.action&gt;&lt;![CDATA[#{javascript:// Get the value off the page and put it in to the key variable
  var key:String = getComponent("scanBox").getValue();
  // clear the scanbox so it's ready for the next scan.
  getComponent("scanBox").setValue("");
  
  //Managed Bean to hold the last scan value - since the scanbox is cleared we do want to show the value
  Scan.setLastScan(key);
  
  // Managed Bean to process the item.
  AddToJob.processItem(key);
  
  viewScope.put("vsShowAssignPanel", true);
  viewScope.put("vsShowMessagePanel", false);}]]&gt;&lt;/xp:this.action&gt;
  &lt;/xp:eventHandler&gt;&lt;/xp:inputText&gt;
  </code></pre>
  
  <p>ANY information is appreciated.  Thank you very much!!!</p>