onAfterChange code executes twice - InfoPath Dev
in

InfoPath Dev

onAfterChange code executes twice

Last post 03-04-2010 08:18 AM by zxkuqyb. 4 replies.
Page 1 of 1 (5 items)
Sort Posts: Previous Next
  • 11-03-2006 11:15 AM

    onAfterChange code executes twice

    I have a check box that at the moment is just opening a word document when the box is checked. The code seems to be executing twice. Anyone have any idea why?



    function msoxd_my_adddoc::OnAfterChange(eventObj)
    {
    // Write code here to restore the global state.
    var adddoc = XDocument.DOM.selectSingleNode("/my:SCInvoice/my:Header/my:adddoc").text;
    if (adddoc == "true")
    {
    var doc = new ActiveXObject("Word.Application");
    doc.Visible=false;
    doc.Documents.Open("Something.doc");
    }
    if (eventObj.IsUndoRedo)
    {
    // An undo or redo operation has occurred and the DOM is read-only.
    return;
    }

    // A field change has occurred and the DOM is writable. Write code here to respond to the changes.

    }

  • 11-03-2006 05:25 PM In reply to

    Re: onAfterChange code executes twice

    First off, you should move the IsUndoRedo above the other code. The event handlers will enter multiple times with different actions. I don't remember but think that Checkbox may always enter with both a Delete and an Insert, you'll want to add some code to check. Here's a reference:

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ipsdk/html/xdproOperation_2_HV01021316.asp

    function msoxd__item::OnAfterChange(eventObj)
    {
    if (!eventObj.IsUndoRedo && eventObj.Operation == "Delete"
    && eventObj.Site.nodeName == "item" &&
    eventObj.Source.nodeName == "item")
    {
    Calculate();
    }
    }

    Above, the code is checking for just Delete. Add a breakpoint to your code and see if the action is different. My guess is that it is and you need to filter out one.
    Patrick Halstead
    Project Manager at Qdabra
  • 03-04-2010 07:13 AM In reply to

    • zxkuqyb
    • Not Ranked
      Male
    • Joined on 01-14-2009
    • Indianapolis, IN
    • Posts 18

    Re: onAfterChange code executes twice

    I appologize for the redundancy, but I'm experiencing a similar issue and can't figure out what I'm doing wrong.  Basically, I have a "Status" field and when it changes, I want to append to the history that the Status has changed...the history is appending twice and am not sure how to have it append once...any help would be greatly appreciated...

     Thanks

    Thomas N Barrett

     

    function msoxd_my_Approval_Status::OnAfterChange(eventObj)
    {
    // Write code here to restore the global state.

       if (!eventObj.IsUndoRedo && eventObj.Operation == "Delete"
          && eventObj.Site.nodeName == "item" &&
             eventObj.Source.nodeName == "item")
    {
     return;
    }

    var StatusChoice = XDocument.DOM.selectSingleNode("/my:myFields/my:InputData/my:Approval_Status").text

     switch (StatusChoice)
      {
       case "Approved":

        XDocument.DOM.selectSingleNode("/my:myFields/my:Admin/my:Approval_Status_FLAG").text = "Approved";
        XDocument.DOM.selectSingleNode("/my:myFields/my:Admin/my:Approval_Status_Change_Date").text = todaysDate();

        // Append to History that the Current Status has changed...Also, capture Username and Date / Time stamp of the Current Status change
        var CurrentUser = XDocument.DOM.selectSingleNode("/my:myFields/my:Admin/my:AdminSection/my:Current_User_Name").text;
        var newComment = XDocument.DOM.selectSingleNode("/my:myFields/my:NewCommentGroup/my:NewComment").text;

        // Create the individual nodes (for the Comment History Table)
        var nodeCommentHistoryTableRows = XDocument.DOM.createNode(1, "CommentHistoryTable", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2007-03-16T18:51:51");
        var nodeCommentUserName = XDocument.DOM.createNode(1, "CommentUserName", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2007-03-16T18:51:51");
        var nodeCommentDate = XDocument.DOM.createNode(1, "CommentDate", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2007-03-16T18:51:51");
        var nodeCommentHistory = XDocument.DOM.createNode(1, "CommentHistory", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2007-03-16T18:51:51"); 

        // Create the node structure for the Comment History Table
        nodeCommentHistoryTableRows.appendChild(nodeCommentUserName);
        nodeCommentHistoryTableRows.appendChild(nodeCommentDate);
        nodeCommentHistoryTableRows.appendChild(nodeCommentHistory);

        // Append Values
        nodeCommentUserName.text = CurrentUser;
        nodeCommentDate.text = XDocument.DOM.selectSingleNode("/my:myFields/my:Admin/my:Approval_Status_Change_Date").text;
        nodeCommentHistory.text = "Current Status changed to: " + XDocument.DOM.selectSingleNode("/my:myFields/my:Admin/my:Approval_Status_FLAG").text;

        // Append row to XML document
        var parent = XDocument.DOM.selectSingleNode("/my:myFields/my:CommentHistoryGroup");
        parent.appendChild(nodeCommentHistoryTableRows);

       break;

       case "In Process":

        XDocument.DOM.selectSingleNode("/my:myFields/my:Admin/my:Approval_Status_FLAG").text = "In Process";
        XDocument.DOM.selectSingleNode("/my:myFields/my:Admin/my:Approval_Status_Change_Date").text = todaysDate();

        // Append to History that the Current Status has changed...Also, capture Username and Date / Time stamp of the Current Status change
        var CurrentUser = XDocument.DOM.selectSingleNode("/my:myFields/my:Admin/my:AdminSection/my:Current_User_Name").text;
        var newComment = XDocument.DOM.selectSingleNode("/my:myFields/my:NewCommentGroup/my:NewComment").text;

        // Create the individual nodes (for the Comment History Table)
        var nodeCommentHistoryTableRows = XDocument.DOM.createNode(1, "CommentHistoryTable", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2007-03-16T18:51:51");
        var nodeCommentUserName = XDocument.DOM.createNode(1, "CommentUserName", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2007-03-16T18:51:51");
        var nodeCommentDate = XDocument.DOM.createNode(1, "CommentDate", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2007-03-16T18:51:51");
        var nodeCommentHistory = XDocument.DOM.createNode(1, "CommentHistory", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2007-03-16T18:51:51"); 

        // Create the node structure for the Comment History Table
        nodeCommentHistoryTableRows.appendChild(nodeCommentUserName);
        nodeCommentHistoryTableRows.appendChild(nodeCommentDate);
        nodeCommentHistoryTableRows.appendChild(nodeCommentHistory);

        // Append Values
        nodeCommentUserName.text = CurrentUser;
        nodeCommentDate.text = XDocument.DOM.selectSingleNode("/my:myFields/my:Admin/my:Approval_Status_Change_Date").text;
        nodeCommentHistory.text = "Current Status changed to: " + XDocument.DOM.selectSingleNode("/my:myFields/my:Admin/my:Approval_Status_FLAG").text;

        // Append row to XML document
        var parent = XDocument.DOM.selectSingleNode("/my:myFields/my:CommentHistoryGroup");
        parent.appendChild(nodeCommentHistoryTableRows);

       break;

       case "Rejected":

         XDocument.DOM.selectSingleNode("/my:myFields/my:Admin/my:Approval_Status_FLAG").text = "Rejected";
         XDocument.DOM.selectSingleNode("/my:myFields/my:Admin/my:Approval_Status_Change_Date").text = todaysDate();

        // Append to History that the Current Status has changed...Also, capture Username and Date / Time stamp of the Current Status change
        var CurrentUser = XDocument.DOM.selectSingleNode("/my:myFields/my:Admin/my:AdminSection/my:Current_User_Name").text;
        var newComment = XDocument.DOM.selectSingleNode("/my:myFields/my:NewCommentGroup/my:NewComment").text;

        // Create the individual nodes (for the Comment History Table)
        var nodeCommentHistoryTableRows = XDocument.DOM.createNode(1, "CommentHistoryTable", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2007-03-16T18:51:51");
        var nodeCommentUserName = XDocument.DOM.createNode(1, "CommentUserName", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2007-03-16T18:51:51");
        var nodeCommentDate = XDocument.DOM.createNode(1, "CommentDate", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2007-03-16T18:51:51");
        var nodeCommentHistory = XDocument.DOM.createNode(1, "CommentHistory", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2007-03-16T18:51:51"); 

        // Create the node structure for the Comment History Table
        nodeCommentHistoryTableRows.appendChild(nodeCommentUserName);
        nodeCommentHistoryTableRows.appendChild(nodeCommentDate);
        nodeCommentHistoryTableRows.appendChild(nodeCommentHistory);

        // Append Values
        nodeCommentUserName.text = CurrentUser;
        nodeCommentDate.text = XDocument.DOM.selectSingleNode("/my:myFields/my:Admin/my:Approval_Status_Change_Date").text;
        nodeCommentHistory.text = "Current Status changed to: " + XDocument.DOM.selectSingleNode("/my:myFields/my:Admin/my:Approval_Status_FLAG").text;

        // Append row to XML document
        var parent = XDocument.DOM.selectSingleNode("/my:myFields/my:CommentHistoryGroup");
        parent.appendChild(nodeCommentHistoryTableRows);

       break;
      }

    // A field change has occurred and the DOM is writable. Write code here to respond to the changes.
    }

    Thomas N. Barrett
  • 03-04-2010 07:59 AM In reply to

    Re: onAfterChange code executes twice

     This is the problem:

       if (!eventObj.IsUndoRedo && eventObj.Operation == "Delete"
          && eventObj.Site.nodeName == "item" &&
             eventObj.Source.nodeName == "item")
    {
     return;
    }

    This causes the handler to exit only if:

    • A Delete operation is being performed AND
    • The node that triggered the event is called "item" AND
    • The node being modified is called "item"

    Since your node is not called "item," this if statement will never cause the event handler to exit and the event handler will always run twice (once for the Delete event and once for the Insert event) every time the field is modified.  Try changing the above to this:

    if(eventObj.IsUndoRedo || eventObj.Operation == "Delete")
    {
         return;
    }

    I'd also suggest refactoring your code a bit.  It looks like you have the exact same large chunk of code repeated three times.

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 03-04-2010 08:18 AM In reply to

    • zxkuqyb
    • Not Ranked
      Male
    • Joined on 01-14-2009
    • Indianapolis, IN
    • Posts 18

    Re: onAfterChange code executes twice

    Jimmy,

    Thank you for this...it works perfectly.  Much appreciated!

     Thomas N. Barrett

    Thomas N. Barrett
Page 1 of 1 (5 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.