Can't create a Vendor Comment using the writeattach API from AP10.1.

Author
Messages
Randy Jacoy
Veteran Member
Posts: 46
Veteran Member

    I am attempting to programmatically create a vendor comment in the FORM_OnAfterTransaction(data) event of AP10.1.  On pages 105-106 of the Lawson Design Studio User Guide version 10 (LDSUG_10.0.9.0_UWA.pdf) there is an example for exactly what I want to do.  The problem is that it's not a working example.  It looks like it's creating an Invoice comment, not a Vendor comment because it references Invoice data fields that aren't available to AP10.  Here is their example:

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

     // grab the value for the select box
     var vList = lawForm.getFormValue("select12");
     if (vList != "None")
     addComment(vList);
    }

    function addComment(vList)
    {

    //Case for the four options
     var vAudt
     switch (vList)
     {
     case "All":
     vAudt="A";
     break;
     case "Report":
     vAudt="D";
     break;
     case "Note":
     vAudt="N";
     break;
     case "Check":
     vAudt="C";
     break;
     }
     // set variables from the form to use in API
     var vComm=lawForm.getFormValue("textarea1");
     vCompany = lawForm.getDataValue("API-COMPANY");
     vVendor = lawForm.getDataValue("API-VENDOR");
     var vInvoice=lawForm.getFormValue("text12");
     vTitle = portalWnd.oUserProfile.getAttribute("lawsonusername");

     // build the API call for the writeattach cgi
     s = portalWnd.WRITEATTACHPath + "?_OUT=XML&_PDL=" + strPDL;
     s += "&_FN=APINVOICE&_IN=APISET1&K1=" + vCompany;
     s += "&K2=" + vVendor;
     s += "&K3=" + vInvoice;
     s += "&K4=000&K5=0000&_ATYP=C&_AUDT=" + vAudt;
     s += "&_USCH=none&_DATA=TRUE&_OPM=M&_ECODE=FALSE&_ANAM=" + vTitle;
     s += "&_ATXT=" + vComm;

     // send API to the server
     portalWnd.httpRequest(s);

     // Reset the Textarea and set the select to None
     var space="";
     lawForm.setFormValue("textarea1","");
     lawForm.setFormValue("select12","None");
    }

    You can see it gets data from API-COMPANY and API-VENDOR which are not AP10 data fields.  Here is what I am doing (have some debugging code in there for now):

    Here is my call to the addVendorComment function below:

    addVendorComment(vendorCompany,sVendorfld,"","D","OFAC","OFAC CHECK COMPLETED PAYEE INFO NOT A MATCH TO WLE.");

    function addVendorComment(vendorCompany,vendor,locationCode,commentType,commentTitle,comment){

     /* This function will add a vendor comment. */

     /* build the API call for the writeattach cgi */
     s = portalWnd.WRITEATTACHPath + "?_OUT=XML&_PDL=" + strPDL;
     s += "&_FN=APCOMMENTS&_IN=APCSET1";
     s += "&K1=V";
     s += "&K2=" + vendorCompany;
     s += "&K3=" + vendor;
     s += "&K4=" + locationCode;
     s += "&K5=0000&_ATYP=C&_AUDT=" + commentType;
     s += "&_USCH=none&_DATA=TRUE&_OPM=M&_ECODE=TRUE";
     s += "&_ANAM=" + commentTitle;
     s += "&_ATXT=" + comment;

     alert("s=" + s);  // For debugging
     
     /* send API to the server */
     var addCmnt = portalWnd.httpRequest(s);
     alert("HTTP Request complete");  // For debugging

     if (!addCmnt || addCmnt.status)
     {
        // error retrieving file: display message
        var msg="Error adding " + commentTitle + " comment.";
        if (addCmnt)
           msg+=("\n"+portalWnd.getHttpStatusMsg(addCmnt.status));
        portalWnd.cmnDlg.messageBox(msg,"ok","stop",window);
        return;
     }

     alert("Checking error message"); // For debugging
     var addCmntErrMsg = addCmnt.selectSingleNode("//ErrMsg").text;
     alert("Error message: " + addCmntErrMsg); // For debugging

    }

    The last "alert" displays "Error message: Invalid value for User Defined Type: _AUDT=(D)."

    I opened an incident with Lawson support and pointed out the example in their documentation doesn't work and is incorrect but they told me I would need to contact professional services since it was a customization.  Apparently they don't care that their documentation is wrong.

    Has anyone been able to successfully generate a vendor comment programmatically or can you see any obvious errors with my code?

    Thanks,
    Randy

    Ragu Raghavan
    Veteran Member
    Posts: 468
    Veteran Member
      Based on the error message referring to _AUDT= D, can you try setting that to A? This is what I see in AP.sr (TYPE = A), While Invoice comments have A/N/D/C

      *********************************************************************
      * Attachments for Vendor Comment AP12
      *********************************************************************
      DEFINE OBJWIN "Vendor Comments"
      ID AP-APC-W-0001
      FILENAME APCOMMENTS
      COMMENT TYPE=A
      WINFLDS COMPANY: " Company:"
      VENDOR : " Vendor :"
      LOCATION-CODE: "Location Code:"
      Randy Jacoy
      Veteran Member
      Posts: 46
      Veteran Member
        Thank you Ragu.  I changed the code to an "A" and now I get "Parent Record Not Found". At least that is progress!  I will check my key fields and make sure I am formatting them properly.
        Randy Jacoy
        Veteran Member
        Posts: 46
        Veteran Member

          I've made some progress but it's not necessarily good news.  The "Parent Record not Found" message wasn't referring to the vendor record but rather the parent comment record.  I guess it makes sense.  When creating a vendor comment in AP12.1 you first create the comment record (the "Parent") and then click the "Comments" button next to the comment you just added and then add the detail.  When you create the parent comment a sequence number is generated.  When I placed that sequence number in the fifth key field (&K5=0001) of my writeattach call it was successful. 

          I'm not sure if it's possible to do what I want to do.  Unless I can create that parent record first and get the sequence number that was generated I'm out of luck.  Maybe an AGS call first.  I will play with it.

          Jason Beard
          Veteran Member
          Posts: 124
          Veteran Member

            In the example from the manual the comment is being added to the APINVOICE table.  The Vendor comments are actually attachments to the APCOMMENTS table rather than the APVENMAST table so yes you would need to do an ags/Transaction call to AP12 first and then the writeattachrec call.  

             

            make sense?

            Jason Beard
            617-548-5568
            jabeard3@gmail.com
            Randy Jacoy
            Veteran Member
            Posts: 46
            Veteran Member
              Jason,

              That does make sense. I'm unable to create the parent comment with the AGS call however. This is what I am using:

              var sAGSCall = portalWnd.AGSPath;
              sAGSCall += "?_PDL="+strPDL;
              sAGSCall += "&_TKN=AP12.1&_EVT=ADD&_LFN=ALL&_TDS=IGNORE&FC=A";
              sAGSCall += "&APC-COMPANY=" + vendorCompany;
              sAGSCall += "&APC-VENDOR=" + vendor;
              sAGSCall += "&APC-LOCATION-CODE="; // blank
              sAGSCall += "&APC-PAY-GROUP=" + payGroup; // 0001
              sAGSCall += "&APC-REC-TYPE=" + commentType; //D
              sAGSCall += "&_OUT=XML&_EOT=TRUE";
              var sAGSInfo = portalWnd.httpRequest(sAGSCall);
              if (!sAGSInfo || sAGSInfo.status > 400) { return; }
              var sMessage = sAGSInfo.selectSingleNode("//Message").text;
              var sStatusNbr = sAGSInfo.selectSingleNode("//StatusNbr").text
              alert("Message: "+ sMessage + ", StatusNbr: " + sStatusNbr);

              I get back a "Change Complete -- Continue" message however when I view comments, nothing was added. Even if I was able to successfully create the parent I would need the sequence number of the newly created parent comment because I need it to add the detail to the comment.
              Jason Beard
              Veteran Member
              Posts: 124
              Veteran Member

                AP12 is a little funky...  You need to also populate LINE-FCr0=A and APC-PRINT-CODEr0= whatever value you want for that field (A,D,C,N) depending on your needs.

                Then when you run your command it will return an xml document that will have the Sequence number populated.

                Also if you are going to be adding more comments for the same vendor it will get a little more complicated.

                Jason Beard
                617-548-5568
                jabeard3@gmail.com
                Jason Beard
                Veteran Member
                Posts: 124
                Veteran Member

                  AP12 is a little funky...  You need to also populate LINE-FCr0=A and APC-PRINT-CODEr0= whatever value you want for that field (A,D,C,N) depending on your needs.

                  Then when you run your command it will return an xml document that will have the Sequence number populated.

                  Also if you are going to be adding more comments for the same vendor it will get a little more complicated.

                  Jason Beard
                  617-548-5568
                  jabeard3@gmail.com
                  Randy Jacoy
                  Veteran Member
                  Posts: 46
                  Veteran Member

                    Jason,

                    Thank you for the insight!  I was able to get it working by adding the two fields you included.  I had to add a DME call to get the current number of comments so I could determine which line number to use in the line and print code fields.  Using 0 is fine if there are no other comments for that vendor but if the vendor already has comments you need to determine which detail line is next.  And of course if the number of existing comments is 14 or more (meaning you're no longer on the first page of comments) you have to keep subtracting 14 from that number until you have a number less than 14.  Once I got that figured out I was able to add a comment.  The sequence is as follows:

                     

                    1. Get the current number of comments using a DME call.

                    2. Add the parent comment using an AGS call to AP12.1.

                    3. Add detail to the parent comment using writeattachrec.

                     

                    It's a little convoluted but gets the job done.  Thanks again for your help!

                    ---