Opening an IP form with JS - InfoPath Dev
in

InfoPath Dev

Opening an IP form with JS

Last post 07-09-2007 07:22 AM by ErikvanD. 3 replies.
Page 1 of 1 (4 items)
Sort Posts: Previous Next
  • 07-05-2007 11:21 PM

    • ErikvanD
    • Not Ranked
      Male
    • Joined on 07-02-2007
    • Netherlands
    • Posts 5

    Opening an IP form with JS

    Hey all,

     

    Being totally new to Infopath (2003) and almost new to JS I have been struggling with an IP form for several days now…

     

    I have a simple form that I would like to call with a JS that fills out the first field on the form. Also very similar to:

     

    this post

    and this one

     

    I have been searching this forum, the internet and several IP books but cannot find the solution. I guess it might have to do something with namespaces but as I do not have a debugger or the IP tookit it is hard to find the error.

     

    This is my code:

     

    //Start the application

    var oApp = new ActiveXObject("InfoPath.Application");

     

    //Open an InfoPath document from the published template

    var oXDocumentCollection = oApp.XDocuments;

    var oXDocument = oXDocumentCollection.NewFromSolution("file:///C:\\temp\\prog_1.xsn");

    var oXMLDocument = oXDocument.DOM;

    var oXMLDocNamespace = oXMLDocument.selectSingleNode("/*[1]").namespaceURI;

     

    oXMLDocument.setProperty("SelectionNamespaces","xmlns:dfs ='" + oXMLDocNamespace + "'");

    var oID = oXMLDocument.selectSingleNode("/dfs:myFields/dfs:Dimensie").text = 84011;

     

    //Query the database

    oXDocument.Query()

     

     

    When I run this script I get the error: “oXMLDocument.selectSingleNode(…)’ is null or not an object”

     

    What am I doing wrong??

     

     

    Kind regards,

    Erik

  • 07-06-2007 09:18 AM In reply to

    Re: Opening an IP form with JS

    Here is what I have used successfully before.

    This allows for the parmeters to be passed on the command line.  A couple things to watch out for is that you may have to specify your path to infopath or run this in the same folder.  Also If you do not have the exact amount of variables it wont work.  So if you only need one variable eliminate the others in the script.

    //myscript.js

    //parse parameters

    if(WScript.Arguments.count()==3)

    {

    var param1 = WScript.Arguments.Item(0);

    var param2 = WScript.Arguments.Item(1);

    var param3 = WScript.Arguments.Item(2);

    }

    //Start the application

    var oApp = new ActiveXObject("InfoPath.Application");

    WScript.Echo("InfoPath Version: " + oApp.Version);

    //Open an InfoPath document from the published template

    var oXDocumentCollection = oApp.XDocuments;

    var oXDocument = oXDocumentCollection.NewFromSolution("http://insideen/sites/hrs/am/workflow/forms/template.xsn");

    oXDocument.DOM.setProperty("SelectionNamespaces", "xmlns:dfs='http://schemas.microsoft.com/office/infopath/2003/dataFormSolution' xmlns:my='http://schemas.microsoft.com/office/infopath/2003/myXSD/2006-12-04T22:36:39'");

    // Get pointers to the target fields

    var oNum = oXDocument.DOM.selectSingleNode("//dfs:myFields/my:Header/my:ActionID");

    var oName = oXDocument.DOM.selectSingleNode("//dfs:myFields/my:Header/my:EmpName");

    var oJob = oXDocument.DOM.selectSingleNode("//dfs:myFields/my:Header/my:JobTitle");


    //Update the fields

    oNum.text = param1;

    oName.text = param2;

    oJob.text = param3;

     

    //Submit the Form

    //oXDocument.Submit();

    //Close up shop

    //oXDocumentCollection = null;

    //oApp.Quit();

    //oApp = null;

    You would not assign your node to the namespace I think is your issue. You need to define a variable linked to the field and then set the value of that field.

    You should be able to add your query command and you see that this script has a submit and close commands commented out.

    So the form is left open ready for additional use when it is done. 

    Have Fun.

  • 07-06-2007 09:26 AM In reply to

    Re: Opening an IP form with JS

    Hey Erik.

    Looking at this closer it appears you are trying to set a main data connection to a database.  If that is so then I am not sure your xpath is correct it would typically be /dfs:myFields/dfs:queryFields/dfs:Dimensie

    Maybe the namespace thing you have works, I have not tried that before.

    If you are not planning on using a variable and passing the number to query in then you could just have rules to accomplish this in the template when it opens.

  • 07-09-2007 07:22 AM In reply to

    • ErikvanD
    • Not Ranked
      Male
    • Joined on 07-02-2007
    • Netherlands
    • Posts 5

    Re: Opening an IP form with JS

    Hi Clay,

    Thank you for your help. I have tried your script you posted above. But I still get the same error. So I included a messagebox just before "Update the fields" . I also changed the place of the "Dimensie" field on my IP form to being not the first field on the form. Purpose was to see if the cursor really gets to the correct field after opening the form. Well the the script correcty opens the form but the cursor remains at the first field (so not the one I put in the script) and it does not fill out a value. Then it gives the same error again (null or no object).

    I have also tried it on my original form that connects to a SQL database (I used a very much simplified version to test the correct filling from the JS). Same error. When I use the following dataconnectionstring: //dfs:myFields/dfs:dataFields/d:tbl_prog/@Dimensiecode_1 I get the error that "d: is an undeclared namespace prefix".

    So, not very succesful so far... Any ideas? To be honest, after strugging to get it to work for a week now I am getting pretty fed up with it..... but on the other hand, I am sure that it could work!

    Cheers, Erik 

    PS: One other question: in your script where do you actually fill the param1 etc with an actual value? I would like to open my form with the value "84011" in the field "Dimensie". 

     

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