C# PO Write Fails BOI

SOLVED

First, thanks in advance for taking a look at this.  I'm looking at doing PO creation from C# and have been able to connect, get the next PO number etc., but everytime I try to finalize the PO, such as call the "nWrite" command, it always fails.  Any insight into what could be happening?  Below is my code.

using (DispatchObject poObject = new DispatchObject(pvx.InvokeMethod("NewObject", "PO_PurchaseOrder_bus", oSS.GetObject())))
{
try
{
string poNumber = "";
//int test1 = (int)poObject.InvokeMethod("nGetNextPurchaseOrderNo");
object[] poPass = new object[] {poNumber};
successCheck((int)poObject.InvokeMethodByRef("nGetNextPurchaseOrderNo", poPass), "GetNextPurchaseOrder");

//this was just to see what the PO number was coming back.
poNumber = poPass[0].ToString();

//we continue
successCheck((int) poObject.InvokeMethodByRef("nSetKey", poPass), "SetKey");
object[] divisionNoObject = new object[] {"APDivisionNo$", "00"};
successCheck((int) poObject.InvokeMethodByRef("nSetValue", divisionNoObject), "SetDivisionNo");
object[] vendorObject = new object[] { "VendorNo$", "0006630" };
successCheck((int)poObject.InvokeMethodByRef("nSetValue", vendorObject), "SetVendorNo");
object[] taxObject = new object[] { "TaxSchedule$", "NONTAX" };
successCheck((int)poObject.InvokeMethodByRef("nSetValue", taxObject), "SetTaxSchedule");

DispatchObject poLines = new DispatchObject(poObject.GetProperty("oLines"));
successCheck((int)poLines.InvokeMethod("nAddLine"), "AddLine");
object[] firstLine = new object[] { "ItemCode$", "/1." };
successCheck((int)poLines.InvokeMethodByRef("nSetValue", firstLine), "PoLineItemCode");
firstLine[0] = "QuantityOrdered";
firstLine[1] = "1";
successCheck((int)poLines.InvokeMethodByRef("nSetValue", firstLine), "PoLineQuantityOrdered");
successCheck((int)poLines.InvokeMethod("nWrite"), "PoLinesWrite");
successCheck((int) poObject.InvokeMethod("nWrite"), "Write");

MessageBox.Show("SUCCESS! " + poNumber.ToString());

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

}

And below is my successCheck method to see what fails.

public void successCheck(int checkValue, string call)
{
if (checkValue == 0)
{
MessageBox.Show("Error! " + call);

}

}

Parents
  • 0
    I would call sLastErrorMsg after the method that is failing on poObject. If it is falling into your catch before you can get the last error from poObject, then I would check the sLastErrorMsg on your oSS object. I just created an integration that was falling into an exception on nWrite, so I couldn't grab the last error. It turned out that the error was actually happening on my session object.
Reply
  • 0
    I would call sLastErrorMsg after the method that is failing on poObject. If it is falling into your catch before you can get the last error from poObject, then I would check the sLastErrorMsg on your oSS object. I just created an integration that was falling into an exception on nWrite, so I couldn't grab the last error. It turned out that the error was actually happening on my session object.
Children
  • 0 in reply to willHyland

    Checking the sLastErrorMsg property is always a good idea!

    Luckily, if you are using the DispatchObject object wrapper that Sage provided, it includes a property indexer that makes it easy to do:
    var lastError = poObject["sLastErrorMsg"];

    That said, I got your code to work in my test project pretty much as is...
    I fixed the nSetKey line to pass the string poNumber, vs. the object array named poPass.
    The other thing I changed was the usage of InvokeMethodByRef to InvokeMethod, in a few places, where parameters are passed by value, not by ref.
    Not sure if this caused any problem, but ByRef is more overhead so I only use it when needed.
    Also I changed the vendor to 01-AIRWAY and the item code to "6655" to work with my ABX company, but everything else should fly.

    Thanks!
    Bret.

    string poNumber = "";
    //int test1 = (int)poObject.InvokeMethod("nGetNextPurchaseOrderNo");
    object[] poPass = new object[] { poNumber };
    successCheck((int)poObject.InvokeMethodByRef("nGetNextPurchaseOrderNo", poPass), "GetNextPurchaseOrder");

    //this was just to see what the PO number was coming back.
    poNumber = poPass[0].ToString();

    //we continue
    successCheck((int)poObject.InvokeMethod("nSetKey", poNumber), "SetKey");
    object[] divisionNoObject = new object[] { "APDivisionNo$", "01" };
    successCheck((int)poObject.InvokeMethod("nSetValue", divisionNoObject), "SetDivisionNo");
    object[] vendorObject = new object[] { "VendorNo$", "AIRWAY" };
    successCheck((int)poObject.InvokeMethod("nSetValue", vendorObject), "SetVendorNo");
    object[] taxObject = new object[] { "TaxSchedule$", "NONTAX" };
    successCheck((int)poObject.InvokeMethod("nSetValue", taxObject), "SetTaxSchedule");

    DispatchObject poLines = new DispatchObject(poObject.GetProperty("oLines"));
    successCheck((int)poLines.InvokeMethod("nAddLine"), "AddLine");
    //object[] firstLine = new object[] { "ItemCode$", "/1." };
    object[] firstLine = new object[] { "ItemCode$", "6655" };
    successCheck((int)poLines.InvokeMethod("nSetValue", firstLine), "PoLineItemCode");
    firstLine[0] = "QuantityOrdered";
    firstLine[1] = "1";
    successCheck((int)poLines.InvokeMethod("nSetValue", firstLine), "PoLineQuantityOrdered");
    successCheck((int)poLines.InvokeMethod("nWrite"), "PoLinesWrite");
    successCheck((int)poObject.InvokeMethod("nWrite"), "Write");

    Console.WriteLine("SUCCESS! " + poNumber.ToString());

  • 0 in reply to Bret
    Thank you very much for taking a look at this. With a little more tinkering, and some input from my accounting folks I was able to get everything to work. It turns out I was not passing the P.O. line enough information. I had to pass a unit cost, and an account number for the lines to write, then once I had at least one line, the po itself would write. I found this out by invoking the sLastErrorMsg function as described above. Thanks all for your help! Below is what I ended up with for future reference:

    using (DispatchObject poObject = new DispatchObject(pvx.InvokeMethod("NewObject", "PO_PurchaseOrder_bus", oSS.GetObject())))
    {
    try
    {
    string poNumber = "";
    //int test1 = (int)poObject.InvokeMethod("nGetNextPurchaseOrderNo");
    object[] poPass = new object[] {poNumber};
    successCheck((int)poObject.InvokeMethodByRef("nGetNextPurchaseOrderNo", poPass), "GetNextPurchaseOrder");
    poNumber = poPass[0].ToString();

    successCheck((int) poObject.InvokeMethodByRef("nSetKey", poPass), "SetKey");
    object[] divisionNoObject = new object[] {"APDivisionNo$", "00"};
    successCheck((int) poObject.InvokeMethodByRef("nSetValue", divisionNoObject), "SetDivisionNo");
    object[] vendorObject = new object[] { "VendorNo$", "0006630" };
    successCheck((int)poObject.InvokeMethodByRef("nSetValue", vendorObject), "SetVendorNo");
    object[] taxObject = new object[] { "TaxSchedule$", "NONTAX" };
    successCheck((int)poObject.InvokeMethodByRef("nSetValue", taxObject), "SetTaxSchedule");

    DispatchObject poLines = new DispatchObject(poObject.GetProperty("oLines"));
    successCheck((int)poLines.InvokeMethod("nAddLine"), "AddLine");
    object[] firstLine = new object[] { "ItemCode$", "/1." };
    successCheck((int)poLines.InvokeMethodByRef("nSetValue", firstLine), "PoLineItemCode");
    firstLine[0] = "QuantityOrdered";
    firstLine[1] = 1;
    successCheck((int)poLines.InvokeMethodByRef("nSetValue", firstLine), "PoLineQuantityOrdered");
    firstLine[0] = "UnitCost";
    firstLine[1] = "1";
    successCheck((int)poLines.InvokeMethodByRef("nSetValue", firstLine), "PoLineUnitCost");
    firstLine[0] = "PurchasesAcctKey$";
    firstLine[1] = "120000200";
    successCheck((int)poLines.InvokeMethodByRef("nSetValue", firstLine), "PoLinePurchasesAcctKey");
    firstLine[0] = "CommentText$";
    firstLine[1] = "An Item";
    successCheck((int)poLines.InvokeMethodByRef("nSetValue", firstLine), "PoLineCommentText");
    successCheck((int)poLines.InvokeMethod("nWrite"), "PoLinesWrite");
    poObject.InvokeMethod("nWrite");
    //MessageBox.Show(poObject.InvokeMethod("sLastErrorMsg").ToString());

    MessageBox.Show("SUCCESS! " + poNumber.ToString());

    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message);
    }

    }