AM25.1 Looping DME Query

 20 Replies
 0 Subscribed to this topic
 12 Subscribed to this forum
Sort:
Author
Messages
brihyn
Veteran Member
Posts: 95
Veteran Member

    The users would like to see the name of the Asset next to the asset number on the bottom portion of AM25.1.  I wrote the DME query, and it works.

    The first time I do an inquire, the name field will be random. If i go to page two, I see page 1's Asset number name next to page 2's asset number. Page three sees page two's name, and so on (basically my dme query is one page behind).

    Also, I only get a name for the first Asset number. I'm not really sure how I loop my script to populate through all 22 Asset's attached per page. Any help would be appreciated.

    Here's my query:


    function FORM_OnAfterTransaction(fc)
    {
     //was transaction successful?
     if (formState.agsError) return;

     //get Asset number
     var vAst=lawForm.getFormValue("out9");
     //alert("asset number is "+vAst);

     var vDesc = ""
     lawForm.setFormValue("text11","");

     //Do DME call
     var s = portalWnd.DMEPath;
     s += "?PROD=" +strPDL;
     s += "&FILE=AMASSET&FIELD=DESCRIPTION;&INDEX=ASTSET1";
     s += "&KEY=" + vAst;
     s += "&XCOLS=TRUE&XKEYS=TRUE&XRELS=TRUE&XCOUNT=TRUE&XIDA=TRUE&OUT=XML";
     alert("string is "+s)

     //send DME call to server
     var vDMEInfo = portalWnd.httpRequest(s);
     //alert(vDMEInfo.xml);

     // DME Error Checking
     if(!vDMEInfo||vDMEInfo.status)
     {
      var msg="Error calling DME, ";
      msg += (vDMEInfo? "(status code): "+vDMEInfo.status: "bad server response.");
      alert(msg);
      return true;
     }

     //create an XML object to contain the DME data
     var vObjDMEXML = new portalWnd.DataStorage(vDMEInfo);
     var vRecords=vObjDMEXML.document.getElementsByTagName("RECORD");
     if (vRecords.length == 0)
     return true;

     var vCols=vRecords[0].getElementsByTagName("COL");
     var vDesc = vCols[0].firstChild.data;
     //alert("Description is "+vDesc);

     lawForm.setFormValue("text11",vDesc);

     
    }

    brihyn
    Veteran Member
    Posts: 95
    Veteran Member
      I wrote the DME query, and it works.


      that was supposed to be "it kinda works"
      Terry P
      Veteran Member
      Posts: 234
      Veteran Member
        I'm not familair with that particular form, but you'll need to do something like this. First you need to use the "row" for your getFormValue and setFormValue.

        Create a loop like shown below. If you're on version 9, row numbers start with 1, otherwise they start with 0.

        for (var rowNum = 1; rowNum < 23; ++rowNum)
        {
        //get Asset number
        var vAst=lawForm.getFormValue("out9",rowNum);

        // insert all you other DME code here

        lawForm.setFormValue("text11",vDesc,rowNum);
        }
        brihyn
        Veteran Member
        Posts: 95
        Veteran Member
          Thanks. I was just in the DS class last week, so all of this is still pretty new for me.
          brihyn
          Veteran Member
          Posts: 95
          Veteran Member
            Made the suggested changes, but the only change in my results is that the inquire query now takes forever (2-5 minutes after hitting I). I still get a random item listed on the first asset on the first screen (and the other 21 have no data at all) and then screen 2 shows the name associated with screen 1's asset #, and so on.
            Still seeking any help you all can provide.
            Thanks again!
            for reference, here's the updated script:

            function FORM_OnBeforeTransaction(fc)
            {
            //was transaction successful?
            if (formState.agsError) return;
            //blank out the fields
            var vDesc = ""
            lawForm.setFormValue("text11","");
            for (var rowNum = 1; rowNum < 23; ++rowNum)
            {
            //get Asset number
            var vAst=lawForm.getFormValue("out9",rowNum);
            //Do DME call
            var s = portalWnd.DMEPath;
            s += "?PROD=" +strPDL;
            s += "&FILE=AMASSET&FIELD=DESCRIPTION;&INDEX=ASTSET1";
            s += "&KEY=" + vAst;
            s += "&XCOLS=TRUE&XKEYS=TRUE&XRELS=TRUE&XCOUNT=TRUE&XIDA=TRUE&OUT=XML";
            //send DME call to server
            var vDMEInfo = portalWnd.httpRequest(s);
            // DME Error Checking
            if(!vDMEInfo||vDMEInfo.status)
            {
            var msg="Error calling DME, ";
            msg += (vDMEInfo? "(status code): "+vDMEInfo.status: "bad server response.");
            alert(msg);
            return true;
            }

            //create an XML object to contain the DME data
            var vObjDMEXML = new portalWnd.DataStorage(vDMEInfo);
            var vRecords=vObjDMEXML.document.getElementsByTagName("RECORD");
            if (vRecords.length == 0)
            return true;
            var vCols=vRecords[0].getElementsByTagName("COL");
            var vDesc = vCols[0].firstChild.data;
            lawForm.setFormValue("text11",vDesc,rowNum);
            }

            return true;
            }
            brihyn
            Veteran Member
            Posts: 95
            Veteran Member
              Update-
              So reading the articles here on LG.com, I've figured out how to populate all 22 fields in the detail window. I still have the one problem where the first screen shows random asset names, screen two shows names that should have been associated with the asset numbers on screen one, screen three shows a connection to #'s on page 2, and so on.
              It's really as if the function FORM_OnBeforeTransaction(fc) is the wrong fuction, but I've tried AfterTransaction (which is what I originally assumed), as well as several others.
              Gary Davies
              Veteran Member
              Posts: 248
              Veteran Member
                I assume it is a type and you are not doing it on OnBeforeTransaction.

                Try this

                function FORM_OnAfterTransaction()
                {
                //was transaction successful?
                if (formState.agsError) return;

                for (var rowNum = 1; rowNum < 23; ++rowNum)
                {
                //get Asset number
                var vAst=lawForm.getFormValue("out9",rowNum);
                //reset desc to blank for each detail line
                var vDesc = "";
                //Do DME call if there is an asset
                if (vAst != "") {
                var s = portalWnd.DMEPath;
                s += "?PROD=" +strPDL;
                s += "&FILE=AMASSET&FIELD=DESCRIPTION;&INDEX=ASTSET1";
                s += "&KEY=" + vAst;
                s += "&OUT=XML";
                //send DME call to server
                var vDMEInfo = portalWnd.httpRequest(s);
                // DME Error Checking
                if(!vDMEInfo||vDMEInfo.status)
                {
                var msg="Error calling DME, ";
                msg += (vDMEInfo? "(status code): "+vDMEInfo.status: "bad server response.");
                alert(msg);
                return true;
                }

                //create an XML object to contain the DME data
                var vObjDMEXML = new portalWnd.DataStorage(vDMEInfo);
                var vRecords=vObjDMEXML.document.getElementsByTagName("RECORD");
                if (vRecords.length != 0) {
                var vCols=vRecords[0].getElementsByTagName("COL");
                var vDesc = vCols[0].firstChild.data;
                }
                }
                lawForm.setFormValue("text11",vDesc,rowNum);
                }

                return true;
                }

                Terry P
                Veteran Member
                Posts: 234
                Veteran Member
                  The code Gary suggested on seeing if there even an asset number first should take care of the amount of time it's taking to populate the array. If you do have a full 22 asset numbers on the screen, it will take some time to do 22 dme calls. I wouldn't expect it to take 2-5 minutes though. I suspect what happened is if the key field is "blank", its reading ALL the records. In either case, Gary's code should fix that.
                  brihyn
                  Veteran Member
                  Posts: 95
                  Veteran Member
                    Yup, Gary's code made a HUGE difference on the speed. I think it was about 5 minutes on the initial inquire before, now it's as fast as it was prior to even having any Javascript, so a huge thanks for that.
                    I'm still getting the first screen showing all bogus information, second screen shows names attached to first screen's numbers and so on.
                    Getting closer....
                    Terry P
                    Veteran Member
                    Posts: 234
                    Veteran Member
                      So have you tried putting an alert to display vAst and the rowNum before the dme call, then an alert after to display the returned value.
                      brihyn
                      Veteran Member
                      Posts: 95
                      Veteran Member
                        So have you tried putting an alert to display vAst and the rowNum before the dme call, then an alert after to display the returned value.


                        Yes, actually I removed them when I pasted my script here for help to save a bit of screen,but I have alerts all over the place. Showing the Vast etc, showing the resulting xml, and anything else that I thought would help.
                        The alerts are actually showing the data as I'd expect it to show based on my inquire results. For instance, the vAst is empty the first time I hit Inquire. Second time I hit it, it shows me my vAst from the first screen. So my results are really expected. It really seems like the "AfterTransaction" event happens first, it runs my script, and then it pulls the results in to display on the screen.
                        Terry P
                        Veteran Member
                        Posts: 234
                        Veteran Member
                          You might try this. I use a slightly different method to get the return value from a DME call, especially when it's only one field I'm getting. You might try this, or something like it. Your value will be in the variable sReturn

                          //Return value from table
                          var objhttp = new ActiveXObject("Msxml2.XMLHTTP")
                          objhttp.Open("GET", sDME, false)
                          objhttp.Send("UserInfo")
                          sReturn = objhttp.responseTEXT
                          brihyn
                          Veteran Member
                          Posts: 95
                          Veteran Member
                            Thanks. I'll give that a try.
                            Here's something interesting that I just saw with the original Gary-ized script. I changed my alert to as follows:

                            //get Asset number
                            var vAst=lawForm.getFormValue("out9",rowNum);
                            alert("asset number is "+vAst+" row number is "+rowNum);

                            so the first time i hit Inquire, I get "your asset number is " (blank, no data), "row number is "(and it gives me 22 alerts, displaying row numbers 1-22)
                            When I'm done hitting 22 "ok's", i then get my initial screen with the 22 asset numbers, and 22 bogus asset names.

                            I then hit Inquire again, and it starts filling in page 1's asset names correct, with the alert 1-22. "asset number is "(correct asset number), "row number is" (with the correct row number)
                            Once I hit ok for this second round of 22 times, the details screen then changes showing page 2's 22 asset numbers, with the asset names still from page 1's list of numbers.

                            That help at all?
                            brihyn
                            Veteran Member
                            Posts: 95
                            Veteran Member
                              So I'm still struggling with this. The problem is really that it's running the query first, and then populating the Detail Area portion of the form. Is there any way to insure that the Detail Area is populated first prior to running my script?
                              Again, thanks to everyone for your excellent help.
                              John Henley
                              Posts: 3353
                                You should put the code into OnAfterTransaction, but only execute it for an Inquire, Next, Previous, PageUp, PageDown--but not for Add/Change/Delete, etc....

                                If the code is in OnBeforeTransaction, it is using the asset numbers from whatever was on the screen before.
                                Thanks for using the LawsonGuru.com forums!
                                John
                                Gary Davies
                                Veteran Member
                                Posts: 248
                                Veteran Member

                                  I thought that at first too and had logic in their such as:

                                  if (lawForm.magic.FC == "I" || ...)   But it may be needed for the Add/Change,  what if deleting an Asset, you need to go through the logic to clear out the descriptions.  You would also need to add the description if adding an asset tag.

                                  John Henley
                                  Posts: 3353
                                    Good point--thanks!
                                    Thanks for using the LawsonGuru.com forums!
                                    John
                                    brihyn
                                    Veteran Member
                                    Posts: 95
                                    Veteran Member
                                      Incidentally, I *am* actually using the AfterTransaction, and I tried with the I (although got an error...didn't have something typed correctly).
                                      My paste above with the Before was actually when I was trying every other function to see if something would work. AfterTransaction still gives me the names prior to updating the screen.
                                      brihyn
                                      Veteran Member
                                      Posts: 95
                                      Veteran Member
                                        I really need to consolidate replies!
                                        I did just try the if (lawForm.magic.FC == "I" ||lawForm.magic.FC == "N" ) and same results. it' does the script first, then fills in the detail window, even with the AfterTransaction.
                                        Gary Davies
                                        Veteran Member
                                        Posts: 248
                                        Veteran Member
                                          Try referencing the DataValue instead of the FormValue for the asset number. As a general rule for me, if it has a screen field for a data source if read the DataValue and write the FormValue. The state of these fields are different in BeforeTransaction and AfterTransaction.
                                          brihyn
                                          Veteran Member
                                          Posts: 95
                                          Veteran Member
                                            Hot dang, that did it.
                                            Thanks, Gary, and everyone else!

                                            here's the full script if anyone is interested:

                                            function FORM_OnAfterTransaction(fc)
                                            {

                                            //was transaction successful?
                                            if (formState.agsError) return;

                                            for (var rowNum = 0; rowNum < 22; ++rowNum)
                                            {
                                            //get Asset number
                                            var vAst=lawForm.getDataValue("ASSET",rowNum);
                                            // alert("asset number is: "+vAst+"; rownumber is "+rowNum);

                                            //reset desc to blank for each detail line
                                            var vDesc = "";

                                            //Do DME call if there is an asset
                                            if(vAst != "")
                                            {
                                            var s = portalWnd.DMEPath;
                                            s += "?PROD=" +strPDL;
                                            s += "&FILE=AMASSET&FIELD=DESCRIPTION;&INDEX=ASTSET1";
                                            s += "&KEY=" + vAst;
                                            s += "&XCOLS=TRUE&XKEYS=TRUE&XRELS=TRUE&XCOUNT=TRUE&XIDA=TRUE&OUT=XML";
                                            // alert("string is "+s)

                                            //send DME call to server
                                            var vDMEInfo = portalWnd.httpRequest(s);
                                            // alert(vDMEInfo.xml);

                                            // DME Error Checking
                                            if(!vDMEInfo||vDMEInfo.status)
                                            {
                                            var msg="Error calling DME, ";
                                            msg += (vDMEInfo? "(status code): "+vDMEInfo.status: "bad server response.");
                                            alert(msg);
                                            return true;
                                            }

                                            //create an XML object to contain the DME data
                                            var vObjDMEXML = new portalWnd.DataStorage(vDMEInfo);
                                            var vRecords=vObjDMEXML.document.getElementsByTagName("RECORD");
                                            if (vRecords.length != 0)
                                            {
                                            var vCols=vRecords[0].getElementsByTagName("COL");
                                            var vDesc = vCols[0].firstChild.data;
                                            // alert("Description is "+vDesc);
                                            }
                                            }
                                            lawForm.setFormValue("text11",vDesc,rowNum);
                                            }
                                            return true;

                                            }