project QadFinancials > class BPosting > method ApiStdMaintainTT

Description

ApiStdMaintainTT; Method that allows the creation of new instances based on the content of the input temp-tables.


Parameters


tApiPostinginputtemp-tableInput table containing the postings
tApiPostingLineinputtemp-tableInput table containing the posting-lines
tApiPostingSafinputtemp-tableInput table containing the posting-safs of a posting-line
tApiPostingVatinputtemp-tableInput table containing the PostingVat
tApiPostingVatDelayinputtemp-tableInput table containing the PostingVatDelay
icActioninputcharacterSAVE
SAVESTORE
STORE
VALIDATE
ocLstPrimKeyoutputcharacterLstPrimKey
Char4 separated list with the primary keys of the newly created main-table records. The fields of a primkey are separated with Char2.
E.g. 1<Char2>2002<Char2>100<Char4>1<Char2>2002<Char2>101
ocLstReturnoutputcharacterLstReturn
Char4 separated list with the return-status of the newly created main-table records. Zero means no errors occured.
E.g. 0<Char4>-1<Char4>-504<Char4>1<Char4>0
ocLstRowidoutputcharacterLstRowid
Char4 separated list with the temporarely rowids (negative for new ones) of the newly created main-table records. .
E.g.
-1<Char2>aox00098001<Char2>-26
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


unused


program code (program9/bposting.p)

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

/* ============================================================== */
/* Call method that can extend the tApi-tables with some defaults */
/* ============================================================== */
<M-15 run ApiStdMaintainTTInitialDefaulting  (output viFcReturnSuper (oiReturnStatus)) in BPosting>
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 vcMsgApi       = trim(subst(#T-28'Posting integration: the specified action (&1) should be in the list &2.':255(450560736)T-28#,trim(icAction),"{&DAEMONACTION-SAVESTORE},{&DAEMONACTION-SAVE},{&DAEMONACTION-STORE},{&DAEMONACTION-VALIDATE}":U))
           oiReturnStatus = -1.
    <M-2 run SetMessage (input  vcMsgApi (icMessage),
                     input  '':U (icArguments),
                     input  '':U (icFieldName),
                     input  '':U (icFieldValue),
                     input  'E':U (icType),
                     input  1 (iiSeverity),
                     input  '':U (icRowid),
                     input  'QADFIN-1345':U (icFcMsgNumber),
                     input  '' (icFcExplanation),
                     input  '' (icFcIdentification),
                     input  '' (icFcContext),
                     output viFcReturnSuper (oiReturnStatus)) in BPosting>
    return.
end. /* if icAction */

/* ======================== */
/* Count main-table records */
/* ======================== */
for each tApiPosting no-lock :
    assign viMainTableRecordCount = viMainTableRecordCount + 1.
end. /* for each */
if viMainTableRecordCount = 0
then do:
    assign vcMsgApi       = trim(#T-29'Posting integration: no main table records (posting) found.':255(825519009)T-29#)
           oiReturnStatus = -1.
    <M-4 run SetMessage (input  vcMsgApi (icMessage),
                     input  '':U (icArguments),
                     input  '':U (icFieldName),
                     input  '':U (icFieldValue),
                     input  'E':U (icType),
                     input  1 (iiSeverity),
                     input  '':U (icRowid),
                     input  'QADFIN-1348':U (icFcMsgNumber),
                     input  '' (icFcExplanation),
                     input  '' (icFcIdentification),
                     input  '' (icFcContext),
                     output viFcReturnSuper (oiReturnStatus)) in BPosting>
    return.
end. /* if viMainTableRecordCount */

/* ================================ */
/* Start loop on main-table records */
/* ================================ */
for each tApiPosting 
    by tApiPosting.tc_Rowid: 
    
    /* =========== */
    /* Start Block */
    /* =========== */
    MAINPOSTINGPROCESSINGBLOCK: DO :
    
    /* ================================== */
    /* Initialise and Create new instance */
    /* ================================== */
    assign vcMsgApi         = "":U 
           vcRowID          = "":U
           vlWarningsFound  = false
           vlSaveAsDraft    = (icAction = {&DAEMONACTION-STORE}).
    
    /* ========================================================================== */
    /* Clear tApiSafFromPosting & Stop a possibly-still existing instance of BSaf */
    /* ========================================================================== */
    empty temp-table tApiSafFromPosting.
    if viBSafPostingID <> 0 and 
       viBSafPostingID = ? 
    then do:
        <I-50 {bFcStopInstance
            &CLASS           = "BSaf"}>
    end. /* if viBSafPostingID <> 0 and */
    
    /* ==================================================================== */
    /* Company_id should belong to current domain, if company is not passed */
    /* current company is defaulted                                         */
    /* ==================================================================== */
    if tApiPosting.Company_ID = ? or 
       tApiPosting.Company_ID = 0
    then assign tApiPosting.Company_ID = viCompanyId.
    else do:
         <Q-55 assign vlFcQueryRecordsAvailable = CompanyByDomain (NoCache)
            (input tApiPosting.Company_ID, (CompanyId)
             input viDomainID, (DomainId)
             input true, (Active)) in BCompany >
         if not (vlFcQueryRecordsAvailable = true)
         then do:    
            assign vcMsgApi      = trim(substitute(#T-42'Company ID (&1) does not match the current domain.)':200(915904178)T-42#, trim(string(tApiPosting.Company_ID)) ))
                   ocLstReturn   = ocLstReturn  + chr(4) + (if viFcReturnSuper = 0 then "-3":U else string(viFcReturnSuper))
                   ocLstPrimKey  = ocLstPrimKey + chr(4) + "*":U
                   ocLstRowid    = ocLstRowid   + chr(4) + "*":U 
                   vlSaveAsDraft = false.
            <M-43 run SetMessage (input  vcMsgApi (icMessage), 
                          input  '':U (icArguments), 
                          input  '':U (icFieldName), 
                          input  '':U (icFieldValue), 
                          input  'E':U (icType), 
                          input  3 (iiSeverity), 
                          input  '':U (icRowid), 
                          input  'QADFIN-3246':U (icFcMsgNumber), 
                          input  '' (icFcExplanation), 
                          input  '' (icFcIdentification), 
                          input  '' (icFcContext), 
                          output viFcReturnSuper (oiReturnStatus)) in BPosting>
            leave MAINPOSTINGPROCESSINGBLOCK.
        end.
    end. /* if tApiPosting.Company_ID <> ? and  */
    
    if tApiPosting.Company_ID <> 0 and
       tApiPosting.Company_ID <> ? and
       tApiPosting.tcJournalCode <> '':U and
       tApiPosting.PostingYear <> 0 and
       tApiPosting.PostingYear <> ? and
       tApiPosting.Postingvoucher <> 0 and
       tApiPosting.Postingvoucher <> ?
    then do:
        <Q-93 run PostingByJournalCode (all) (Read) (NoCache)
           (input tApiPosting.Company_ID, (CompanyId)
            input tApiPosting.tcJournalCode, (JournalCode)
            input tApiPosting.PostingYear, (PeriodYear)
            input tApiPosting.PostingVoucher, (PostingVoucher)
            output dataset tqPostingByJournalCode) in BPosting >    
        
        for first tqPostingByJournalCode where
             tqPostingByJournalCode.tiCompany_ID     =  tApiPosting.Company_ID    and
             tqPostingByJournalCode.tcJournalCode    =  tApiPosting.tcJournalCode and
             tqPostingByJournalCode.tiPostingYear    =  tApiPosting.PostingYear   and
             tqPostingByJournalCode.tiPostingVoucher =  tApiPosting.PostingVoucher:
        end.
        
        
        if available tqPostingByJournalCode
        then do:
            assign ocLstReturn  = ocLstReturn  + chr(4) + "0":U
                   ocLstPrimKey = ocLstPrimKey + chr(4) + string(tqPostingByJournalCode.tiPosting_ID)
                   ocLstRowid   = ocLstRowid   + chr(4) + tApiPosting.tc_rowid.
            if viCompanyID = tqPostingByJournalCode.tiCompany_ID then vcCompCode = vcCompanyCode.
            else do:
                <Q-67 run CompanyPrim (all) (Read) (NoCache)
                   (input tqPostingByJournalCode.tiCompany_ID, (LookupCompanyId)
                    input ?, (CompanyCode)
                    output dataset tqCompanyPrim) in BCompany>
                find first tqCompanyPrim where tqCompanyPrim.tiCompany_ID = tqPostingByJournalCode.tiCompany_ID no-error.
                if available tqCompanyPrim then vcCompCode = tqCompanyPrim.tcCompanyCode.
            end.
            vcMsgApi = trim(substitute(#T-7'This Posting Already Exists: &4/&1/&2/&3':255(342649388)T-7#, tApiPosting.PostingYear, tApiPosting.tcJournalCode , tApiPosting.PostingVoucher,vcCompCode)).                   
            <M-94 run SetMessage
               (input  vcMsgApi (icMessage), 
                input  '':U (icArguments), 
                input  'tApiPosting.PostingOriginReference':U (icFieldName), 
                input  tApiPosting.PostingOriginReference (icFieldValue), 
                input  'W':U (icType), 
                input  3 (iiSeverity), 
                input  '':U (icRowid), 
                input  'qadfin-841928':U (icFcMsgNumber), 
                input  '':U (icFcExplanation), 
                input  '':U (icFcIdentification), 
                input  '':U (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BPosting>       
            
            next.       
        end. /*IF vlFcQueryRecordsAvailable*/
    end. /*if tApiPosting.Company_ID <> 0 and*/   

    /* ============================================ */
    /* Create Posting (modification is not allowed) */
    /* ============================================ */
    <M-9 run DataNew (output viFcReturnSuper (oiReturnStatus)) in BPosting> 
    if viFcReturnSuper >= 0 then do:
        <M-59 run SetRowidConvert
           (input  tApiPosting.tc_Rowid (icOldRowid), 
            input  tPosting.tc_Rowid (icNewRowid), 
            output viFcReturnSuper (oiReturnStatus)) in BPosting>
    end.
    if viFcReturnSuper < 0 
    then assign vcMsgApi = trim(substitute(#T-40'Internal error on the posting integration: unable to create a new instance. Error number: &1.':255(52826970)T-40#,string(viFcReturnSuper))).
    
    /* ====================================== */
    /* Check for errors in the previous block */
    /* ====================================== */
    if vcMsgApi <> "":U
    then do:
        assign ocLstReturn   = ocLstReturn  + chr(4) + (if viFcReturnSuper = 0 then "-3":U else string(viFcReturnSuper))
               ocLstPrimKey  = ocLstPrimKey + chr(4) + "*":U
               ocLstRowid    = ocLstRowid   + chr(4) + "*":U 
               vlSaveAsDraft = false.
        <M-41 run SetMessage (input  vcMsgApi (icMessage), 
                      input  '':U (icArguments), 
                      input  '':U (icFieldName), 
                      input  '':U (icFieldValue), 
                      input  'E':U (icType), 
                      input  3 (iiSeverity), 
                      input  '':U (icRowid), 
                      input  'QADFIN-3245':U (icFcMsgNumber), 
                      input  '' (icFcExplanation), 
                      input  '' (icFcIdentification), 
                      input  '' (icFcContext), 
                      output viFcReturnSuper (oiReturnStatus)) in BPosting>
        leave MAINPOSTINGPROCESSINGBLOCK.
    end. /* if vcMsgApi <> "":U */
    
    /* =========================================================================================== */
    /* Copy all data into the new posting                                                          */
    /* Before continuing, we copy the primary-key-fields of the main table to the input parameters */
    /* This is just to hold it there so it can be assigned to ocPrimKey if the save succeeds. This */
    /* is done because tPosting is no longer available after the save. We use vcRowID in stead  */
    /* of tApiPosting.tc_rowid because tApiPosting.tc_rowid is used as by-clause in the for each   */     
    /* and this would result in the fact that a single record is treated multiple times by the     */
    /* for-each                                                                                    */
    /* =========================================================================================== */           
    buffer-copy tApiPosting 
        except tApiPosting.Posting_ID 
               tApiPosting.tc_ParentRowid 
               tApiPosting.tc_rowid 
               tApiPosting.tc_status 
               tApiPosting.PostingSystemDate
            to tPosting.
    assign tApiPosting.Posting_ID = tPosting.Posting_ID /* Needed to set the prim-key after the save */
           vcRowID                = tPosting.tc_Rowid
           tPosting.PostingOriginIsExternal = (if tPosting.PostingOriginIsExternal = ?
                                               then false
                                               else tPosting.PostingOriginIsExternal)
           tPosting.PostingAddGLNbrDate = (if tPosting.PostingAddGLNbrDate = ?
                                           then tPosting.PostingDate
                                           else tPosting.PostingAddGLNbrDate).     
                                                 
    /* ================= */
    /* Get a new Voucher */
    /* ================= */
    if tPosting.PostingVoucher = 0 or
       tPosting.PostingVoucher = ?
    then do:
        <M-23 run GetPostingNumber (input  tPosting.tc_Rowid (ictcRowid), 
                            input  tPosting.PostingYear (iiYear), 
                            input  tPosting.tcJournalCode (icJournalCode), 
                            output tPosting.PostingVoucher (oiVoucher), 
                            output viFcReturnSuper (oiReturnStatus)) in BPosting>
        if viFcReturnSuper < 0
        then do :
            assign vcMsgApi       = trim(substitute(#T-31'Posting integration: an error (&1) occurred while retrieving a new number for the posting.':255(583990505)T-31#,string(viFcReturnSuper)))
                   vlSaveAsDraft  = (icAction <> {&DAEMONACTION-SAVE} and icAction <> {&DAEMONACTION-VALIDATE})
                   ocLstReturn    = ocLstReturn  + chr(4) + string(viFcReturnSuper)
                   ocLstPrimKey   = ocLstPrimKey + chr(4) + "*":U
                   ocLstRowid     = ocLstRowid   + chr(4) + "*":U.
            Leave MAINPOSTINGPROCESSINGBLOCK.
        end. /* if viFcReturnSuper < 0 */
        else 
        if viFcReturnSuper > 0
        then assign vlWarningsFound = true.

        if not can-find (tCommitNumber where
                         tCommitNumber.tcNumberType   = {&COMMITNUMBERTYPE-POSTINGVOUCHER} and
                         tCommitNumber.tcPostingRowID = tPosting.tc_Rowid)
        then do :
            create tCommitNumber.
            assign tCommitNumber.tcNumberType   = {&COMMITNUMBERTYPE-POSTINGVOUCHER}
                   tCommitNumber.tcPostingRowID = tPosting.tc_Rowid.             
        end.     

    end. /* if tPosting.PostingVoucher = 0 */
    
    /* ======================================================================================= */
    /* Process posting-lines, posting-safs, posting-vats, posting-vat-delays and posting-xrefs */
    /* ======================================================================================= */
    <M-56 run ApiStdMaintainTTSub1
       (input  icAction (icAction), 
        input-output ocLstReturn (bcLstReturn), 
        input-output ocLstPrimKey (bcLstPrimKey), 
        input-output ocLstRowid (bcLstRowid), 
        input-output vlSaveAsDraft (blSaveAsDraft), 
        input-output vlQStartedVatRuleByVatIDCode (blQStartedVatRuleByVatIDCode), 
        input-output vlQStartedGetSafConceptsForStr (blQStartedGetSafConceptsForStr), 
        output viFcReturnSuper (oiReturnStatus)) in BPosting>
    if viFcReturnSuper < 0
    then leave MAINPOSTINGPROCESSINGBLOCK.
    else if viFcReturnSuper > 0
         then assign vlWarningsFound = true.
    
    /* =================================================== */
    /* This is where it ends if only SaveAsDraft is needed */
    /* =================================================== */
    if vlSaveAsDraft
    then do:
        assign ocLstReturn  = ocLstReturn  + chr(4) + "-1":U
               ocLstPrimKey = ocLstPrimKey + chr(4) + "*":U
               ocLstRowid   = ocLstRowid   + chr(4) + "*":U.
        Leave MAINPOSTINGPROCESSINGBLOCK.
    end. /* vlSaveAsDraft */
    
    /* ==================================================================================================================================================== */
    /* Call a method that will create tApiSafFromPosting records for every Saf that is used in the postings and that does not exist as a SAF in the db yet. */
    /* These tApiSafFromPosting records will be the basis for creating new SAF records in the db later on in this method.                                   */
    /* With this mechanism we support an in-flight create of SAFs for external postings                                                                     */
    /* ==================================================================================================================================================== */
    <M-74 run ApiStdMaintainTTCreatetApiSafFromPosting  (output viFcReturnSuper (oiReturnStatus)) in BPosting>
    if viFcReturnSuper < 0
    then do :
        assign ocLstReturn   = ocLstReturn  + chr(4) + string(viFcReturnSuper)
               ocLstPrimKey  = ocLstPrimKey + chr(4) + "*":U
               ocLstRowid    = ocLstRowid   + chr(4) + "*":U
               vlSaveAsDraft = (icAction <> {&DAEMONACTION-SAVE} and icAction <> {&DAEMONACTION-VALIDATE}).
        Leave MAINPOSTINGPROCESSINGBLOCK.
    end. /* viFcReturnSuper < 0 */
    
    /* =================================================== */
    /* Create the SAFs based upon tApiSafFromPosting       */
    /* =================================================== */
    if can-find (first tApiSafFromPosting)
    then do :
        if viBSafPostingID = ? or 
           viBSafPostingID  = 0
        then do:
            <I-51 {bFcStartAndOpenInstance
                 &ADD-TO-TRANSACTION   = "false"
                 &CLASS                = "BSaf"}>
        end.
        else do:
            <I-52 {bFcOpenInstance
            &CLASS           = "BSaf"}>
        end.
        <M-53 run ApiStdMaintainTT (input  tApiSafFromPosting (tApiSaf), 
                            input  icAction (icAction), 
                            output vcLstPrimKey (ocLstPrimKey), 
                            output vcLstReturn (ocLstReturn), 
                            output vcLstRowId (ocLstRowId), 
                            output viBSafReturn (oiReturnStatus)) in BSaf>
        /* ========================================================================================================= */
        /* Normally we would here close BSAF 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                                               */
        /* ========================================================================================================= */
        assign viFcReturnSuper = viBSafReturn.
        if viFcReturnSuper < 0
        then do :
            assign ocLstReturn   = ocLstReturn  + chr(4) + string(viFcReturnSuper)
                   ocLstPrimKey  = ocLstPrimKey + chr(4) + "*":U
                   ocLstRowid    = ocLstRowid   + chr(4) + "*":U
                   vlSaveAsDraft = (icAction <> {&DAEMONACTION-SAVE} and icAction <> {&DAEMONACTION-VALIDATE}).
            Leave MAINPOSTINGPROCESSINGBLOCK.
        end. /* viFcReturnSuper < 0 */
        else if viFcReturnSuper > 0
             then assign vlWarningsFound = true.
    end. /* if can-find (first tApiSafFromPosting) */
    
    /* =============================== */
    /* Set Actvity                     */
    /* =============================== */
    if (vcActivityCode = "":U or
        vcActivityCode = ?) and 
       can-find (first tPosting where
                       tPosting.tc_Status = "N":U)
    then assign vcActivityCode = "ExternalCreate":U.

   /* ============================================================================== */
    /* Add in any defaulting */    
    /* ============================================================================== */
    <M-85 run ApiStdMaintainTTDefaulting  (output viFcReturnSuper (oiReturnStatus)) in BPosting>
    if viFcReturnSuper < 0
    then do :
        assign ocLstReturn   = ocLstReturn  + chr(4) + string(viFcReturnSuper)
               ocLstPrimKey  = ocLstPrimKey + chr(4) + "*":U
               ocLstRowid    = ocLstRowid   + chr(4) + "*":U
               vlSaveAsDraft = (icAction <> {&DAEMONACTION-SAVE} and icAction <> {&DAEMONACTION-VALIDATE}).
        Leave MAINPOSTINGPROCESSINGBLOCK.
    end. /* viFcReturnSuper < 0 */

    /* ============================================================================== */
    /* Validate the data and Perform external updates (can also contain validations). */    
    /* ============================================================================== */
    <M-25 run ValidateBC (output viFcReturnSuper (oiReturnStatus)) in BPosting>
    if viFcReturnSuper < 0
    then do :
        assign ocLstReturn   = ocLstReturn  + chr(4) + string(viFcReturnSuper)
               ocLstPrimKey  = ocLstPrimKey + chr(4) + "*":U
               ocLstRowid    = ocLstRowid   + chr(4) + "*":U
               vlSaveAsDraft = (icAction <> {&DAEMONACTION-SAVE} and icAction <> {&DAEMONACTION-VALIDATE}).
        Leave MAINPOSTINGPROCESSINGBLOCK.
    end. /* viFcReturnSuper < 0 */
    else if viFcReturnSuper > 0
         then assign vlWarningsFound = true.
    <M-17 run AdditionalUpdates (output viFcReturnSuper (oiReturnStatus)) in BPosting>
    if viFcReturnSuper < 0
    then do :
        assign ocLstReturn   = ocLstReturn  + chr(4) + string(viFcReturnSuper)
               ocLstPrimKey  = ocLstPrimKey + chr(4) + "*":U
               ocLstRowid    = ocLstRowid   + chr(4) + "*":U
               vlSaveAsDraft = (icAction <> {&DAEMONACTION-SAVE} and icAction <> {&DAEMONACTION-VALIDATE}).
        Leave MAINPOSTINGPROCESSINGBLOCK.
    end. /* viFcReturnSuper < 0 */
    else if viFcReturnSuper > 0
         then assign vlWarningsFound = true.
    
    /* ================================================== */
    /* Save the data                                      */
    /* If only validation was asked then discard the save */
    /* ================================================== */
    if icAction <> {&DAEMONACTION-VALIDATE}
    then do :      
        <M-18 run DataSave (output viFcReturnSuper (oiReturnStatus)) in BPosting>
        if viFcReturnSuper < 0
        then do :
            assign ocLstReturn   = ocLstReturn  + chr(4) + string(viFcReturnSuper)
                   ocLstPrimKey  = ocLstPrimKey + chr(4) + "*":U
                   ocLstRowid    = ocLstRowid   + chr(4) + "*":U
                   vlSaveAsDraft = (icAction <> {&DAEMONACTION-SAVE} and icAction <> {&DAEMONACTION-VALIDATE}).
            Leave MAINPOSTINGPROCESSINGBLOCK.
        end. /* viFcReturnSuper < 0 */
        else if viFcReturnSuper > 0
             then assign vlWarningsFound = true.
    end. /* if icAction = {&DAEMONACTION-VALIDATE} */
    
    /* =========================================================== */
    /* Indicate the data is saved/stored correctly and set primkey */
    /* =========================================================== */
    assign ocLstReturn  = ocLstReturn  + chr(4) + (if vlWarningsFound = true then "1":U else "0":U)
           ocLstPrimKey = ocLstPrimKey + chr(4) + string(tApiPosting.Posting_ID)
           ocLstRowid   = ocLstRowid   + chr(4) + vcRowID.
    
    END. /* MAINPOSTINGPROCESSINGBLOCK */
    
    /* ================================================================== */
    /* Handle errors: Store if SaveAsDraft is needed                      */
    /* Check: only 1 draft is allowed for a single bus-component instance */
    /* ================================================================== */
    if vlSaveAsDraft and
       index(substring(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 vcMsgApi = trim(#T-44'Only one draft instance can be created per method call.':255(13856)T-44#) + chr(10) +
                              trim(#T-45'This second object will not be stored as a draft instance and will thus be considered as an error situation.':255(999890843)T-45#).
            <M-26 run SetMessage (input  vcMsgApi (icMessage),
                     input  '':U (icArguments),
                     input  '':U (icFieldName),
                     input  '':U (icFieldValue),
                     input  'E':U (icType),
                     input  1 (iiSeverity),
                     input  '':U (icRowid),
                     input  'QADFIN-1349':U (icFcMsgNumber),
                     input  '' (icFcExplanation),
                     input  '' (icFcIdentification),
                     input  '' (icFcContext),
                     output viFcReturnSuper (oiReturnStatus)) in BPosting>
            next.
        end. /* if vlDraftInstanceCreated = true */
        <M-19 run StoreState
           (input  '(':U + tPosting.PostingInvoiceReferenceTxt + ') ':U + trim(#T-32'Posting created via an API method':200(606057275)T-32#) (icDescription), 
            input  ? (icUIClass), 
            output viFcReturnSuper (oiReturnStatus)) in BPosting>
        assign vlDraftInstanceCreated                                         = true
               entry(num-entries(ocLstReturn , chr(4)), ocLstReturn , chr(4)) = string(viFcReturnSuper)
               entry(num-entries(ocLstPrimKey, chr(4)), ocLstPrimKey, chr(4)) = string(tApiPosting.Posting_ID)
               entry(num-entries(ocLstRowid  , chr(4)), ocLstRowid  , chr(4)) = vcRowID.
    end. /* if vlSaveAsDraft and */
    
end. /* for each tApiPosting */

/* ============= */
/* Stop Queries  */
/* ============= */
if vlQStartedVatRuleByVatIDCode = true
then do :
    <Q-57 run VatRuleByVatIDCode  (Stop) in BVAT >
end.
if vlQStartedGetSafConceptsForStr = true
then do :
    <Q-58 run GetSafConceptsForStructure  (Stop) in BSafStructure >
end.

/* ============================================================ */
/* 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 vcMsgApi       = trim(#T-33'Posting integration: incomplete information returned by an API method. See other messages for further details.':200(3206)T-33#) + chr(10) + 
                            trim(substitute(#T-34'Main table records: &1.':255(310)T-34#,string(viMainTableRecordCount))) + chr(10) + 
                            trim(substitute(#T-35'Return Statuses: &1.':255(311)T-35#,ocLstReturn))  + chr(10) +
                            trim(substitute(#T-36'Primary keys: &1.':255(312)T-36#,ocLstPrimKey)) + chr(10) +
                            trim(substitute(#T-37'Row IDs: &1.':255(313)T-37#,ocLstRowid)).
           oiReturnStatus = -3.
    <M-21 run SetMessage (input  vcMsgApi (icMessage),
                     input  '':U (icArguments),
                     input  '':U (icFieldName),
                     input  '':U (icFieldValue),
                     input  'E':U (icType),
                     input  1 (iiSeverity),
                     input  '':U (icRowid),
                     input  'QADFIN-1346':U (icFcMsgNumber),
                     input  '' (icFcExplanation),
                     input  '' (icFcIdentification),
                     input  '' (icFcContext),
                     output viFcReturnSuper (oiReturnStatus)) in BPosting>
    return.
end. /* if num-entries */

/* ===================================================== */
/* Set global return value based on the individual ones: */ 
/* assign it to zero if all individual ones are zero.    */
/* ===================================================== */
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 oiReturnStatus = -3
               vcMsgApi       = trim(#T-38'Posting integration: an error has occurred while running this API method. See other error messages for more details.':255(3207)T-38#).
        <M-22 run SetMessage (input  vcMsgApi (icMessage),
                     input  '':U (icArguments),
                     input  '':U (icFieldName),
                     input  '':U (icFieldValue),
                     input  'E':U (icType),
                     input  1 (iiSeverity),
                     input  '':U (icRowid),
                     input  'QADFIN-1347':U (icFcMsgNumber),
                     input  '' (icFcExplanation),
                     input  '' (icFcIdentification),
                     input  '' (icFcContext),
                     output viFcReturnSuper (oiReturnStatus)) in BPosting>
        return.
    end. /* if index(ocLstReturn,"-":U) <> 0  */
    else do : 
        assign vcMsgApi        = trim(#T-46'Posting integration: a warning has occurred while running this API method. See other error messages for more details.':255(14315)T-46#)
               oiReturnStatus  = +1.
        <M-47 run SetMessage (input  vcMsgApi (icMessage), 
                      input  '':U (icArguments), 
                      input  '':U (icFieldName), 
                      input  '':U (icFieldValue), 
                      input  'W':U (icType), 
                      input  3 (iiSeverity), 
                      input  '':U (icRowid), 
                      input  'QADFIN-3247':U (icFcMsgNumber), 
                      input  '' (icFcExplanation), 
                      input  '' (icFcIdentification), 
                      input  '' (icFcContext), 
                      output viFcReturnSuper (oiReturnStatus)) in BPosting>
        return.
    end. /* not if lookup */
end. /* Not ocLstReturn = .... */


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 = "BPosting".
create ttContext.
assign ttContext.propertyName = "methodName"
       ttContext.propertyValue = "ApiStdMaintainTT".
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/bposting.apistdmaintaintt.i.xsd", ?).
vhParameter = vhInputDS:get-buffer-handle("tParameterI").
vhParameter:buffer-create().
assign vhParameter::icAction = <parameter value>.

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

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

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

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

vhParameter = vhInputDS:get-buffer-handle("tApiPostingVatDelay").
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.