project QadFinancials > class BPeriod > method ApiStdMaintainTT

Description

With this method a Period record can be added, modified or deleted.


Parameters


tApiPeriodinputtemp-tableTemp table like Period.
icActioninputcharacterThis input parameter defines what should happen with the input data:
- SAVE: Save the data if there were no errors.
- SAVESTORE: Save the data if there were no errors. If there were, save as draft.
- STORE: Save the data as draft.
- VALIDATE: Validate the input data, but do not save the data.
ocLstPrimKeyoutputcharacterChar4 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
ocLstReturnoutputcharacterList of Return Status.
ocLstRowidoutputcharacterList of Row ID.
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


unused


program code (program7/bperiod.p)

/* =================================================== */
/* Method Info:                                        */
/* Period will be loaded based on the Logical Key      */
/* =================================================== */

/* ============================================================================ */
/* Clear the instance:                                                          */
/* this will also clear the t_s tables that were filled in ApiMaintainByDataSet */
/* ============================================================================ */
<M-1 run ClearData (output viFcReturnSuper (oiReturnStatus)) in BPeriod>
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 vcPeriodMsg    = trim(subst(#T-21'GL period integration: the specified action (&1) should be in the list &2.':255(3086)t-21#,trim(icAction),"{&DAEMONACTION-SAVESTORE},{&DAEMONACTION-SAVE},{&DAEMONACTION-STORE},{&DAEMONACTION-VALIDATE}":U))
           oiReturnStatus = -1.
    <M-2 run SetMessage (input  vcPeriodMsg (icMessage),
                     input  '':U (icArguments),
                     input  '':U (icFieldName),
                     input  '':U (icFieldValue),
                     input  'E':U (icType),
                     input  1 (iiSeverity),
                     input  '':U (icRowid),
                     input  'QADFIN-2597':U (icFcMsgNumber),
                     input  '' (icFcExplanation),
                     input  '' (icFcIdentification),
                     input  '' (icFcContext),
                     output viFcReturnSuper (oiReturnStatus)) in BPeriod>    
    return.
end. /* if icAction */

/* ======================== */
/* Count main-table records */
/* ======================== */
for each tApiPeriod no-lock :
    assign viMainTableRecordCount = viMainTableRecordCount + 1.
end. /* for each */
if viMainTableRecordCount = 0
then do:
    assign vcPeriodMsg       = trim(#T-22'GL period integration: no main table records (GL period) found.':255(3087)T-22#)
           oiReturnStatus = -1.
    <M-3 run SetMessage (input  vcPeriodMsg (icMessage),
                     input  '':U (icArguments),
                     input  '':U (icFieldName),
                     input  '':U (icFieldValue),
                     input  'E':U (icType),
                     input  1 (iiSeverity),
                     input  '':U (icRowid),
                     input  'QADFIN-2598':U (icFcMsgNumber),
                     input  '' (icFcExplanation),
                     input  '' (icFcIdentification),
                     input  '' (icFcContext),
                     output viFcReturnSuper (oiReturnStatus)) in BPeriod>
    return.
end. /* if viMainTableRecordCount */

/* ================================================================= */
/* Start queries                                                     */
/* ================================================================= */

<Q-4 run PeriodPrim (Start) in BPeriod >

/* ================================ */
/* Start loop on main-table records */
/* ================================ */
for each tApiPeriod by tApiPeriod.tc_Rowid : 

    MAIN-BLOCK:
    DO:

    /* ========== */
    /* Initialize */
    /* ========== */
    assign vcPeriodMsg         = "":U
           vcRowID             = "":U
           vlWarningsFound     = false
           vlSaveAsDraft       = icAction = {&DAEMONACTION-STORE}.
    
    /* ======================================================= */
    /* Period company_id should be the same as the viCompanyID */
    /* ======================================================= */
    if tApiPeriod.Company_ID <> viCompanyId and
       tApiPeriod.Company_ID <> ?           and
       tApiPeriod.Company_ID <> 0
    then do:
        assign vcPeriodMsg   = trim(substitute(#T-23'The entity (ID &1) does not match the current entity.':200(2211)t-23#, trim(string(tApiPeriod.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-5 run SetMessage (input  vcPeriodMsg (icMessage),
                     input  '':U (icArguments),
                     input  '':U (icFieldName),
                     input  '':U (icFieldValue),
                     input  'E':U (icType),
                     input  1 (iiSeverity),
                     input  '':U (icRowid),
                     input  'QADFIN-2599':U (icFcMsgNumber),
                     input  '' (icFcExplanation),
                     input  '' (icFcIdentification),
                     input  '' (icFcContext),
                     output viFcReturnSuper (oiReturnStatus)) in BPeriod>
        leave MAIN-BLOCK.
    end.

    /* ====================================== */
    /* Get the Period_ID based on the logical key */
    /* ====================================== */
    if (tApiPeriod.Period_ID    = ?  or
        tApiPeriod.Period_ID    = 0) and
        tApiPeriod.PeriodYear   <> ? and
        tApiPeriod.PeriodYear   <> 0 and
        tApiPeriod.PeriodPeriod <> ? and
        tApiPeriod.PeriodPeriod <> 0
    then do:
        if tApiPeriod.Company_ID = ? or tApiPeriod.Company_ID = 0
        then assign tApiPeriod.Company_ID = viCompanyId.
        <Q-7 run PeriodPrim (all) (Read) (NoCache)
          (input tApiPeriod.Company_Id, (CompanyId)
           input ?, (PeriodId)
           input tApiPeriod.PeriodYear, (PeriodYear)
           input tApiPeriod.PeriodPeriod, (PeriodPeriod)
           output dataset tqPeriodPrim) in BPeriod >
                
        find tqPeriodPrim where 
             tqPeriodPrim.tiCompany_ID     = tApiPeriod.Company_ID     and
             tqPeriodPrim.tiPeriodYear     = tApiPeriod.PeriodYear     and
             tqPeriodPrim.tiPeriodPeriod   = tApiPeriod.PeriodPeriod
             no-lock no-error.
        if available tqPeriodPrim
        then assign tApiPeriod.Period_ID = tqPeriodPrim.tiPeriod_Id.
    end.

    /* ==================== */
    /* Load/Create Period   */
    /* ==================== */
    if tApiPeriod.Period_ID = ? or
       tApiPeriod.Period_ID = 0
    then assign viFcReturnSuper = -4.
    else do:
        <M-8 run DataLoad (input  ? (icRowids), 
                   input  string(tApiPeriod.Period_ID) (icPkeys), 
                   input  ? (icObjectIds), 
                   input  ? (icFreeform), 
                   input  false (ilKeepPrevious), 
                   output viFcReturnSuper (oiReturnStatus)) in BPeriod>        
    end.
    if viFcReturnSuper = -4
    then do:
        <M-9 run DataNew (output viFcReturnSuper (oiReturnStatus)) in BPeriod>
        if viFcReturnSuper < 0
        then assign vcPeriodMsg = trim(subst(#T-24'Cannot create a new instance. Error number: &1.':255(308)t-24#, string(viFcReturnSuper))).

        find first tPeriod no-error.
    end.
    else do:
        if viFcReturnSuper < 0
        then assign vcPeriodMsg = trim(subst(#T-25'Cannot load the instance (error number: &1, instance ID: &2).':255(289)t-25#, string(viFcReturnSuper), string(tApiPeriod.Period_ID))).
        else do:
            find tPeriod where
                 tPeriod.Period_ID = tApiPeriod.Period_ID
                 no-error.
            if not available tPeriod
            then assign vcPeriodMsg = trim(subst(#T-26'Cannot find the instance after it has been loaded (instance ID: &1).':255(290)t-26#,
                                               string(tApiPeriod.Period_ID))).
            else assign tPeriod.tc_Status = "C":U.
        end.
    end. /* NOT if viFcReturnSuper = -4*/

    /* ================================================================= */
    /* Check for errors in the previous block                            */
    /* ================================================================= */

    if vcPeriodMsg <> "":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-10 run SetMessage (input  vcPeriodMsg (icMessage),
                     input  '':U (icArguments),
                     input  '':U (icFieldName),
                     input  '':U (icFieldValue),
                     input  'E':U (icType),
                     input  1 (iiSeverity),
                     input  '':U (icRowid),
                     input  'QADFIN-2596':U (icFcMsgNumber),
                     input  '' (icFcExplanation),
                     input  '' (icFcIdentification),
                     input  '' (icFcContext),
                     output viFcReturnSuper (oiReturnStatus)) in BPeriod>
        leave MAIN-BLOCK.
    end.

    /* ====================== */
    /* Copy data for Period   */
    /* ====================== */     

    buffer-copy tApiPeriod
        except tApiPeriod.Period_ID
               tApiPeriod.tc_ParentRowid
               tApiPeriod.tc_Rowid
               tApiPeriod.tc_Status
               tApiPeriod.PeriodYear
               tApiPeriod.PeriodPeriod
        to tPeriod.
    if tPeriod.tc_Status = "N":U
    then assign tPeriod.PeriodYear       = tApiPeriod.PeriodYear
                tPeriod.PeriodPeriod     = tApiPeriod.PeriodPeriod.

    assign vcRowID              = tPeriod.tc_Rowid
           tApiPeriod.Period_ID = tPeriod.Period_ID. /*Needed to set the prim-key after the save*/

    /* =================================================== */
    /* 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 MAIN-BLOCK.
    end.

    /* =============================== */
	/* Set Actvity                     */
	/* =============================== */
	if can-find (first tPeriod where
			   		   tPeriod.tc_Status = "N":U)
	then assign vcActivityCode = "CreateAccountingYear":U.
	else if can-find (first tPeriod where
							tPeriod.tc_Status = "C":U)
	then assign vcActivityCode = "ModifyAccountingYear":U.

    /* ================= */
    /* Validate the data */
    /* ================= */
    <M-11 run ValidateBC (output viFcReturnSuper (oiReturnStatus)) in BPeriod>
    
    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 main-block.
    end.
    else if viFcReturnSuper > 0
    then assign vlWarningsFound = true.
    
    /* ================ */
    /* External updates */
    /* ================ */
    <M-12 run AdditionalUpdates (output viFcReturnSuper (oiReturnStatus)) in BPeriod>
        
    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 main-block.
    end.
    else if viFcReturnSuper > 0
    then assign vlWarningsFound = true.
        
    /* ============= */
    /* Save the data */
    /* ============= */
    if icAction <> {&DAEMONACTION-VALIDATE}
    then do:
        <M-13 run DataSave (output viFcReturnSuper (oiReturnStatus)) in BPeriod>
            
        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 main-block.
        end.
        else if viFcReturnSuper > 0
        then assign vlWarningsFound = true.
    end. /*if icAction <> {&DAEMONACTION-VALIDATE}*/

    /* ============================================ */
    /* Indicate that everything was saved correctly */
    /* ============================================ */
    assign ocLstReturn   = ocLstReturn  + chr(4) + (if vlWarningsFound = true then "1":U else "0":U)
           ocLstPrimKey  = ocLstPrimKey + chr(4) + string(tApiPeriod.Period_ID)
           ocLstRowid    = ocLstRowid   + chr(4) + vcRowID.
            
    END. /* MAIN-BLOCK */

    /* ================================================================== */
    /* 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 vcPeriodMsg = trim(#T-35'Only one draft instance can be created per method call.':255(13856)T-35#) +
                                 chr(10) +
                                 trim(#T-36'This second object cannot be stored as a draft instance and so is an error.':255(13879)t-36#).
            <M-37 run SetMessage (input  vcPeriodMsg (icMessage), 
                      input  '':U (icArguments), 
                      input  '':U (icFieldName), 
                      input  '':U (icFieldValue), 
                      input  'E':U (icType), 
                      input  1 (iiSeverity), 
                      input  '':U (icRowid), 
                      input  'QADFIN-3135':U (icFcMsgNumber), 
                      input  '' (icFcExplanation), 
                      input  '' (icFcIdentification), 
                      input  '' (icFcContext), 
                      output viFcReturnSuper (oiReturnStatus)) in BPeriod>
            next.
        end. /*if vlDraftInstanceCreated = true*/
    
        assign vcStoreStateDesc = '(':U + string(tPeriod.PeriodYear) + '/':U + string(tPeriod.PeriodPeriod) + ') ':U + trim(#T-27'GL period created using an API method.':200(3088)T-27#).
        <M-14 run StoreState (input  vcStoreStateDesc (icDescription), 
                      input  ? (icUIClass), 
                      output viFcReturnSuper (oiReturnStatus)) in BPeriod>
            
        assign vlDraftInstanceCreated                                         = true
               entry(num-entries(ocLstReturn , chr(4)), ocLstReturn , chr(4)) = string(viFcReturnSuper)
               entry(num-entries(ocLstPrimKey, chr(4)), ocLstPrimKey, chr(4)) = string(tApiPeriod.Period_ID)
               entry(num-entries(ocLstRowid  , chr(4)), ocLstRowid  , chr(4)) = vcRowID.
        
    end. /*if vlSaveAsDraft and index*/    
end. /* for each tApiPeriod */

/* ================================================================= */
/* Stop queries                                                                  */
/* ================================================================= */

<Q-15 run PeriodPrim (Stop) in BPeriod >
                       
/* ============================================================ */
/* 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 vcPeriodMsg    = trim(#T-28'GL period integration: incomplete information returned by an API method. See other messages for further details.':200(3089)T-28#) + chr(10) + 
                            trim(substitute(#T-29'Main table records: &1.':255(310)T-29#,string(viMainTableRecordCount))) + chr(10) + 
                            trim(substitute(#T-30'Return Statuses: &1.':255(311)T-30#,ocLstReturn))  + chr(10) +
                            trim(substitute(#T-31'Primary keys: &1.':255(312)T-31#,ocLstPrimKey)) + chr(10) +
                            trim(substitute(#T-32'Row IDs: &1.':255(313)T-32#,ocLstRowid)).
           oiReturnStatus = -3.
    <M-18 run SetMessage (input  vcPeriodMsg (icMessage),
                     input  '':U (icArguments),
                     input  '':U (icFieldName),
                     input  '':U (icFieldValue),
                     input  'E':U (icType),
                     input  1 (iiSeverity),
                     input  '':U (icRowid),
                     input  'QADFIN-2600':U (icFcMsgNumber),
                     input  '' (icFcExplanation),
                     input  '' (icFcIdentification),
                     input  '' (icFcContext),
                     output viFcReturnSuper (oiReturnStatus)) in BPeriod>
    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 vcPeriodMsg     = trim(#T-33'An error has occurred while running this API method. See other error messages for more details.':255(301)T-33#)
               oiReturnStatus  = -1.

         <M-19 run SetMessage (input  vcPeriodMsg (icMessage),
                     input  '':U (icArguments),
                     input  '':U (icFieldName),
                     input  '':U (icFieldValue),
                     input  'E':U (icType),
                     input  1 (iiSeverity),
                     input  '':U (icRowid),
                     input  'QADFIN-2601':U (icFcMsgNumber),
                     input  '' (icFcExplanation),
                     input  '' (icFcIdentification),
                     input  '' (icFcContext),
                     output viFcReturnSuper (oiReturnStatus)) in BPeriod>
         
    end. /* if index(ocLstReturn,"-":U) <> 0 */
    else do : 
        assign vcPeriodMsg     = trim(#T-34'A warning has occurred while running this API method. See other error messages for more details.':255(302)T-34#)
               oiReturnStatus  = +1.
        <M-20 run SetMessage (input  vcPeriodMsg (icMessage),
                     input  '':U (icArguments),
                     input  '':U (icFieldName),
                     input  '':U (icFieldValue),
                     input  'E':U (icType),
                     input  1 (iiSeverity),
                     input  '':U (icRowid),
                     input  'QADFIN-2602':U (icFcMsgNumber),
                     input  '' (icFcExplanation),
                     input  '' (icFcIdentification),
                     input  '' (icFcContext),
                     output viFcReturnSuper (oiReturnStatus)) in BPeriod>
    end. /* not if lookup */
end. /* not if 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 = "BPeriod".
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/bperiod.apistdmaintaintt.i.xsd", ?).
vhParameter = vhInputDS:get-buffer-handle("tParameterI").
vhParameter:buffer-create().
assign vhParameter::icAction = <parameter value>.

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