Anyone know how to Insert AR Invoice C# ACCPAC.Advantage

SOLVED

I can insert a customer, i can insert a Vendor, but when i try to insert an invoice into AR, i get object set to null errors lol no matter what i do. I've been doing permutations of the code in hopes of finding WHY the error happens, i tried their RVSpy tool to see what is going on, but that just says NOTHING.

My code is simple enough: 

bool temp = false;
ACCPAC.Advantage.View ARINVOICE1batch;
ACCPAC.Advantage.ViewFields ARINVOICE1batchFields;
ARINVOICE1batch = mDBLinkCmpRW.OpenView("AR0031");
ARINVOICE1batchFields = ARINVOICE1batch.Fields;


ACCPAC.Advantage.View ARINVOICE1header;
ACCPAC.Advantage.ViewFields ARINVOICE1headerFields;
ARINVOICE1header = mDBLinkCmpRW.OpenView("AR0032");
ARINVOICE1headerFields = ARINVOICE1header.Fields;

ACCPAC.Advantage.View ARINVOICE1detail1;
ACCPAC.Advantage.ViewFields ARINVOICE1detail1Fields;
ARINVOICE1detail1 = mDBLinkCmpRW.OpenView("AR0033");
ARINVOICE1detail1Fields = ARINVOICE1detail1.Fields;

ACCPAC.Advantage.View ARINVOICE1detail2;
ACCPAC.Advantage.ViewFields ARINVOICE1detail2Fields;
ARINVOICE1detail2 = mDBLinkCmpRW.OpenView("AR0034");
ARINVOICE1detail2Fields = ARINVOICE1detail2.Fields;


ACCPAC.Advantage.View ARINVOICE1detail3;
ACCPAC.Advantage.ViewFields ARINVOICE1detail3Fields;
ARINVOICE1detail3 = mDBLinkCmpRW.OpenView("AR0402");
ARINVOICE1detail3Fields = ARINVOICE1detail3.Fields;

ACCPAC.Advantage.View ARINVOICE1detail4;
ACCPAC.Advantage.ViewFields ARINVOICE1detail4Fields;
ARINVOICE1detail4 = mDBLinkCmpRW.OpenView("AR0401");
ARINVOICE1detail4Fields = ARINVOICE1detail4.Fields;


ACCPAC.Advantage.View[] V1 = new ACCPAC.Advantage.View[1] { ARINVOICE1header };
ARINVOICE1batch.Compose(V1);

ACCPAC.Advantage.View[] V2 = new ACCPAC.Advantage.View[4] { ARINVOICE1batch, ARINVOICE1detail1, ARINVOICE1detail2, ARINVOICE1detail3 };
ARINVOICE1header.Compose(V2);
ACCPAC.Advantage.View[] V3 = new ACCPAC.Advantage.View[3] { ARINVOICE1header, ARINVOICE1batch, ARINVOICE1detail4 };
ARINVOICE1detail1.Compose(V3);
ACCPAC.Advantage.View[] V4 = new ACCPAC.Advantage.View[1] { ARINVOICE1header };
ARINVOICE1detail2.Compose(V4);
ACCPAC.Advantage.View[] V5 = new ACCPAC.Advantage.View[1] { ARINVOICE1header };
ARINVOICE1detail3.Compose(V5);
ACCPAC.Advantage.View[] V6 = new ACCPAC.Advantage.View[1] { ARINVOICE1detail1 };
ARINVOICE1detail4.Compose(V6);

ARINVOICE1batch.Browse("((BTCHSTTS = 1) OR (BTCHSTTS = 7))", true);

ACCPAC.Advantage.View ARINVCPOST2;
ACCPAC.Advantage.ViewFields ARINVCPOST2Fields;
ARINVCPOST2 = mDBLinkCmpRW.OpenView("AR0048");
ARINVCPOST2Fields = ARINVCPOST2.Fields;

ARINVOICE1batch.RecordCreate(ViewRecordCreate.Insert);// 1
ARINVOICE1batchFields.FieldByName("PROCESSCMD").SetValue("1", false);
ARINVOICE1batch.Process();
ARINVOICE1batch.Read(false);
ARINVOICE1header.RecordCreate(ViewRecordCreate.DelayKey);// 2
ARINVOICE1detail1.Cancel();
ARINVOICE1headerFields.FieldByName("IDCUST").SetValue("C01045",false); //<---- *** up
ARINVOICE1headerFields.FieldByName("PROCESSCMD").SetValue("4", false);
ARINVOICE1header.Process();
ARINVOICE1headerFields.FieldByName("IDINVC").SetValue("DOC " + _CUSTID,false);
ARINVOICE1headerFields.FieldByName("INVCTYPE").SetValue("2",false);
temp = ARINVOICE1detail1.Exists;
ARINVOICE1detail1.RecordClear();
temp = ARINVOICE1detail1.Exists;
ARINVOICE1detail1.RecordCreate(ViewRecordCreate.NoInsert);// 0
ARINVOICE1detail1Fields.FieldByName("PROCESSCMD").SetValue("0", false);
ARINVOICE1detail1.Process();
ARINVOICE1detail1Fields.FieldByName("IDDIST").SetValue("11",false);
ARINVOICE1detail1Fields.FieldByName("AMTEXTN").SetValue("666.000",false);
ARINVOICE1detail1Fields.FieldByName("COMMENT").SetValue("20 doors", false);
ARINVOICE1detail1.Insert();
ARINVOICE1detail1Fields.FieldByName("CNTLINE").SetValue("-1", false);
ARINVOICE1detail1.Read(false);
ARINVOICE1headerFields.FieldByName("SPECINST").SetValue("instructions", false);
ARINVOICE1headerFields.FieldByName("ORDRNBR").SetValue("MY ORDER" + _CUSTID, false);
ARINVOICE1headerFields.FieldByName("CUSTPO").SetValue("MY PO" + _CUSTID, false);
ARINVOICE1headerFields.FieldByName("IDSHIPNBR").SetValue("MY SHIP NUMBER" + _CUSTID, false);
ARINVOICE1header.Insert();
ARINVOICE1detail1.Read(false);
ARINVOICE1detail1.Read(false);
ARINVOICE1batch.Read(false);
ARINVOICE1header.RecordCreate(ViewRecordCreate.DelayKey);// 2
ARINVOICE1detail1.Cancel();

  • 0

    You should look at the Session object's errors for the error messages that Sage is producing. The .Net environment won't receive the errors, only that an exception was reported.

    Is the batch being created or does that fail?

  • 0 in reply to Django

    it bombs on this line: 

    ARINVOICE1headerFields.FieldByName("IDCUST").SetValue("C01045",false); //<---- *** up

  • 0 in reply to BOB2023

    What do the Session object's error messages say?

  • 0 in reply to Django

    that session object has no errors, when i try to go through it, the Error count is 0 

  • 0 in reply to BOB2023

    Just to confirm - is the Invoice batch being created? I know that you're code is crashing when you're trying to populate the customer field but I want to confirm that the batch is being created.

    And when you debug, does:

    ARINVOICE1headerFields

    have what looks to be a valid assignment?

    And what about: ARINVOICE1headerFields.FieldByName("IDCUST")?

  • 0 in reply to Django

    When i go into the Sage software i know the first record is created or some sort of initial one is created because i pick it and opens up the Invoice, but there is no other data there.

    all objects are NOT null, i checked them all. 

    i checked the code 4 times, to see if i made a mix up, none there.

  • +1
    verified answer

    i figured this BS out, going from a macro that generated 150 lines to something that is 80% smaller, lol

    accpacSession.Open("SOME USER", "SOME PASSWORD", psCompany, DateTime.Today, 0);
    mDBLinkCmpRW = accpacSession.OpenDBLink(DBLinkType.Company, DBLinkFlags.ReadWrite);
    mDBLinkSysRW = accpacSession.OpenDBLink(DBLinkType.System, DBLinkFlags.ReadWrite);

    ACCPAC.Advantage.View arInvoiceBatch = mDBLinkCmpRW.OpenView("AR0031");
    ACCPAC.Advantage.View arInvoiceHeader = mDBLinkCmpRW.OpenView("AR0032");
    ACCPAC.Advantage.View arInvoiceDetail = mDBLinkCmpRW.OpenView("AR0033");
    ACCPAC.Advantage.View arInvoicePaymentSchedules = mDBLinkCmpRW.OpenView("AR0034");
    ACCPAC.Advantage.View arInvoiceHeaderOptFields = mDBLinkCmpRW.OpenView("AR0402");
    ACCPAC.Advantage.View arInvoiceDetailOptFields = mDBLinkCmpRW.OpenView("AR0401");

    arInvoiceBatch.Compose(new ACCPAC.Advantage.View[] { arInvoiceHeader });
    arInvoiceHeader.Compose(new ACCPAC.Advantage.View[] { arInvoiceBatch, arInvoiceDetail, arInvoicePaymentSchedules, arInvoiceHeaderOptFields });
    arInvoiceDetail.Compose(new ACCPAC.Advantage.View[] { arInvoiceHeader,arInvoiceBatch, arInvoiceDetailOptFields });
    arInvoicePaymentSchedules.Compose(new ACCPAC.Advantage.View[] {arInvoiceHeader });
    arInvoiceHeaderOptFields.Compose(new ACCPAC.Advantage.View[] { arInvoiceHeader });
    arInvoiceDetailOptFields.Compose(new ACCPAC.Advantage.View[] { arInvoiceDetail });

    arInvoiceBatch.Browse("((BTCHSTTS = 1) OR (BTCHSTTS = 7))", false);

    arInvoiceBatch.RecordCreate(ViewRecordCreate.Insert);
    arInvoiceBatch.Fields.FieldByName("PROCESSCMD").SetValue("1", false);
    arInvoiceBatch.Fields.FieldByName("BTCHDESC").SetValue("Batch " + _ID, false);
    arInvoiceBatch.Update();

    arInvoiceHeader.RecordCreate(ViewRecordCreate.DelayKey);
    arInvoiceHeader.Fields.FieldByName("PROCESSCMD").SetValue("4", false);
    arInvoiceHeader.Fields.FieldByName("IDCUST").SetValue("C01045", true);
    arInvoiceHeader.Fields.FieldByName("IDINVC").SetValue("11111" + _ID, false);
    arInvoiceHeader.Fields.FieldByName("INVCTYPE").SetValue("2", true);
    arInvoiceHeader.Fields.FieldByName("SPECINST").SetValue("5555", false);
    arInvoiceHeader.Fields.FieldByName("ORDRNBR").SetValue("3333" + _ID, false);
    arInvoiceHeader.Fields.FieldByName("CUSTPO").SetValue("2222" + _ID, false);
    arInvoiceHeader.Fields.FieldByName("INVCDESC").SetValue("6666666", false);
    arInvoiceHeader.Fields.FieldByName("IDSHIPNBR").SetValue("4444", false);

    arInvoiceDetail.RecordCreate(ViewRecordCreate.NoInsert);
    arInvoiceDetail.Fields.FieldByName("IDDIST").SetValue( "11", true);
    arInvoiceDetail.Fields.FieldByName("AMTEXTN").SetValue( "666.000", true);
    arInvoiceDetail.Fields.FieldByName("CNTLINE").SetValue("-1", false);
    arInvoiceDetail.Insert();
    arInvoiceHeader.Insert();