Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to add multiple entries to A/P batch?

Status
Not open for further replies.

pavelm

Programmer
Nov 4, 2009
21
UA
Hello,

do I need to create all views/compositions to add multiple entries to A/P batch?

I try to record a macro, and it creates a new views every time for every entry... I'm not sure it's an optimal solution, but when I'm trying just re-use already created views, it hangs up unexpectedly without an error.

Accpac 5.4 + C#.NET

Thank you
 
Thank you for response, but problem still exists.
Task: insert into A/P batch a new batch record, and multiple entries each with a single details record.
What I'm doing:

usual compositions, than:
ap0020.CNTBTCH.PutWithoutVerification(0);
ap0020.RecordClear();
ap0020.RecordGenerate(true);
ap0020.BTCHDESC.Value = batchDescription;
ap0020.Update();

foreach (CheckLineItem lineItem in file.LineItems)
{
//ap0021.CNTITEM.PutWithoutVerification(0);
//ap0021.RecordClear();
//ap0021.RecordGenerate(true);
ap0021.RecordCreate(ViewRecordCreate.DelayKey);

ap0021.IDVEND.Value = file.VendorCode;
ap0021.PROCESSCMD.PutWithoutVerification(7);
ap0021.INVCDESC.Value = lineItem.CaseFileNumber;
ap0021.Process();


ap0021.IDINVC.Value = lineItem.VendorInvoiceNumber;
// on next iteration I'm getting Accpac error here (after
// very long delay it returns from API call):
// Invoice
// Invalid field index: 999999993.

ap0021.AMTGROSTOT.Value = lineItem.Amount;
ap0021.DATEINVC.Value = lineItem.InvoiceDate;
ap0021.ORDRNBR.Value = lineItem.CaseFileNumber;
ap0021.INVCDESC.Value = lineItem.CaseFileNumber;

//ap0022.RecordGenerate(false);
//ap0022.CNTLINE.PutWithoutVerification(0);
ap0022.RecordCreate(ViewRecordCreate.DelayKey);

ap0022.IDDIST.Value = lineItem.BillingCode;
ap0022.AMTDIST.Value = lineItem.Amount;
ap0022.IDGLACCT.Value = lineItem.BillingCode;
ap0022.TAXCLASS1.Value = 1;

ap0022.Insert();

ap0021.Insert();

}

strange, but everything works well with a single cycle iteration, and error appears only on the second step...

I doubt, I skip somewhere something like
APINVOICE3detail1Fields("CNTLINE").PutWithoutVerification ("-1")
APINVOICE3detail1.Read
but I'm not sure where...

Please ignore strange syntax used above to access fields/views, I'm using my own library for convenience.

Do you know where could be a problem?
 
Here's a VBA snippet that runs flawlessly:

ApInvoiceHeader.RecordClear
ApInvoiceHeader.RecordGenerate False

ApInvoiceHeader.Fields("IDVEND") = recDetail.Fields(D_vendorId)
ApInvoiceHeader.Fields("DATEINVC").PutWithoutVerification (MakeDate(recOrder.Fields(H_creationTime)))
ApInvoiceHeader.Fields("IDINVC").PutWithoutVerification (recOrder.Fields(D_transactionId))
ApInvoiceHeader.Fields("DATEDUE") = GetTermsDate(recDetail.Fields(D_vendorId), MakeDate(recOrder.Fields(H_CheckInDate)), MakeDate(recOrder.Fields(H_CheckOutDate)), ApInvoiceHeader.Fields("DATEDUE"), ApInvoiceHeader.Fields("DATEINVC"))

AddAllAPHeaderOptionals recOrder, recDetail

Do While Not recDetail.EOF
If Val(recDetail.Fields(D_extendedCost)) <> 0 Then

ApInvoiceDetail1.GoBottom
iLine = ApInvoiceDetail1.Fields("CNTLINE")
ApInvoiceDetail1.RecordClear
ApInvoiceDetail1.Fields("CNTLINE").PutWithoutVerification (iLine)
ApInvoiceDetail1.RecordGenerate False
ApInvoiceDetail1.Fields("IDGLACCT").Value = GetCostAccount(recDetail.Fields(D_detailType)
If recDetail.Fields(D_detailDescription) <> "" Then ApInvoiceDetail1.Fields("TEXTDESC").Value = recDetail.Fields(D_detailDescription)
ApInvoiceDetail1.Fields("AMTDIST") = Abs(recDetail.Fields(D_extendedCost))
ApInvoiceDetail1.Insert

nDetailAmount = nDetailAmount + ApInvoiceDetail1.Fields("AMTDIST")
ApInvoiceHeader.Fields("AMTGROSTOT") = ApInvoiceHeader.Fields("AMTGROSTOT") + ApInvoiceDetail1.Fields("AMTDIST")
End If
recDetail.MoveNext
Loop

 
Your code inserts multiple ApInvoiceDetails, but a single AP Invoice Header. I need to insert multiple Invoice Headers...

I found, that field IDINVC (which I have problems with) belongs to second composite key: Vendor Number/Document Number: ORIGCOMP, IDVEND, IDINVC - may be it is the reason?

Basically, I'm a little shocked, because it looks like there is no rules of insertion - I used protocol, described in 2007_SDK-ProgrammingGuide.pdf from Accpac site - but it doesn't work:

Insertion protocol
The following insertion protocol must be followed for views
that are classified as batch/header/detail.
1. Put zero in the batch key.
2. Init batch to get the next available key for the batch.
3. Put the fields in the batch.
4. Verify the batch. (Optional.)
5. Update batch. (The batch record is already inserted by
the Init call, in step 2.)
6. Put zero in the header key.
7. Init header to get the next available key for the header.
The key for the batch in the header view is assumed to
be set already by the composition.
8. Put the fields in the header.
9. Init detail to initialize the fields.
10. Put zero into the detail key field to insert from the start.
11. Put values in the other detail fields.
12. Verify detail. (Optional.)
13. Insert detail.
14. Go to step 11 until no more detail.
15. Verify header. (Optional.)
16. Insert header. (This will do a Post of the details.)
17. Go back to step 6 to add another header.

looks, like others used generated VBA macro files as a reference... but I'm still playing with them trying to remove garbage and make them work...
 
also, I've noticed, that if I change in my code line

ap0021.IDINVC.Value = lineItem.VendorInvoiceNumber;

to

ap0021.IDINVC.PutWithoutVerification(lineItem.VendorInvoiceNumber);

I will have same problems during record insertion on last ap0021.Insert(); command
 
OK, fine, if you want multiple invoices, you simply need another loop.

do until {whatever condition}
ApInvoiceHeader.RecordClear
ApInvoiceHeader.RecordGenerate False
{Set the header fields
{insert details}
ApInvoiceHeader.Insert
Loop

It doesn't take any fancy programming.
 
Code below runs perfectly one-by-one - when I run it over single line item in a file, and hangs on IDINVC assignment if I'll group two SAME line items per file (of cause I'm restoring DB backup every time):


foreach (HBCheckLineItem lineItem in file.LineItems)
{
ap0021.CNTITEM.PutWithoutVerification(0); // 6. Put zero in the header key.
ap0021.RecordClear();
ap0021.RecordGenerate(true);// 7. Init header to get the next available key for the header.
// The key for the batch in the header view is assumed to be set already by the composition.

ap0021.IDVEND.Value = file.VendorCode; // 8. Put the fields in the header.
ap0021.DATEINVC.Value = lineItem.InvoiceDate;
ap0021.IDINVC.Value = lineItem.VendorInvoiceNumber; // Document Number
ap0021.AMTGROSTOT.Value = lineItem.Amount;
ap0021.IDINVC.PutWithoutVerification(lineItem.VendorInvoiceNumber); // Document Number
ap0021.ORDRNBR.Value = lineItem.CaseFileNumber; // Order Number
ap0021.INVCDESC.Value = lineItem.CaseFileNumber; // Invoice Description

ap0022.RecordClear();
ap0022.RecordGenerate(false);// 9. Init detail to initialize the fields.
ap0022.CNTLINE.PutWithoutVerification(0);// 10. Put zero into the detail key field to insert from the start.
ap0022.IDDIST.Value = lineItem.BillingCode; // Distribution Code
ap0022.AMTDIST.Value = lineItem.Amount; // Distributed Amount
ap0022.IDGLACCT.Value = lineItem.BillingCode; // 11. Put values in the other detail fields.
ap0022.TAXCLASS1.Value = 1;
ap0022.Insert();// // 13. Insert detail

ap0021.Insert();//16. Insert header. (This will do a Post of the details.)
}
 
Actually, I'm doing it. Below is the generated macro code, which has no problems.
My code uses same idea: generate header, generate details, insert details, insert header - move to the next header... but why it fails on IDINVC assigment?

I know it looks stupid and annoying, sorry... Do you have skype? My ID is pavel_maltsev, I can share my screen if you have time and interested...


APINVOICE1batch.Browse "((BTCHSTTS = 1) OR (BTCHSTTS = 7))", 1
temp = APINVOICE1batch.Exists
APINVOICE1batch.RecordCreate 1
APINVOICE1batch.Read
temp = APINVOICE1header.Exists
APINVOICE1header.RecordCreate 0
APINVOICE1detail1.Cancel
APINVOICE1batchFields("BTCHDESC").PutWithoutVerification ("batch description") ' Description
APINVOICE1batch.Update
APINVOICE1headerFields("IDVEND").Value = "1AH" ' Vendor Number

APINVOICE1headerFields("PROCESSCMD").PutWithoutVerification ("7") ' Process Command Code
APINVOICE1headerFields("INVCDESC").PutWithoutVerification ("entry description") ' Invoice Description

APINVOICE1header.Process

APINVOICE1headerFields("PROCESSCMD").PutWithoutVerification ("4") ' Process Command Code

APINVOICE1header.Process

APINVOICE1headerFields("IDINVC").Value = "DOCUMENT NUMBER" ' Document Number
APINVOICE1headerFields("AMTGROSTOT").Value = "1.000" ' Document Total Including Tax
APINVOICE1headerFields("DATEINVC").Value = DateSerial(2009, 11, 10) ' Invoice Date

APINVOICE1detail1Fields("CNTLINE").PutWithoutVerification ("-1") ' Line Number
APINVOICE1detail1.Read

APINVOICE1detail1Fields("IDDIST").Value = "1" ' Distribution Code

temp = APINVOICE1detail1.Exists

APINVOICE1detail1Fields("AMTDIST").Value = "1.000" ' Distributed Amount

APINVOICE1detail1.Update

APINVOICE1detail1Fields("CNTLINE").PutWithoutVerification ("-1") ' Line Number

APINVOICE1detail1.Read
APINVOICE1header.Insert
APINVOICE1batch.Read
temp = APINVOICE1header.Exists
APINVOICE1header.RecordCreate 0
APINVOICE1detail1.Cancel
APINVOICE1headerFields("IDVEND").Value = "2AH" ' Vendor Number

APINVOICE1headerFields("PROCESSCMD").PutWithoutVerification ("7") ' Process Command Code
APINVOICE1headerFields("INVCDESC").PutWithoutVerification ("entry description2") ' Invoice Description

APINVOICE1header.Process

APINVOICE1headerFields("PROCESSCMD").PutWithoutVerification ("4") ' Process Command Code

APINVOICE1header.Process

APINVOICE1headerFields("IDINVC").Value = "DOCUMENT NUMBER2" ' Document Number
APINVOICE1headerFields("AMTGROSTOT").Value = "12.000" ' Document Total Including Tax
APINVOICE1headerFields("DATEINVC").Value = DateSerial(2009, 11, 10) ' Invoice Date

APINVOICE1detail1Fields("CNTLINE").PutWithoutVerification ("-1") ' Line Number

APINVOICE1detail1Fields("CNTLINE").PutWithoutVerification ("-1") ' Line Number

APINVOICE1detail1.Read

APINVOICE1detail1Fields("IDDIST").Value = "1" ' Distribution Code

temp = APINVOICE1detail1.Exists

APINVOICE1detail1Fields("AMTDIST").Value = "12.000" ' Distributed Amount

APINVOICE1detail1.Update

APINVOICE1detail1Fields("CNTLINE").PutWithoutVerification ("-1") ' Line Number

APINVOICE1detail1.Read
APINVOICE1header.Insert
APINVOICE1batch.Read
temp = APINVOICE1header.Exists
APINVOICE1header.RecordCreate 0
APINVOICE1detail1.Cancel


 
Looks, like there is an error somewhere in Accpac C#.NET library or so... in any case cleaned-up VB script works without any problems from Accpac, while absolutely same C# code have problems with IDINVC assignment...
 
Code below runs successfully on C#, it's almost direct translation from VB script. Strange, but it doesn't work if I'm using my lib with view names/fields shortcuts, which seems to work well before... May be bug is there.

Thank you, tuba, I'll try to find it myself.

using System;
using ACCPAC.Advantage;
using AIS.CommonClasses;
using AIS.CommonClasses.Exceptions;
using AIS.Datalayer.AccpacViews;
using AIS.DataLayer.AccpacViews.Bank;

namespace Accpac54ConsoleTest
{
class Program
{
static void Main(string[] args)
{
Session sessionMain;

DBLink dbLinkMain;
sessionMain = new Session();
sessionMain.Init("", "CS", "CS0001", "54A");

sessionMain.Open("ADMIN", "SUPER", "EMPTY2", DateTime.Now, 0);
dbLinkMain = sessionMain.OpenDBLink(DBLinkType.Company, DBLinkFlags.ReadWrite);

Console.WriteLine("Start");

try
{
string time = DateTime.Now.ToString("HHmmss");

View ap0020 = dbLinkMain.OpenView("AP0020");
ViewFields ap0020f = ap0020.Fields;

View ap0021 = dbLinkMain.OpenView("AP0021");
ViewFields ap0021f = ap0021.Fields;

View ap0021o = dbLinkMain.OpenView("AP0402");
ViewFields ap0021of = ap0021o.Fields;

View ap0022 = dbLinkMain.OpenView("AP0022");
ViewFields ap0022f = ap0022.Fields;

View ap0022o = dbLinkMain.OpenView("AP0401");
ViewFields ap0022of = ap0022o.Fields;

View ap0023 = dbLinkMain.OpenView("AP0023");
ViewFields ap0023f = ap0023.Fields;

ap0020.Compose(new View[] { ap0021 });
ap0021.Compose(new View[] { ap0020, ap0022, ap0023, ap0021o });

ap0022.Compose(new View[] { ap0021, ap0020 });
ap0022.Compose(new View[] { ap0021, ap0020, ap0022o });
ap0023.Compose(new View[] { ap0021 });
ap0021o.Compose(new View[] { ap0021 });
ap0022o.Compose(new View[] { ap0022 });


ap0020.Browse("((BTCHSTTS = 1) OR (BTCHSTTS = 7))",true);
ap0020.RecordCreate(ACCPAC.Advantage.ViewRecordCreate.Insert);
ap0020.Read(false);
ap0020f.FieldByName("BTCHDESC").SetValue("batch " + time, false);
ap0020.Update();

for (int i = 0; i < 2; i++)
{
ap0021.RecordCreate(ACCPAC.Advantage.ViewRecordCreate.NoInsert);
ap0022.Cancel();
ap0021f.FieldByName("IDVEND").SetValue("1AH", true);
ap0021f.FieldByName("PROCESSCMD").SetValue("7", false);
ap0021f.FieldByName("INVCDESC").SetValue("entry description"+i.ToString(), false);
ap0021.Process();
ap0021f.FieldByName("PROCESSCMD").SetValue("4", false);
ap0021.Process();

ap0021f.FieldByName("IDINVC").SetValue("VenInvN" + time + "_" + i.ToString(), true); // Document Number
ap0021f.FieldByName("AMTGROSTOT").SetValue(1, true);
ap0021f.FieldByName("DATEINVC").SetValue(DateTime.Now, true);

ap0022f.FieldByName("CNTLINE").SetValue(-1, false);
ap0022.Read(false);
ap0022f.FieldByName("IDDIST").SetValue("1", true);
ap0022f.FieldByName("AMTDIST").SetValue(1, true);
ap0022f.FieldByName("IDGLACCT").SetValue("30300", true);
ap0022f.FieldByName("TAXCLASS1").SetValue(1, true);
ap0022.Update();

ap0021.Insert();
}

Console.WriteLine("Sucessfull...");
Console.ReadLine();
}
catch (Exception ex)
{
for (int i=0; i<dbLinkMain.Parent.Errors.Count; i++)
{
Console.WriteLine(dbLinkMain.Parent.Errors.Message);
}
Console.WriteLine(ex.ToString());
}
}
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top