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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C# COMAPI Create Cashbook batch with entry type Accounts Payable

Status
Not open for further replies.

Ilia Deviatov

Programmer
Mar 27, 2024
2
ZM
I am using COMAPI to create a cashbook batch. When creating a normal cashbook batch (I.e. entry type =0) everything works fine. For entry type =1 i am getting an error "An Accounts Payable entry must be fully applied".

I recorded a marco and tried to convert it to C#

original macro

Code:
Sub MainSub()
'
' Sage 300 Macro file: C:\A4W\MACROS\ap_cashbook_apply_entryavb.avb
' Recorded at: Wed Mar 27 10:44:59 2024
'

On Error GoTo ACCPACErrorHandler

' TODO: To increase efficiency, comment out any unused DB links.
Dim mDBLinkCmpRW As AccpacCOMAPI.AccpacDBLink
Set mDBLinkCmpRW = OpenDBLink(DBLINK_COMPANY, DBLINK_FLG_READWRITE)

Dim mDBLinkSysRW As AccpacCOMAPI.AccpacDBLink
Set mDBLinkSysRW = OpenDBLink(DBLINK_SYSTEM, DBLINK_FLG_READWRITE)

Dim temp As Boolean
Dim CBBTCH2batch As AccpacCOMAPI.AccpacView
Dim CBBTCH2batchFields As AccpacCOMAPI.AccpacViewFields
mDBLinkCmpRW.OpenView "CB0009", CBBTCH2batch
Set CBBTCH2batchFields = CBBTCH2batch.Fields

Dim CBBTCH2header As AccpacCOMAPI.AccpacView
Dim CBBTCH2headerFields As AccpacCOMAPI.AccpacViewFields
mDBLinkCmpRW.OpenView "CB0010", CBBTCH2header
Set CBBTCH2headerFields = CBBTCH2header.Fields

Dim CBBTCH2detail1 As AccpacCOMAPI.AccpacView
Dim CBBTCH2detail1Fields As AccpacCOMAPI.AccpacViewFields
mDBLinkCmpRW.OpenView "CB0011", CBBTCH2detail1
Set CBBTCH2detail1Fields = CBBTCH2detail1.Fields

Dim CBBTCH2detail2 As AccpacCOMAPI.AccpacView
Dim CBBTCH2detail2Fields As AccpacCOMAPI.AccpacViewFields
mDBLinkCmpRW.OpenView "CB0012", CBBTCH2detail2
Set CBBTCH2detail2Fields = CBBTCH2detail2.Fields

Dim CBBTCH2detail3 As AccpacCOMAPI.AccpacView
Dim CBBTCH2detail3Fields As AccpacCOMAPI.AccpacViewFields
mDBLinkCmpRW.OpenView "CB0013", CBBTCH2detail3
Set CBBTCH2detail3Fields = CBBTCH2detail3.Fields

Dim CBBTCH2detail4 As AccpacCOMAPI.AccpacView
Dim CBBTCH2detail4Fields As AccpacCOMAPI.AccpacViewFields
mDBLinkCmpRW.OpenView "CB0014", CBBTCH2detail4
Set CBBTCH2detail4Fields = CBBTCH2detail4.Fields

Dim CBBTCH2detail5 As AccpacCOMAPI.AccpacView
Dim CBBTCH2detail5Fields As AccpacCOMAPI.AccpacViewFields
mDBLinkCmpRW.OpenView "CB0015", CBBTCH2detail5
Set CBBTCH2detail5Fields = CBBTCH2detail5.Fields

Dim CBBTCH2detail6 As AccpacCOMAPI.AccpacView
Dim CBBTCH2detail6Fields As AccpacCOMAPI.AccpacViewFields
mDBLinkCmpRW.OpenView "CB0016", CBBTCH2detail6
Set CBBTCH2detail6Fields = CBBTCH2detail6.Fields

Dim CBBTCH2detail7 As AccpacCOMAPI.AccpacView
Dim CBBTCH2detail7Fields As AccpacCOMAPI.AccpacViewFields
mDBLinkCmpRW.OpenView "CB0403", CBBTCH2detail7
Set CBBTCH2detail7Fields = CBBTCH2detail7.Fields

Dim CBBTCH2detail8 As AccpacCOMAPI.AccpacView
Dim CBBTCH2detail8Fields As AccpacCOMAPI.AccpacViewFields
mDBLinkCmpRW.OpenView "CB0404", CBBTCH2detail8
Set CBBTCH2detail8Fields = CBBTCH2detail8.Fields

CBBTCH2batch.Compose Array(CBBTCH2header)

CBBTCH2header.Compose Array(CBBTCH2batch, CBBTCH2detail1, CBBTCH2detail4, CBBTCH2detail8)

CBBTCH2detail1.Compose Array(CBBTCH2header, CBBTCH2detail2, CBBTCH2detail5, CBBTCH2detail7)

CBBTCH2detail2.Compose Array(CBBTCH2detail1, CBBTCH2detail3, CBBTCH2detail6)

CBBTCH2detail3.Compose Array(CBBTCH2detail2)

CBBTCH2detail4.Compose Array(CBBTCH2header)

CBBTCH2detail5.Compose Array(CBBTCH2detail1)

CBBTCH2detail6.Compose Array(CBBTCH2detail2)

CBBTCH2detail7.Compose Array(CBBTCH2detail1)

CBBTCH2detail8.Compose Array(CBBTCH2header)


CBBTCH2header.Init

CBBTCH2headerFields("PROCESSCMD").Value = "0"                         ' Process Command

CBBTCH2header.Process
CBBTCH2detail4.Init
temp = CBBTCH2detail1.Exists
temp = CBBTCH2header.Exists
CBBTCH2header.RecordClear

CBBTCH2headerFields("PROCESSCMD").Value = "0"                         ' Process Command

CBBTCH2header.Process
CBBTCH2detail1.FilterSelect "(BATCHID = ""000577"" AND ENTRYNO = ""00002"")", True, 0, 0
CBBTCH2detail1.Order = 0
CBBTCH2header.Init

CBBTCH2headerFields("PROCESSCMD").Value = "0"                         ' Process Command

CBBTCH2header.Process

CBBTCH2headerFields("ENTRYTYPE").Value = "1"                          ' Entry Type

CBBTCH2headerFields("MISCCODE").Value = "930"                         ' Miscellaneous Code
temp = CBBTCH2detail1.Exists
temp = CBBTCH2detail1.Exists
CBBTCH2detail1.RecordClear
temp = CBBTCH2detail1.Exists
CBBTCH2detail1.RecordCreate 0
CBBTCH2detail1.Process

CBBTCH2detail1Fields("SRCECODE").Value = "TR"                         ' Source Code
CBBTCH2detail1Fields("CREDITAMT").Value = "250.000"                   ' Credit Amount

CBBTCH2detail1.Insert

CBBTCH2detail1Fields("DETAILNO").PutWithoutVerification ("-000000001")   ' Detail Number

CBBTCH2detail1.Read
CBBTCH2detail3Fields("CBBTHDRVH").Value = "259443604"                 ' Bthd RVW

CBBTCH2detail3Fields("CBBTHDVW").Value = "259443604"                  ' Bthd VW

CBBTCH2detail3.Process
CBBTCH2detail1Fields("ALLOCMODE").Value = "1"                         ' Allocation Mode
temp = CBBTCH2detail1.Exists
CBBTCH2detail1.Update
CBBTCH2header.Insert

Exit Sub

ACCPACErrorHandler:
  Dim lCount As Long
  Dim lIndex As Long

  If Errors Is Nothing Then
       MsgBox Err.Description
  Else
      lCount = Errors.Count

      If lCount = 0 Then
          MsgBox Err.Description
      Else
          For lIndex = 0 To lCount - 1
              MsgBox Errors.Item(lIndex)
          Next
          Errors.Clear
      End If
      Resume Next

  End If

End Sub

My C#

Code:
view.BatchView.RecordCreate(tagViewRecordCreateEnum.VIEW_RECORD_CREATE_INSERT);
view.BatchView.Fields.FieldByName["BANKCODE"].set_Value(model.BankCode);
view.BatchView.Fields.FieldByName["TEXTDESC"].set_Value(model.BatchName);
view.BatchView.Update();

foreach (ApCashbookBatchHeaderEntryModel header in model.Headers)
{
    // create header
    view.HeaderView.RecordCreate(tagViewRecordCreateEnum.VIEW_RECORD_CREATE_NOINSERT);
    view.HeaderView.Fields.FieldByName["DATE"].set_Value(header.EntryDate);
    view.HeaderView.Fields.FieldByName["ENTRYTYPE"].set_Value("1"); // AP
    view.HeaderView.Fields.FieldByName["MISCCODE"].set_Value(header.MiscCode);

    view.HeaderView.Fields.FieldByName["BT2GLCURSR"].set_Value(header.Currency);
    view.HeaderView.Fields.FieldByName["BT2GLRATE"].set_Value(header.ExchangeRate);
    view.HeaderView.Fields.FieldByName["BK2GLRATE"].set_Value(header.ExchangeRate);

    view.HeaderView.Fields.FieldByName["REFERENCE"].set_Value(header.ReferenceNo);
    view.HeaderView.Fields.FieldByName["TEXTDESC"].set_Value(header.Description);
    view.HeaderView.Process();

    // header optional fields

    view.HeaderOptFieldsView.RecordCreate(tagViewRecordCreateEnum.VIEW_RECORD_CREATE_NOINSERT);
    view.HeaderOptFieldsView.Fields.FieldByName["OPTFIELD"].set_Value("CREDITCODE");
    view.HeaderOptFieldsView.Fields.FieldByName["SWSET"].set_Value("1");    // Value set
    view.HeaderOptFieldsView.Fields.FieldByName["VALIFTEXT"].set_Value(header.CreditCode);
    view.HeaderOptFieldsView.Insert();

    foreach (CashbookBatchEntryDetailModel detail in header.Details)
    {
        view.DetailView.RecordCreate(tagViewRecordCreateEnum.VIEW_RECORD_CREATE_NOINSERT);
        view.DetailView.Fields.FieldByName["SRCECODE"].set_Value(detail.SourceCode);
        view.DetailView.Fields.FieldByName["TEXTDESC"].set_Value(detail.Description);

        if (detail.DebitAmount.HasValue)
        {
            view.DetailView.Fields.FieldByName["DEBITAMT"].set_Value(detail.DebitAmount.Value);
        }

        if (detail.CreditAmount.HasValue)
        {
            view.DetailView.Fields.FieldByName["CREDITAMT"].set_Value(detail.CreditAmount.Value);
        }

        view.DetailView.Insert();
        view.DetailView.Read();

        view.DetailAdjustmentView.Process();

        view.DetailView.Fields.FieldByName["ALLOCMODE"].set_Value("1"); //Allocation mode = prepayment
        view.DetailView.Update();

        view.HeaderView.Insert();
        view.BatchView.Update();

        view.DetailView.RecordClear();
    }

    view.HeaderView.RecordClear();
}

I see in the macro two fields are set:

CBBTCH2detail3Fields("CBBTHDRVH").Value = "259443604" ' Bthd RVW

CBBTCH2detail3Fields("CBBTHDVW").Value = "259443604" ' Bthd VW

These are set on the Adjustment Detail View, but i dont know how they are calculated and if they are the cause of my problem.

Any ideas?
 
Managed to figure it out

For some reason the macro did not capture correct views, after looking at the excel export i managed to apply the full amount using sub detail view

Code:
                        view.SubDetailView.RecordCreate(tagViewRecordCreateEnum.VIEW_RECORD_CREATE_NOINSERT);
                        view.SubDetailView.Fields.FieldByName["DOCNUMBER"].set_Value("0");
                        view.SubDetailView.Fields.FieldByName["PAYNUMBER"].set_Value(1);
                        view.SubDetailView.Fields.FieldByName["DOCTYPE"].set_Value(0);
                        view.SubDetailView.Fields.FieldByName["APPLAMOUNT"].set_Value(amount);
                        view.SubDetailView.Fields.FieldByName["IDCUST"].set_Value(header.MiscCode);
                        view.SubDetailView.Fields.FieldByName["ENTRYTYPE"].set_Value("1");
                        view.SubDetailView.Process();
                        view.SubDetailView.Insert();

And the adjustment view changes were not required
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top