project QadFinancials > class BCInvoice > method ApiStdMaintainTTV01

Description

This method can create new instances or update existing instances based on the content of the input temp-table. The action that is passed as a parameter will define if the records are saved, saved as draft or just validated.

See method ApiStdMaintainTTInitialDefaulting for all further details
If the creditor invoice can be found (based on his logical key : Company ID, posting year, journal code, voucher), the existing creditor invoice will be updated. If not, a new creditor invoice will be created.

NEW METHOD : tApiCInvoiceWHT is added, needed for backwards compatibility


Parameters


tApiCInvoiceinputtemp-table
tApiCInvoiceBankinputtemp-table
tApiCInvoiceBankPayCodeinputtemp-table
tApiCInvoiceMovementinputtemp-table
tApiCInvoicePOinputtemp-table
tApiCInvoicePostinginputtemp-table
tApiCInvoiceVatinputtemp-table
tApiCInvoiceStageinputtemp-table
tApiCInvoiceWHTinputtemp-table
icActioninputcharacter
ocLstPrimKeyoutputcharacter
ocLstReturnoutputcharacter
ocLstRowidoutputcharacter
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BCInvoice.ApiStdMaintainTT
method BCInvoice.MaintainByDataset
method BCInvoice.MaintainByDatasetWithOutput
method BCInvoiceJournalEntry.ApiStdMaintainTTV01


program code (program9/bcinvoice.p)

/* ============================================================================ */
/* Clear the instance:                                                          */
/* this will also clear the t_s tables that were filled in ApiMaintainByDataSet */
/* ============================================================================ */
<M-55 run ClearData  (output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
if viFcReturnSuper <> 0
then assign oiReturnStatus = viFcReturnSuper.
if oiReturnStatus < 0
then return.

/* ==================== */
/* Set Default Return   */
/* Parameter Validation */
/* ==================== */
assign oiReturnStatus         = -98
       vlDraftInstanceCreated = false.
if icAction = ? then assign icAction = "":U.
if icAction <> {&DAEMONACTION-SAVESTORE} and 
   icAction <> {&DAEMONACTION-SAVE}      and 
   icAction <> {&DAEMONACTION-STORE}     and 
   icAction <> {&DAEMONACTION-VALIDATE}
then do:
    assign vcMessage      = trim(subst(#T-22'A supplier invoice integration error occurred. The specified action (&1) must be in the list '&2'.':255(14373)T-22#, icAction, "{&DAEMONACTION-SAVESTORE},{&DAEMONACTION-SAVE},{&DAEMONACTION-STORE},{&DAEMONACTION-VALIDATE}":U))
           oiReturnStatus = -1.
    <M-53 run SetMessage
       (input  vcMessage (icMessage), 
        input  '':U (icArguments), 
        input  '':U (icFieldName), 
        input  '':U (icFieldValue), 
        input  'E':U (icType), 
        input  3 (iiSeverity), 
        input  '':U (icRowid), 
        input  'qadfin-586088':U (icFcMsgNumber), 
        input  '' (icFcExplanation), 
        input  '' (icFcIdentification), 
        input  '' (icFcContext), 
        output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
    return.
end. /* if icAction */

/* ============================================================== */
/* Call method that can extend the tApi-tables with some defaults */
/* ============================================================== */
<M-97 run ApiStdMaintainTTInitialDefaulting  (output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
if viFcReturnSuper <> 0
then assign oiReturnStatus = viFcReturnSuper.
if viFcReturnSuper < 0
then return.

for each tApiCInvoice no-lock:
    <Q-607 run PaymentConditionByIdCode (all) (Read) (NoCache)
       (input ?, (PaymentConditionId)
        input tApiCInvoice.tcNormalPaymentConditionCode, (PaymentConditionCode)
        output dataset tqPaymentConditionByIdCode) in BPaymentCondition>
    
    find first tqPaymentConditionByIdCode where
               tqPaymentConditionByIdCode.tcPaymentConditionCode = tApiCInvoice.tcNormalPaymentConditionCode
    no-lock no-error.
    if available tqPaymentConditionByIdCode 
    then do:
        
        for each paymentconditionstaged where 
                paymentconditionstaged.PaymentCondition_ID = tqPaymentConditionByIdCode.tiPaymentCondition_Id 
        no-lock,      
        each paymentcondition where 
             paymentcondition.paymentcondition_id = paymentconditionstaged.stagedpaymentcondition_id 
        no-lock:
            if not can-find (tApiCInvoiceStage where
                tApiCInvoiceStage.tcPaymentConditionCode = PaymentCondition.PaymentConditionCode and
                tApiCInvoiceStage.tc_ParentRowid = tApiCInvoice.tc_Rowid
            no-lock)
            then do :
                assign vcMessage      = trim(subst(#T-17'The input Credit Term is incorrect.':255(501952263)T-17#))
                       oiReturnStatus = -1.
                   
                <M-64 run SetMessage
                   (input  vcMessage (icMessage), 
                    input  '':U (icArguments), 
                    input  '':U (icFieldName), 
                    input  '':U (icFieldValue), 
                    input  'E':U (icType), 
                    input  3 (iiSeverity), 
                    input  '':U (icRowid), 
                    input  'qadfin-338774':U (icFcMsgNumber), 
                    input  '':U (icFcExplanation), 
                    input  '':U (icFcIdentification), 
                    input  '':U (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
                return.
            end. /*if not  can-find (tApiCInvoiceStage where*/
        end. /*for each paymentconditionstaged where*/
    end. /*if available tqPaymentConditionByCode */
end. /*for each tApiCInvoice no-lock:*/

/* ======================== */
/* Count main-table records */
/* ======================== */
for each tApiCInvoice:
    assign viMainTableRecordCount = viMainTableRecordCount + 1.
end. /* for each tApiCInvoice */
if viMainTableRecordCount = 0
then do:
    assign vcMessage      = trim(#T-10'The supplier invoice was created using an API method.':200(1216)T-10#)
           oiReturnStatus = -1.
    <M-71 run SetMessage
       (input  vcMessage (icMessage), 
        input  '':U (icArguments), 
        input  '':U (icFieldName), 
        input  '':U (icFieldValue), 
        input  'E':U (icType), 
        input  3 (iiSeverity), 
        input  '':U (icRowid), 
        input  'qadfin-310442':U (icFcMsgNumber), 
        input  '' (icFcExplanation), 
        input  '' (icFcIdentification), 
        input  '' (icFcContext), 
        output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
    return.
end. /* if viMainTableRecordCount = 0 */

/* ================================================================================ */
/* Start and open journal entry (in case the caller already set viBJournalEntryCIID */
/* before calling this method, then this will result in just an Open of BJE)        */
/* ================================================================================ */
if viBJournalEntryCIID = 0 or viBJournalEntryCIID = ?
then do:
    <I-3 {bFcStartAndOpenInstance
         &ADD-TO-TRANSACTION   = "true"
         &CLASS                = "BJournalEntry"}>
    assign vlBJEIsStartedFromCI = true.
end. /* if viBJournalEntryCIID = 0 or viBJournalEntryCIID = ? */
else do:
   <I-96 {bFcOpenInstance
        &CLASS           = "BJournalEntry"}>
end. /* Not if viBJournalEntryCIID = 0 or viBJournalEntryCIID = ? */

/* ====================================================== */
/* Go through all main-table records                      */
/* ====================================================== */
for each tApiCInvoice by tApiCInvoice.tc_Rowid :
    
    MAINAPICINVOICEBLOCK : DO : 
    
    /* ========== */
    /* Initialize */
    /* ========== */
    if tApiCInvoice.Creditor_ID    = ? then assign tApiCInvoice.Creditor_ID    = 0.
    if tApiCInvoice.tcCreditorCode = ? then assign tApiCInvoice.tcCreditorCode = "":U.
    if tApiCInvoice.CInvoiceType   = ? then assign tApiCInvoice.CInvoiceType   = "":U.
    assign vcMessage       = "":U
           vcRowID         = "":U
           vlWarningsFound = false
           vlSaveAsDraft   = (icAction = {&DAEMONACTION-STORE}).
    
    /* ====================================================== */
    /* Delete balance fields, they will be recalculated again */
    /* ====================================================== */
    assign tApiCInvoice.CInvoiceBalanceDebitTC  = 0
           tApiCInvoice.CInvoiceBalanceCreditTC = 0
           tApiCInvoice.CInvoiceBalanceDebitLC  = 0
           tApiCInvoice.CInvoiceBalanceCreditLC = 0
           tApiCInvoice.CInvoiceBalanceDebitCC  = 0
           tApiCInvoice.CInvoiceBalanceCreditCC = 0
           tApiCInvoice.CInvoiceBalanceTC       = 0
           tApiCInvoice.CInvoiceBalanceLC       = 0
           tApiCInvoice.CInvoiceBalanceCC       = 0.

    
    /* ================================================= */
    /* Check on the link-record between CInvoice-Posting */
    /* ================================================= */
    find first tApiCInvoicePosting where
               tApiCInvoicePosting.tc_ParentRowid      = tApiCInvoice.tc_Rowid         and
               tApiCInvoicePosting.CInvoicePostingType = {&INVOICEPOSTINGTYPE-INITIAL} and
               tApiCInvoicePosting.Posting_ID         <> 0                             and 
               tApiCInvoicePosting.Posting_ID         <> ?
               no-error.
    if available tApiCInvoicePosting
    then do:    
        /* ======================================================================================== */
        /* Amount of invoice must be the same as the sum of the amounts of the posting lines for LC */
        /* not for TC because the currency can change for every line of it                          */
        /* ======================================================================================== */
        <M-38 run GetPostingHeaderByPostingId
           (input  tApiCInvoicePosting.Posting_ID (iiPostingId), 
            input  ? (icPostingRowID), 
            output viPostingYear (oiPostingYear), 
            output viPostingPeriod (oiPostingPeriod), 
            output vtPostingDate (otPostingDate), 
            output vcPostingJournalCode (ocPostingJournalCode), 
            output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
        if viFcReturnSuper < 0
        then do:
            assign ocLstReturn   = ocLstReturn  + chr(4) + string(viFcReturnSuper)
                   ocLstPrimKey  = ocLstPrimKey + chr(4) + "*":U
                   ocLstRowid    = ocLstRowid   + chr(4) + "*":U
                   vlSaveAsDraft = false.
            leave MAINAPICINVOICEBLOCK.
        end. /* if viFcReturnSuper <> 0 */
        else if viFcReturnSuper > 0
             then assign vlWarningsFound = true.
        if tApiCInvoice.CInvoicePostingDate <> vtPostingDate
        then do:
            assign vcMessage     = trim(substitute(#T-44'The posting date of the posting (&1) does not match the posting date of the invoice (&2).':255(1084)T-44#, string(vtPostingDate), string(tApiCInvoice.CInvoicePostingDate))) + chr(10) +
                                   trim(substitute(#T-41'GL Calendar Year = &1; GL Period = &2; Daybook = &3; Voucher = &4.':255(969)T-41#,string(tAPICInvoice.CInvoicePostingYear),string(tAPICInvoice.CInvoicePostingPeriod), tAPICInvoice.tcJournalCode, string(tAPICInvoice.CInvoiceVoucher)))
                   ocLstReturn   = ocLstReturn  + chr(4) + string(-1)
                   ocLstPrimKey  = ocLstPrimKey + chr(4) + "*":U
                   ocLstRowid    = ocLstRowid   + chr(4) + "*":U
                   vlSaveAsDraft = false.
            <M-18 run SetMessage
               (input  vcMessage (icMessage), 
                input  '':U (icArguments), 
                input  '':U (icFieldName), 
                input  '':U (icFieldValue), 
                input  'E':U (icType), 
                input  3 (iiSeverity), 
                input  '':U (icRowid), 
                input  'qadfin-777667':U (icFcMsgNumber), 
                input  '' (icFcExplanation), 
                input  '' (icFcIdentification), 
                input  '' (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
            Leave MAINAPICINVOICEBLOCK.
        end. /* if tApiCInvoice.CInvoicePostingDate <> vtPostingDate */
        if tApiCInvoice.tcJournalCode <> vcPostingJournalCode
        then do:
            assign vcMessage     = trim(substitute(#T-13'The daybook referenced in the posting (&1) does not match daybook of the invoice (&2).':255(1085)T-13#, vcPostingJournalCode, tApiCInvoice.tcJournalCode)) + chr(10) +
                                   trim(substitute(#T-14'GL Calendar Year = &1; GL Period = &2; Daybook = &3; Voucher = &4.':255(969)T-14#,string(tAPICInvoice.CInvoicePostingYear),string(tAPICInvoice.CInvoicePostingPeriod), tAPICInvoice.tcJournalCode, string(tAPICInvoice.CInvoiceVoucher)))
                   ocLstReturn   = ocLstReturn  + chr(4) + string(-1)
                   ocLstPrimKey  = ocLstPrimKey + chr(4) + "*":U
                   ocLstRowid    = ocLstRowid   + chr(4) + "*":U
                   vlSaveAsDraft = false.
            <M-75 run SetMessage
               (input  vcMessage (icMessage), 
                input  '':U (icArguments), 
                input  '':U (icFieldName), 
                input  '':U (icFieldValue), 
                input  'E':U (icType), 
                input  3 (iiSeverity), 
                input  '':U (icRowid), 
                input  'qadfin-794907':U (icFcMsgNumber), 
                input  '' (icFcExplanation), 
                input  '' (icFcIdentification), 
                input  '' (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
            Leave MAINAPICINVOICEBLOCK.
        end. /* if tApiCInvoice.tcJournalCode <> vcPostingJournalCode */
        /* ======================================================== */
        /* Check year/period of invoice with year/period of posting */
        /* Check single CI-posting for a CI                         */
        /* ======================================================== */
        if tApiCInvoice.CInvoicePostingYear   <> viPostingYear or
           tApiCInvoice.CInvoicePostingPeriod <> viPostingPeriod
        then do:
            assign vcMessage     = trim(substitute(#T-72'The GL period and GL calendar year of the customer invoice do not match the GL calendar year (&1) and GL period (&2) of the posting.':255(12832)T-72#, string(viPostingYear), string(viPostingPeriod))) + chr(10) +
                                   trim(substitute(#T-43'GL Calendar Year = &1; GL Period = &2; Daybook = &3; Voucher = &4.':255(969)T-43#,string(tAPICInvoice.CInvoicePostingYear),string(tAPICInvoice.CInvoicePostingPeriod), tAPICInvoice.tcJournalCode, string(tAPICInvoice.CInvoiceVoucher)))
                   ocLstReturn   = ocLstReturn  + chr(4) + string(-1)
                   ocLstPrimKey  = ocLstPrimKey + chr(4) + "*":U
                   ocLstRowid    = ocLstRowid   + chr(4) + "*":U
                   vlSaveAsDraft = false.
            <M-9 run SetMessage
               (input  vcMessage (icMessage), 
                input  '':U (icArguments), 
                input  '':U (icFieldName), 
                input  '':U (icFieldValue), 
                input  'E':U (icType), 
                input  3 (iiSeverity), 
                input  '':U (icRowid), 
                input  'qadfin-737390':U (icFcMsgNumber), 
                input  '' (icFcExplanation), 
                input  '' (icFcIdentification), 
                input  '' (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
            Leave MAINAPICINVOICEBLOCK.
        end. /* if tApiCInvoice.CInvoicePostingYear   <> viPostingYear or */
        find next tApiCInvoicePosting where
                  tApiCInvoicePosting.tc_ParentRowid       = tApiCInvoice.tc_Rowid         and
                  tApiCInvoicePosting.CInvoicePostingType  = {&INVOICEPOSTINGTYPE-INITIAL} and
                  tApiCInvoicePosting.Posting_ID          <> 0                             and 
                  tApiCInvoicePosting.Posting_ID          <> ?
                  no-error.
        if available tApiCInvoicePosting
        then do:
            assign vcMessage     = trim(#T-34'A supplier invoice/journal entry integration error occurred. There is more than one posting record for this supplier invoice.':255(14385)T-34#) + chr(10) +
                                   trim(substitute(#T-51'GL Calendar Year = &1; GL Period = &2; Daybook = &3; Invoice = &4.':255(14386)T-51#,string(tAPICInvoice.CInvoicePostingYear),string(tAPICInvoice.CInvoicePostingPeriod), tAPICInvoice.tcJournalCode, string(tAPICInvoice.CInvoiceVoucher)))
                   ocLstReturn   = ocLstReturn  + chr(4) + string(-1)
                   ocLstPrimKey  = ocLstPrimKey + chr(4) + "*":U
                   ocLstRowid    = ocLstRowid   + chr(4) + "*":U
                   vlSaveAsDraft = false.
            <M-42 run SetMessage
               (input  vcMessage (icMessage), 
                input  '':U (icArguments), 
                input  '':U (icFieldName), 
                input  '':U (icFieldValue), 
                input  'E':U (icType), 
                input  3 (iiSeverity), 
                input  '':U (icRowid), 
                input  'qadfin-134872':U (icFcMsgNumber), 
                input  '' (icFcExplanation), 
                input  '' (icFcIdentification), 
                input  '' (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BCInvoice>        
            Leave MAINAPICINVOICEBLOCK.
        end. /* if available tApiCInvoicePosting */
        
    end. /* if available tApiCInvoicePosting after find first */

    /* ======================================== */
    /* Calculate the lc, cc and balance amounts */
    /* ======================================== */
    <M-94 run ApiStdMaintainTTCalcAmounts  (output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
    if viFcReturnSuper < 0
    then do:
        assign ocLstReturn   = ocLstReturn  + chr(4) + string(viFcReturnSuper)
               ocLstPrimKey  = ocLstPrimKey + chr(4) + "*":U
               ocLstRowid    = ocLstRowid   + chr(4) + "*":U
               vlSaveAsDraft = false.
        leave MAINAPICINVOICEBLOCK.
    end. /* if viFcReturnSuper <> 0 */
    else if viFcReturnSuper > 0
         then assign vlWarningsFound = true.
    
    /* ========================================================================================================================= */
    /* Call a submethod depending on the fact whether the required postings for the CI are created outside this component or not */
    /* ========================================================================================================================= */
    if not can-find(first tApiCInvoicePosting where
                          tApiCInvoicePosting.tc_ParentRowid  = tApiCInvoice.tc_Rowid and
                          tApiCInvoicePosting.Posting_ID     <> 0                     and
                          tApiCInvoicePosting.Posting_ID     <> ?)
    then do:
        <M-80 run ApiStdMaintainTTWithIntPost
           (input  icAction (icAction), 
            input-output ocLstReturn (bcLstReturn), 
            input-output ocLstPrimKey (bcLstPrimKey), 
            input-output ocLstRowid (bcLstRowId), 
            output vlSaveAsDraft (olSaveAsDraft), 
            output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
        if viFcReturnSuper < 0
        then leave MAINAPICINVOICEBLOCK.
        else if viFcReturnSuper > 0
             then assign vlWarningsFound = true.
    end. /* if not can-find */
    else do:
        <M-79 run ApiStdMaintainTTWithExtPost
           (input  icAction (icAction), 
            input-output ocLstReturn (bcLstReturn), 
            input-output ocLstPrimKey (bcLstPrimKey), 
            input-output ocLstRowid (bcLstRowId), 
            output vlSaveAsDraft (olSaveAsDraft), 
            output viFcReturnSuper (oiReturnStatus)) in BCInvoice>                                          
        if viFcReturnSuper < 0
        then leave MAINAPICINVOICEBLOCK.
        else if viFcReturnSuper > 0
             then assign vlWarningsFound = true.
    end. /* Not if not can-find */
    
    END. /* MAINAPICINVOICEBLOCK */
    
    assign vcRowID = entry(num-entries(ocLstRowid  , chr(4)), ocLstRowid  , chr(4)) no-error.
    
    /* ================================================================== */        
    /* Handle errors: Store if SaveAsDraft is needed                      */
    /* Check: only 1 draft is allowed for a single bus-component instance */
    /* ================================================================== */
    if vlSaveAsDraft and 
       index(substr(ocLstReturn, r-index(ocLstReturn, chr(4)) + 1,-1,"CHARACTER":U),"-":U) <> 0 /* Negative return-status for this entry */
    then do:
        if vlDraftInstanceCreated = true
        then do :
            assign vcMessage = trim(#T-85'Only one draft instance can be created per method call.':255(858)T-85#) + chr(10) + trim(#T-56'This second object will not be stored as a draft instance and so is an error.':255(859)T-56#).
            <M-90 run SetMessage
               (input  vcMessage (icMessage), 
                input  '':U (icArguments), 
                input  '':U (icFieldName), 
                input  '':U (icFieldValue), 
                input  'E':U (icType), 
                input  3 (iiSeverity), 
                input  '':U (icRowid), 
                input  'qadfin-775097':U (icFcMsgNumber), 
                input  '' (icFcExplanation), 
                input  '' (icFcIdentification), 
                input  '' (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
            next.
        end. /* if vlDraftInstanceCreated = true */
        <M-8 run StoreState
           (input  '(':U + tCInvoice.cinvoicedescription + ') ':U + trim(#T-59'The supplier invoice was created using an API method.':200(1216)T-59#) (icDescription), 
            input  ? (icUIClass), 
            output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
        assign vlDraftInstanceCreated                                         = true
               entry(num-entries(ocLstReturn , chr(4)), ocLstReturn , chr(4)) = string(viFcReturnSuper)
               entry(num-entries(ocLstPrimKey, chr(4)), ocLstPrimKey, chr(4)) = string(tApiCInvoice.CInvoice_ID)
               entry(num-entries(ocLstRowid  , chr(4)), ocLstRowid  , chr(4)) = vcRowID
               no-error.
    end. /* if vlSaveAsDraft and substr */

    /*=======================*/
    /* LBO - Gunters request */
    /*=======================*/
    if tApiCInvoice.CInvoicePostingYear <> 0    and
       tApiCInvoice.CInvoicePostingYear <> ?    and
       tApiCInvoice.tcCAJournalCode     <> "":U and
       tApiCInvoice.tcCAJournalCode     <> ?    and
      (tApiCInvoice.tiCAVoucher          = 0    or
       tApiCInvoice.tiCAVoucher          = ?)
    then do :
        <M-98 run DefaultValuesCAJournal
           (input  vcRowId (icCInvoiceRowId), 
            input-output tApiCInvoice.tcCAJournalCode (bcCAJournalCode), 
            input  tApiCInvoice.CInvoicePostingYear (iiCInvoicePostingYear), 
            input  0 (iiOldCInvoicePostingYear), 
            input  '':U (icOldCAJournalCode), 
            input  0 (iiOldCAVoucher), 
            output tApiCInvoice.tiCAVoucher (oiCAVoucher), 
            output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
        if viFcReturnSuper < 0 or (viFcReturnSuper > 0 and oiReturnStatus = 0)        
        then assign oiReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0
        then do:
            <I-47 {bFcCloseInstance
                 &CLASS           = "BJournalEntry"}>
            return.
        end. /* if viFcReturnSuper < 0 */
    end. /* if tApiCInvoice.CInvoicePostingYear <> 0    and */
    
end. /* for each tApiCInvoice */

/* =================== */
/* Close Journal Entry */
/* =================== */
if vhBJournalEntryCIInst <> ? and /* Can be closed by a subprocedure */
   valid-handle(vhBJournalEntryCIInst) = true
then.   /* ========================================================================================================= */
        /* Normally we would here close the communication to BJournalEntry but we will not do it for performance     */
        /* reasons as this way the Commit of the transaction does not have to re-open the instance to commit it.     */
        /* And in all other scenario's the instance of the class ill be closed by the de-activiate procedure of the  */
        /* appserver that is executed after each call to the appserver                                               */
        /* ========================================================================================================= */


/* ============================================================ */
/* Trim the first character of ocLstReturn and primkey          */
/* Check for correct number and format of the return-parameters */
/* ============================================================ */
if length(ocLstReturn,"CHARACTER":U)  > 1 then assign ocLstReturn  = substring(ocLstReturn,2,-1,"CHARACTER":U).
if length(ocLstPrimKey,"CHARACTER":U) > 1 then assign ocLstPrimKey = substring(ocLstPrimKey,2,-1,"CHARACTER":U).
if length(ocLstRowid,"CHARACTER":U)   > 1 then assign ocLstRowid   = substring(ocLstRowid,2,-1,"CHARACTER":U).
if num-entries(ocLstReturn,chr(4))  <> viMainTableRecordCount or
   num-entries(ocLstPrimKey,chr(4)) <> viMainTableRecordCount or
   num-entries(ocLstRowid,chr(4))   <> viMainTableRecordCount
then do :
    assign vcMessage     = trim(#T-92'A supplier invoice/journal entry integration error occurred.  An API method returned incomplete information. See other messages for further details.':255(14389)T-92#) + chr(10) + 
                            trim(substitute(#T-24'Main table records: &1.':255(310)T-24#,string(viMainTableRecordCount))) + chr(10) + 
                            trim(substitute(#T-7'Return Statuses: &1.':255(311)T-7#,ocLstReturn))  + chr(10) +
                            trim(substitute(#T-95'Primary keys: &1.':255(312)T-95#,ocLstPrimKey)) + chr(10) +
                            trim(substitute(#T-58'Row IDs: &1.':255(313)T-58#,ocLstRowid)).
           oiReturnStatus = -3.
    <M-74 run SetMessage
       (input  vcMessage (icMessage), 
        input  '':U (icArguments), 
        input  '':U (icFieldName), 
        input  '':U (icFieldValue), 
        input  'E':U (icType), 
        input  3 (iiSeverity), 
        input  '':U (icRowid), 
        input  'qadfin-678518':U (icFcMsgNumber), 
        input  '' (icFcExplanation), 
        input  '' (icFcIdentification), 
        input  '' (icFcContext), 
        output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
    return.
end. /* if num-entries */

/* ===================================================== */
/* Set global return status based on the individual ones */
/* ===================================================== */
if ocLstReturn = substring(fill("0":U + CHR(4),viMainTableRecordCount),1,length(ocLstReturn,"CHARACTER":U),"CHARACTER":U)
then assign oiReturnStatus = 0.
else do : 
    if index(ocLstReturn,"-":U) <> 0        
    then do :
        assign vcMessage      = trim(#T-60'An error has occurred while running this API method. See other error messages for more details.':255(301)T-60#)
               oiReturnStatus = -1.
        <M-12 run SetMessage
           (input  vcMessage (icMessage), 
            input  '':U (icArguments), 
            input  '':U (icFieldName), 
            input  '':U (icFieldValue), 
            input  'E':U (icType), 
            input  3 (iiSeverity), 
            input  '':U (icRowid), 
            input  'qadfin-210817':U (icFcMsgNumber), 
            input  '' (icFcExplanation), 
            input  '' (icFcIdentification), 
            input  '' (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
    end. /* index(ocLstReturn,"-":U) <> 0 */
    else do : 
        assign vcMessage      = trim(#T-456'A warning occurred when running the API method. See other error messages for more details.':255(14390)T-456#)
               oiReturnStatus = +1.
        <M-78 run SetMessage
           (input  vcMessage (icMessage), 
            input  '':U (icArguments), 
            input  '':U (icFieldName), 
            input  '':U (icFieldValue), 
            input  'W':U (icType), 
            input  3 (iiSeverity), 
            input  '':U (icRowid), 
            input  'qadfin-924556':U (icFcMsgNumber), 
            input  '' (icFcExplanation), 
            input  '' (icFcIdentification), 
            input  '' (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
    end. /* not if lookup */
end. /* not if ocLstReturn */

/* return status */
if oiReturnStatus = -98 then assign oiReturnStatus = 0.


Sample code: how to call this method through RPCRequestService (QXtend Inbound)

define temp-table ttContext no-undo
    field propertyQualifier as character
    field propertyName as character
    field propertyValue as character
    index entityContext is primary unique
        propertyQualifier
        propertyName
    index propertyQualifier
        propertyQualifier.

define dataset dsContext for ttContext.

define variable vhContextDS as handle no-undo.
define variable vhExceptionDS as handle no-undo.
define variable vhServer as handle no-undo.
define variable vhInputDS as handle no-undo.
define variable vhInputOutputDS as handle no-undo.
define variable vhOutputDS as handle no-undo.
define variable vhParameter as handle no-undo.

/* Create context */
create ttContext.
assign ttContext.propertyName = "programName"
       ttContext.propertyValue = "BCInvoice".
create ttContext.
assign ttContext.propertyName = "methodName"
       ttContext.propertyValue = "ApiStdMaintainTTV01".
create ttContext.
assign ttContext.propertyName = "applicationId"
       ttContext.propertyValue = "fin".
create ttContext.
assign ttContext.propertyName = "entity"
       ttContext.propertyValue = "1000".
create ttContext.
assign ttContext.propertyName = "userName"
       ttContext.propertyValue = "mfg".
create ttContext.
assign ttContext.propertyName = "password"
       ttContext.propertyValue = "".

/* Create input dataset */
create dataset vhInputDS.
vhInputDS:read-xmlschema("file", "xml/bcinvoice.apistdmaintainttv01.i.xsd", ?).
vhParameter = vhInputDS:get-buffer-handle("tParameterI").
vhParameter:buffer-create().
assign vhParameter::icAction = <parameter value>.

vhParameter = vhInputDS:get-buffer-handle("tApiCInvoice").
vhParameter:buffer-create().
assign vhParameter::<field-name-1> = <field-value-1>
       vhParameter::<field-name-2> = <field-value-2>
       ...

vhParameter = vhInputDS:get-buffer-handle("tApiCInvoiceBank").
vhParameter:buffer-create().
assign vhParameter::<field-name-1> = <field-value-1>
       vhParameter::<field-name-2> = <field-value-2>
       ...

vhParameter = vhInputDS:get-buffer-handle("tApiCInvoiceBankPayCode").
vhParameter:buffer-create().
assign vhParameter::<field-name-1> = <field-value-1>
       vhParameter::<field-name-2> = <field-value-2>
       ...

vhParameter = vhInputDS:get-buffer-handle("tApiCInvoiceMovement").
vhParameter:buffer-create().
assign vhParameter::<field-name-1> = <field-value-1>
       vhParameter::<field-name-2> = <field-value-2>
       ...

vhParameter = vhInputDS:get-buffer-handle("tApiCInvoicePO").
vhParameter:buffer-create().
assign vhParameter::<field-name-1> = <field-value-1>
       vhParameter::<field-name-2> = <field-value-2>
       ...

vhParameter = vhInputDS:get-buffer-handle("tApiCInvoicePosting").
vhParameter:buffer-create().
assign vhParameter::<field-name-1> = <field-value-1>
       vhParameter::<field-name-2> = <field-value-2>
       ...

vhParameter = vhInputDS:get-buffer-handle("tApiCInvoiceVat").
vhParameter:buffer-create().
assign vhParameter::<field-name-1> = <field-value-1>
       vhParameter::<field-name-2> = <field-value-2>
       ...

vhParameter = vhInputDS:get-buffer-handle("tApiCInvoiceStage").
vhParameter:buffer-create().
assign vhParameter::<field-name-1> = <field-value-1>
       vhParameter::<field-name-2> = <field-value-2>
       ...

vhParameter = vhInputDS:get-buffer-handle("tApiCInvoiceWHT").
vhParameter:buffer-create().
assign vhParameter::<field-name-1> = <field-value-1>
       vhParameter::<field-name-2> = <field-value-2>
       ...

/* Connect the AppServer */
create server vhServer.
vhServer:connect("-URL <appserver-url>").

if not vhServer:connected()
then do:
    message "Could not connect AppServer" view-as alert-box error title "Error".
    return.
end.

/* Run */
assign vhContextDS = dataset dsContext:handle.

run program/rpcrequestservice.p on vhServer
    (input-output dataset-handle vhContextDS by-reference,
           output dataset-handle vhExceptionDS,
     input        dataset-handle vhInputDS by-reference,
     input-output dataset-handle vhInputOutputDS by-reference,
           output dataset-handle vhOutputDS).

/* Handle output however you want, in this example, we dump it to xml */
if valid-handle(vhExceptionDS)
then vhExceptionDS:write-xml("file", "Exceptions.xml", true).

if valid-handle(vhOutputDS)
then vhOutputDS:write-xml("file", "Output.xml", true).

/* Cleanup */
vhServer:disconnect().
assign vhServer = ?.

if valid-handle(vhInputDS)
then delete object vhInputDS.

if valid-handle(vhOutputDS)
then delete object vhOutputDS.

if valid-handle(vhExceptionDS)
then delete object vhExceptionDS.