project QadFinancials > class BBusinessRelation > method DataLoadByInput

Description

This method loads database records corresponding to input values (records found in the t_s*** tables).
When nothing found in the database, a new record should be initialised.
Input values should also be updated with :
- identity fields (override input values)
- tc_Rowid / tc_ParentRowid / tc_Status

Annotation

This template can be used to implement the code for this method.

* Replace [MainTableName] with the name of the main table in the class dataset (no prefixes).
* This template assumes that the main table has an identity field, and this field is the only field of the primary index of the table. Replace [PrimaryKeyFieldName] with the name of this field.
* This template assumes that the table has one logical alternate key (a key that is visible and editable to end-users) with one field. Replace [LogicalKeyFieldName] with the name of this field. If the logical key contains more than one field, repeat the code with this tag for every field in the key.
* The run statement following [METHOD CALL] should not be copied literally, but inserted as a method call (run the method in your own component).




for each t_s[MainTableName] by rowid(t_s[MainTableName]) on error undo, throw:

/* ================================================================= */
/* Lookup object by logical key */
/* When in create mode, skip lookup. */
/* ================================================================= */
if vcActivityCode <> "Create":U
then do:
vcFreeform = "for each [MainTableName] where [MainTableName].[LogicalKeyFieldName] = '":U + t_s[MainTableName].[LogicalKeyFieldName] + "'":U.
[METHOD CALL]
run DataLoad
(input '' (icRowids),
input '' (icPkeys),
input '' (icObjectIds),
input vcFreeform (icFreeform),
input yes (ilKeepPrevious),
output viFcReturnSuper (oiReturnStatus))
if viFcReturnSuper >= 0
then do:
/* DataLoad succeeded, no no-error needed */
find first t[MainTableName] where t[MainTableName].[LogicalKeyFieldName] = t_s[MainTableName].[LogicalKeyFieldName].
if t[MainTableName].tc_Status <> ""
then do:
[METHOD CALL]
run SetMessage
(input [duplicate key error] (icMessage),
input t_s[MainTableName].[LogicalKeyFieldName] (icArguments),
input 't[MainTableName].[LogicalKeyFieldName]':U (icFieldName),
input t_s[MainTableName].[LogicalKeyFieldName] (icFieldValue),
input 'E':U (icType),
input 3 (iiSeverity),
input t_s[MainTableName].tc_Rowid (icRowid),
input 'ERR-xyz':U (icFcMsgNumber),
input '' (icFcExplanation),
input '' (icFcIdentification),
input '' (icFcContext),
output viFcReturnSuper (oiReturnStatus))
assign oiReturnStatus = -1.
return.
end.
end.
end.

/* ================================================================= */
/* When in delete mode, ignore input data. */
/* When object not found, do not return an error. */
/* ================================================================= */
if vcActivityCode = "Delete":U
then do:
if viFcReturnSuper = -4
then delete t_s[MainTableName].
else do:
if viFcReturnSuper <> 0
then oiReturnStatus = viFcReturnSuper.
if viFcReturnSuper < 0
then return.

/* keep the UI rowid for usage in error messages (tFcMessages.tcFcRowid) */
[METHOD CALL]
run SetRowidConvert
(input t_s[MainTableName].tc_Rowid (icOldRowid),
input t[MainTableName].tc_Rowid (icNewRowid),
output viFcReturnSuper (oiReturnStatus))

/* ================================================================= */
/* Update input to allow correct matching */
/* ================================================================= */
assign t_s[MainTableName].tc_Rowid = t[MainTableName].tc_Rowid
t_s[MainTableName].tc_ParentRowid = ""
opPrimeKey = (if opPrimeKey = "" then "" else opPrimeKey + chr(4))
+ string(t[MainTableName].[PrimaryKeyFieldName]).
end.

next.
end.

if vcActivityCode = "Create":U
or viFcReturnSuper = -4
then do:
/* ================================================================= */
/* When object not found and in modify only mode, return an error. */
/* ================================================================= */
if vcActivityCode = "Modify":U
then do:
[METHOD CALL]
run SetMessage
(input #T'Object with key $1 does not exist.':100T# (icMessage),
input t_s[MainTableName].[LogicalKeyFieldName] (icArguments),
input 't[MainTableName].[LogicalKeyFieldName]':U (icFieldName),
input t_s[MainTableName].[LogicalKeyFieldName] (icFieldValue),
input 'E':U (icType),
input 3 (iiSeverity),
input t_s[MainTableName].tc_Rowid (icRowid),
input 'ERR-xyz':U (icFcMsgNumber),
input '' (icFcExplanation),
input '' (icFcIdentification),
input '' (icFcContext),
output viFcReturnSuper (oiReturnStatus))
assign oiReturnStatus = -1.
return.
end.

/* ================================================================= */
/* When object not found or in create mode, create it. */
/* ================================================================= */
[METHOD CALL]
run AddDetailLine
(input '[MainTableName]':U (icTable),
input '' (icParentRowid),
output viFcReturnSuper (oiReturnStatus))

/* ================================================================= */
/* Update input to allow correct matching */
/* ================================================================= */
assign t_s[MainTableName].tc_Status = "N":U.
end.
else do:
/* ================================================================= */
/* Update input to allow correct matching */
/* ================================================================= */
assign t_s[MainTableName].tc_Status = "C":U
t[MainTableName].tc_Status = "C":U.
end.

if viFcReturnSuper <> 0
then oiReturnStatus = viFcReturnSuper.
if viFcReturnSuper < 0
then return.

/* insert code for subtables here */

/* keep the UI rowid for usage in error messages (tFcMessages.tcFcRowid) */
[METHOD CALL]
run SetRowidConvert
(input t_s[MainTableName].tc_Rowid (icOldRowid),
input t[MainTableName].tc_Rowid (icNewRowid),
output viFcReturnSuper (oiReturnStatus))

/* ================================================================= */
/* override value for identity fields in input */
/* ================================================================= */
assign t_s[MainTableName].[PrimaryKeyFieldName] = t[MainTableName].[PrimaryKeyFieldName]

/* ================================================================= */
/* Update input to allow correct matching */
/* ================================================================= */
t_s[MainTableName].tc_Rowid = t[MainTableName].tc_Rowid
t_s[MainTableName].tc_ParentRowid = ""

opPrimeKey = (if opPrimeKey = "" then "" else opPrimeKey + chr(4))
+ string(t[MainTableName].[PrimaryKeyFieldName]).
end.






If the class dataset contains more than one table, insert this code for every additional table.

/* ================================================================= */
/* Delete existing detail */
/* ================================================================= */
for each t[ChildTableName] where
t[ChildTableName].tc_ParentRowid = t[ParentTableName].tc_Rowid:
assign t[ChildTableName].tc_Status = "D":U.
end.

/* ================================================================= */
/* Update / add detail */
/* ================================================================= */
for each t_s[ChildTableName] where
t_s[ChildTableName].tc_ParentRowid = t_s[ParentTableName].tc_Rowid
on error undo, throw:

find first t[ChildTableName] where
t[ChildTableName].tc_ParentRowid = t[ParentTableName].tc_Rowid and
t[ChildTableName].[LogicalKeyFieldName] = t_s[ChildTableName].[LogicalKeyFieldName] no-error.
if available t[ChildTableName]
then if t[ChildTableName].tc_Status = "D":U
then assign t_s[ChildTableName].tc_Status = "C":U
t[ChildTableName].tc_Status = "C":U.
else do:
[METHOD CALL]
run SetMessage
(input [duplicate key error] (icMessage),
input t_s[ChildTableName].[LogicalKeyFieldName] (icArguments),
input 't[ChildTableName].[LogicalKeyFieldName]':U (icFieldName),
input t_s[ChildTableName].[LogicalKeyFieldName] (icFieldValue),
input 'E':U (icType),
input 3 (iiSeverity),
input t_s[ChildTableName].tc_Rowid (icRowid),
input 'ERR-xyz':U (icFcMsgNumber),
input '' (icFcExplanation),
input '' (icFcIdentification),
input '' (icFcContext),
output viFcReturnSuper (oiReturnStatus))
assign oiReturnStatus = -1.
return.
end.
else do:
[METHOD CALL]
run AddDetailLine (input '[ChildTableName]':U (icTable),
input t[ParentTableName].tc_Rowid (icParentRowid),
output viFcReturnSuper (oiReturnStatus))
if viFcReturnSuper <> 0
then oiReturnStatus = viFcReturnSuper.
if viFcReturnSuper < 0
then return.
assign t_s[ChildTableName].tc_Status = "N":U
t[ChildTableName].[LogicalKeyFieldName] = t_s[ChildTableName].[LogicalKeyFieldName].
end.

/* keep the UI rowid for usage in error messages (tFcMessages.tcFcRowid) */
[METHOD CALL]
run SetRowidConvert
(input t_s[ChildTableName].tc_Rowid (icOldRowid),
input t[ChildTableName].tc_Rowid (icNewRowid),
output viFcReturnSuper (oiReturnStatus))

/* ================================================================= */
/* override value for identity fields in input */
/* ================================================================= */
assign t_s[ChildTableName].[ChildPrimaryKeyFieldName] = t[ChildTableName].[ChildPrimaryKeyFieldName]

/* ================================================================= */
/* Update input to allow correct matching */
/* ================================================================= */
t_s[ChildTableName].[ParentPrimaryKeyFieldName] = t[ChildTableName].[ParentPrimaryKeyFieldName]
t_s[ChildTableName].tc_Rowid = t[ChildTableName].tc_Rowid
t_s[ChildTableName].tc_ParentRowid = t[ParentTableName].tc_Rowid.
end.

PreCondition

The code of this method must always be overridden, not extended.


Parameters


opPrimeKeyoutputlongcharPrimary Key field value of loaded record(s).
Keys are chr(4) separated.
Key fields are chr(2) separated.
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


unused


program code (program2/bbusinessrelation.p)

if oiReturnStatus = 0
then oiReturnStatus = -98.

<Q-10 run AddressTypePrim  (Start) in BAddressType >
<Q-12 run SafConceptPrim  (Start) in BSafConcept >
<Q-15 run CountryPrim  (Start) in BCountry >

<I-82 {bFcStartAndOpenInstance
     &CLASS                = "PGTM"}>

PROCESSBLOCK:
for each t_sBusinessRelation:

    /* ================================================================= */
    /* Lookup object by logical key                                      */
    /* When in create mode, skip lookup.                                 */
    /* ================================================================= */
    if vcActivityCode <> "Create":U 
    then do:
        vcFreeForm = "for each BusinessRelation where BusinessRelation.BusinessRelationCode = '":U + t_sBusinessRelation.BusinessRelationCode + "'":U.
        <M-2 run DataLoad
           (input  '' (icRowids), 
            input  '' (icPkeys), 
            input  '' (icObjectIds), 
            input  vcFreeForm (icFreeform), 
            input  yes (ilKeepPrevious), 
            output viFcReturnSuper (oiReturnStatus)) in BBusinessRelation>
        if viFcReturnSuper >= 0
        then do:
            find first tBusinessRelation where 
                       tBusinessRelation.BusinessRelationCode = t_sBusinessRelation.BusinessRelationCode 
                       no-error.
            if available tBusinessRelation and tBusinessRelation.tc_Status <> "":U
            then do:
                assign vcMessage      = trim(#T-70'This business relation was already processed in this transaction.':255(179041868)T-70#) + chr(10) + 
                                        substitute(#T-49'Business Relation Code: &1':255(116151359)T-49#, trim(t_sBusinessRelation.BusinessRelationCode))
                       oiReturnStatus = -1.
                <M-34 run SetMessage
                   (input  vcMessage (icMessage), 
                    input  '':U (icArguments), 
                    input  'tBusinessRelation.BusinessRelationCode':U (icFieldName), 
                    input  t_sBusinessRelation.BusinessRelationCode (icFieldValue), 
                    input  'E':U (icType), 
                    input  3 (iiSeverity), 
                    input  t_sBusinessRelation.tc_Rowid (icRowid), 
                    input  'QadFin-476609':U (icFcMsgNumber), 
                    input  '':U (icFcExplanation), 
                    input  '':U (icFcIdentification), 
                    input  '':U (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BBusinessRelation>
                return.
            end. /* if available tBusinessRelation and tBusinessRelation.tc_Status <> "":U */
        end. /* if viFcReturnSuper >= 0 */
    end.

    /* ================================================================= */
    /* When in delete mode, ignore input data.                           */
    /* When object not found, do not return an error.                    */
    /* ================================================================= */
    if vcActivityCode = "Delete":U
    then do:
        if viFcReturnSuper = -4 
        then delete t_sBusinessRelation.
        else do:
            if viFcReturnSuper <> 0
            then oiReturnStatus = viFcReturnSuper.
            if viFcReturnSuper < 0
            then leave PROCESSBLOCK.
            
            <M-48 run SetRowidConvert
               (input  t_sBusinessRelation.tc_Rowid (icOldRowid), 
                input  tBusinessRelation.tc_Rowid (icNewRowid), 
                output viFcReturnSuper (oiReturnStatus)) in BBusinessRelation>
            
            /* ================================================================= */
            /* Update input to allow correct matching                            */
            /* ================================================================= */
            assign t_sBusinessRelation.tc_Rowid  = tBusinessRelation.tc_Rowid
                   t_sBusinessRelation.tc_ParentRowid = ""
                   opPrimeKey   = (if opPrimeKey = "" then "" else opPrimeKey + chr(4))
                                + string(tBusinessRelation.BusinessRelation_ID).
        end.

        next.
    end.

    if vcActivityCode = "Create":U
    or viFcReturnSuper = -4
    then do:
        /* ================================================================= */
        /* When object not found and in modify only mode, return an error.   */
        /* ================================================================= */
        if vcActivityCode = "Modify":U
        then do:
            assign
                vcMessage = trim(#T-1'Object with key $1 does not exist.':100(48021)T-1#).
            <M-3 run SetMessage (input  vcMessage (icMessage), 
                     input  t_sBusinessRelation.BusinessRelationCode (icArguments), 
                     input  'tBusinessRelation.BusinessRelationCode':U (icFieldName), 
                     input  t_sBusinessRelation.BusinessRelationCode (icFieldValue), 
                     input  'E':U (icType), 
                     input  3 (iiSeverity), 
                     input  t_sBusinessRelation.tc_Rowid (icRowid), 
                     input  'QadFin-5208':U (icFcMsgNumber), 
                     input  '' (icFcExplanation), 
                     input  '' (icFcIdentification), 
                     input  '' (icFcContext), 
                     output viFcReturnSuper (oiReturnStatus)) in BBusinessRelation>            
            assign oiReturnStatus = -1.
            leave PROCESSBLOCK.
        end.

        /* ================================================================= */
        /* When object not found or in create mode, create it.               */
        /* ================================================================= */
        <M-4 run AddDetailLine (input  'BusinessRelation':U (icTable), 
                        input  '' (icParentRowid), 
                        output viFcReturnSuper (oiReturnStatus)) in BBusinessRelation>

        /* ================================================================= */
        /* Update input to allow correct matching                            */
        /* ================================================================= */
        assign t_sBusinessRelation.tc_Status = "N":U
               tBusinessRelation.tc_Status   = "N":U. /* we need to assign this tomake are test for doubles work */
    end.
    else if available tBusinessRelation
    then do:
        /* ================================================================= */
        /* Update input to allow correct matching                            */
        /* ================================================================= */
        assign t_sBusinessRelation.tc_Status = "C":U
               tBusinessRelation.tc_Status   = "C":U. /* we need to assign this tomake are test for doubles work */
        if t_sBusinessRelation.BusinessRelationIsDomRestr and tBusinessRelation.BusinessRelationIsDomRestr 
        then assign t_sBusinessRelation.Domain_ID =  tBusinessRelation.Domain_ID.             
    end.

    if viFcReturnSuper <> 0
    then oiReturnStatus = viFcReturnSuper.
    if viFcReturnSuper < 0
    then leave PROCESSBLOCK.
    
    /* insert code for subtables here: Addresses per BusinessRelation */
    /* ================================================================= */
    /* Delete existing detail                                            */
    /* ================================================================= */
    for each tAddress where
             tAddress.tc_ParentRowid = tBusinessRelation.tc_Rowid:
        assign tAddress.tc_Status = "D":U.
    end.
    
    /* ================================================================= */
    /* Update / add detail                                               */
    /* ================================================================= */
    for each t_sAddress where
             t_sAddress.tc_ParentRowid = t_sBusinessRelation.tc_Rowid and
             t_sAddress.tc_Status     <> "D":U:
        assign vcTaxZoneCode = "":U.
        /* get IDs for data from excel */
        if vcActivityCode <> "Create" 
        then do:
            assign t_sAddress.BusinessRelation_ID = tBusinessRelation.BusinessRelation_ID.
            if (t_sAddress.AddressType_ID = 0 or 
                t_sAddress.AddressType_ID = ?)       and
                t_sAddress.tcAddressTypeCode <> "":U and
                t_sAddress.tcAddressTypeCode <> ?   
            then do:
                <Q-9 run AddressTypePrim (all) (Read) (NoCache)
                   (input ?, (AddressTypeId)
                    input t_sAddress.tcAddressTypeCode, (AddressTypeCode)
                    output dataset tqAddressTypePrim) in BAddressType >
                find first tqAddressTypePrim no-error.
                if available tqAddressTypePrim
                then assign t_sAddress.AddressType_ID = tqAddressTypePrim.tiAddressType_ID.
            end.
        end.
        find first tAddress where tAddress.tc_ParentRowid      = tBusinessRelation.tc_Rowid
                              and tAddress.AddressStreet1      = t_sAddress.AddressStreet1 
                              and tAddress.AddressStreet2      = t_sAddress.AddressStreet2 
                              and tAddress.AddressStreet3      = t_sAddress.AddressStreet3 
                              and tAddress.AddressZip          = t_sAddress.AddressZip 
                              and tAddress.AddressCity         = t_sAddress.AddressCity 
                              and tAddress.BusinessRelation_ID = t_sAddress.BusinessRelation_ID 
                              and tAddress.AddressType_ID      = t_sAddress.AddressType_ID 
                   no-error.
        if not available tAddress
        then do:
            find first tAddress where 
                       tAddress.tc_ParentRowid        = tBusinessRelation.tc_Rowid       and
                       tAddress.AddressLogicKeyString = t_sAddress.AddressLogicKeyString and
                       tAddress.BusinessRelation_ID = t_sAddress.BusinessRelation_ID     and
                       tAddress.AddressType_ID      = t_sAddress.AddressType_ID 
                       no-error.
        end.
        if available tAddress
        then assign t_sAddress.tc_Status  = "C":U
                    t_sAddress.Address_ID = tAddress.Address_ID.
        else do:
            <M-5 run AddDetailLine (input  'Address':U (icTable), 
                        input  tBusinessRelation.tc_Rowid (icParentRowid), 
                        output viFcReturnSuper (oiReturnStatus)) in BBusinessRelation>
            if viFcReturnSuper <> 0
            then oiReturnStatus = viFcReturnSuper.
            if viFcReturnSuper < 0
            then leave PROCESSBLOCK.
            assign t_sAddress.tc_Status = "N":U.
        end.

        if t_sAddress.TxzTaxZone  = "":U or
           t_sAddress.TxzTaxZone  = ?
        then do:

            <M-89 run GetTaxZoneByParam
               (input  t_sAddress.tcCountryCode (icCountryCode), 
                input  t_sAddress.tcStateCode (icStateCode), 
                input  t_sAddress.tcCountyCode (icCountyCode), 
                input  t_sAddress.AddressCity (icAddressCity), 
                input  t_sAddress.AddressZip (icAddressZip), 
                output vcTaxZoneCode (ocTaxZoneCode), 
                output viFcReturnSuper (oiReturnStatus)) in PGTM>
            if viFcReturnSuper <> 0
            then oiReturnStatus = viFcReturnSuper.
            if viFcReturnSuper < 0
            then leave PROCESSBLOCK.

            assign t_sAddress.TxzTaxZone = vcTaxZoneCode.
        end.
    
        /* insert code for subtables here: Contacts per Address */
        /* ================================================================= */
        /* Delete existing detail                                            */
        /* ================================================================= */
        for each tContact where
                 tContact.tc_ParentRowid = tAddress.tc_Rowid:
            assign tContact.tc_Status = "D":U.
        end.
        
        /* ================================================================= */
        /* Update / add detail                                               */
        /* ================================================================= */
        for each t_sContact where
                 t_sContact.tc_ParentRowid = t_sAddress.tc_Rowid and
                 t_sContact.tc_Status     <> "D":U:
            
            /* get IDs for data from excel */
            if vcActivityCode <> "Create" 
            then assign t_sContact.Address_ID = t_sAddress.Address_ID.

            find first tContact where
                       tContact.tc_ParentRowid = tAddress.tc_Rowid and
                       tContact.Address_ID   = t_sContact.Address_ID and
                       tContact.ContactName  = t_sContact.ContactName no-error.
            if available tContact
            then assign t_sContact.tc_Status = "C":U.
            else do:
                <M-6 run AddDetailLine (input  'Contact':U (icTable), 
                        input  tAddress.tc_Rowid (icParentRowid), 
                        output viFcReturnSuper (oiReturnStatus)) in BBusinessRelation>
                if viFcReturnSuper <> 0
                then oiReturnStatus = viFcReturnSuper.
                if viFcReturnSuper < 0
                then leave PROCESSBLOCK.
                assign t_sContact.tc_Status = "N":U.
            end.
            
            <M-24 run SetRowidConvert
               (input  t_sContact.tc_Rowid (icOldRowid), 
                input  tContact.tc_Rowid (icNewRowid), 
                output viFcReturnSuper (oiReturnStatus)) in BBusinessRelation>
            
            /* ================================================================= */
            /* override value for identity fields in input                       */
            /* ================================================================= */
            assign t_sContact.Contact_ID = tContact.Contact_ID
        
            /* ================================================================= */
            /* Update input to allow correct matching                            */
            /* ================================================================= */
                   t_sContact.Address_ID     = tContact.Address_ID
                   t_sContact.tc_Rowid       = tContact.tc_Rowid
                   t_sContact.tc_ParentRowid = tAddress.tc_Rowid.
        end.

        <M-57 run SetRowidConvert
           (input  t_sAddress.tc_Rowid (icOldRowid), 
            input  tAddress.tc_Rowid (icNewRowid), 
            output viFcReturnSuper (oiReturnStatus)) in BBusinessRelation>

        /* ================================================================= */
        /* override value for identity fields in input                       */
        /* ================================================================= */
        assign t_sAddress.Address_ID = tAddress.Address_ID
               t_sAddress.BusinessRelation_ID = tAddress.BusinessRelation_ID
               t_sAddress.tc_Rowid       = tAddress.tc_Rowid
               t_sAddress.tc_ParentRowid = tBusinessRelation.tc_Rowid.

    end.

    /* insert code for subtables here: BusRelationSafDefault per BusinessRelation */
    /* ================================================================= */
    /* Delete existing detail                                            */
    /* ================================================================= */
    for each tBusRelationSafDefault where
             tBusRelationSafDefault.tc_ParentRowid = tBusinessRelation.tc_Rowid:
        assign tBusRelationSafDefault.tc_Status = "D":U.
    end.
    
    /* ================================================================= */
    /* Update / add detail                                               */
    /* ================================================================= */
    for each t_sBusRelationSafDefault where
             t_sBusRelationSafDefault.tc_ParentRowid = t_sBusinessRelation.tc_Rowid and
             t_sBusRelationSafDefault.tc_Status     <> "D":U:
        assign t_sBusRelationSafDefault.BusinessRelation_ID = tBusinessRelation.BusinessRelation_ID.

        /* ============================================ */
        /* Get the SafConcept_ID based on the logical key   */
        /* ============================================ */       
        if ( t_sBusRelationSafDefault.SafConcept_ID = ?    or
             t_sBusRelationSafDefault.SafConcept_ID = 0)   and
             t_sBusRelationSafDefault.tcSafConceptCode <> "" and
             t_sBusRelationSafDefault.tcSafConceptCode <> ?
        then do:
            <Q-11 run SafConceptPrim (all) (Read) (NoCache)
               (input ?, (SafConceptID)
                input t_sBusRelationSafDefault.tcSafConceptCode, (SafConceptCode)
                output dataset tqSafConceptPrim) in BSafConcept >
            find tqSafConceptPrim where 
                 tqSafConceptPrim.tcSafConceptCode = t_sBusRelationSafDefault.tcSafConceptCode           
                 no-lock no-error.
            if available tqSafConceptPrim
            then assign  t_sBusRelationSafDefault.SafConcept_ID = tqSafConceptPrim.tiSafConcept_ID.
        end. /* if t_sBusRelationSafDefault_ID = ? or */

        find first tBusRelationSafDefault where 
                   tBusRelationSafDefault.tc_ParentRowid       = tBusinessRelation.tc_Rowid                   and
                   tBusRelationSafDefault.BusinessRelation_ID  = t_sBusRelationSafDefault.BusinessRelation_ID and
                   tBusRelationSafDefault.SafConcept_ID        = t_sBusRelationSafDefault.SafConcept_ID 
                   no-error.
        if available tBusRelationSafDefault
        then assign t_sBusRelationSafDefault.tc_Status = "C":U.
        else do:
            <M-7 run AddDetailLine (input  'BusRelationSafDefault':U (icTable), 
                        input  tBusinessRelation.tc_Rowid (icParentRowid), 
                        output viFcReturnSuper (oiReturnStatus)) in BBusinessRelation>
            if viFcReturnSuper <> 0
            then oiReturnStatus = viFcReturnSuper.
            if viFcReturnSuper < 0
            then leave PROCESSBLOCK.
            assign t_sBusRelationSafDefault.tc_Status = "N":U.
        end.
        
        <M-28 run SetRowidConvert
           (input  t_sBusRelationSafDefault.tc_Rowid (icOldRowid), 
            input  tBusRelationSafDefault.tc_Rowid (icNewRowid), 
            output viFcReturnSuper (oiReturnStatus)) in BBusinessRelation>
        
        /* ================================================================= */
        /* override value for identity fields in input                       */
        /* ================================================================= */
        assign t_sBusRelationSafDefault.BusRelationSafDefault_ID = tBusRelationSafDefault.BusRelationSafDefault_ID
    
        /* ================================================================= */
        /* Update input to allow correct matching                            */
        /* ================================================================= */
               t_sBusRelationSafDefault.BusinessRelation_ID = tBusRelationSafDefault.BusinessRelation_ID
               t_sBusRelationSafDefault.tc_Rowid       = tBusRelationSafDefault.tc_Rowid
               t_sBusRelationSafDefault.tc_ParentRowid = tBusinessRelation.tc_Rowid.
    end.
    

    /* insert code for subtables here: VatNumber per BusinessRelation */
    /* ================================================================= */
    /* Delete existing detail                                            */
    /* ================================================================= */
    for each tVatNumber where
             tVatNumber.tc_ParentRowid = tBusinessRelation.tc_Rowid:
        assign tVatNumber.tc_Status = "D":U.
    end.
    
    /* ================================================================= */
    /* Update / add detail                                               */
    /* ================================================================= */
    for each t_sVatNumber where
             t_sVatNumber.tc_ParentRowid = t_sBusinessRelation.tc_Rowid and
             t_sVatNumber.tc_Status     <> "D":U:
        assign t_sVatNumber.BusinessRelation_ID = tBusinessRelation.BusinessRelation_ID.

        /* ============================================ */
        /* Get the Country_ID based on the logical key  */
        /* ============================================ */       
        if ( t_sVatNumber.IdentityCountry_ID = ?    or
             t_sVatNumber.IdentityCountry_ID = 0)   and
             t_sVatNumber.tcIdentityCountryCode <> "" and
             t_sVatNumber.tcIdentityCountryCode <> ?
        then do:
            <Q-14 run CountryPrim (all) (Read) (NoCache)
               (input ?, (CountryId)
                input t_sVatNumber.tcIdentityCountryCode, (CountryCode)
                output dataset tqCountryPrim) in BCountry >
            find tqCountryPrim where 
                 tqCountryPrim.tcCountryCode = t_sVatNumber.tcIdentityCountryCode           
                 no-lock no-error.
            if available tqCountryPrim
            then assign  t_sVatNumber.IdentityCountry_ID = tqCountryPrim.tiCountry_ID.
        end. /* if t_sVatNumber_ID = ? or */

        find first tVatNumber where tVatNumber.tc_ParentRowid       = tBusinessRelation.tc_Rowid 
                                and tVatNumber.BusinessRelation_ID  = t_sVatNumber.BusinessRelation_ID
                                and tVatNumber.VatNumberIdentity    = t_sVatNumber.VatNumberIdentity
                                and tVatNumber.IdentityCountry_ID   = t_sVatNumber.IdentityCountry_ID
                   no-error.
        if available tVatNumber
        then assign t_sVatNumber.tc_Status = "C":U.
        else do:
            <M-8 run AddDetailLine (input  'VatNumber':U (icTable), 
                        input  tBusinessRelation.tc_Rowid (icParentRowid), 
                        output viFcReturnSuper (oiReturnStatus)) in BBusinessRelation>
            if viFcReturnSuper <> 0
            then oiReturnStatus = viFcReturnSuper.
            if viFcReturnSuper < 0
            then leave PROCESSBLOCK.
            assign t_sVatNumber.tc_Status = "N":U.
        end.
        
        <M-84 run SetRowidConvert
           (input  t_sVatNumber.tc_Rowid (icOldRowid), 
            input  tVatNumber.tc_Rowid (icNewRowid), 
            output viFcReturnSuper (oiReturnStatus)) in BBusinessRelation>
        
        /* ================================================================= */
        /* override value for identity fields in input                       */
        /* ================================================================= */
        assign t_sVatNumber.VatNumber_ID = tVatNumber.VatNumber_ID
    
        /* ================================================================= */
        /* Update input to allow correct matching                            */
        /* ================================================================= */
               t_sVatNumber.BusinessRelation_ID = tVatNumber.BusinessRelation_ID
               t_sVatNumber.tc_Rowid       = tVatNumber.tc_Rowid
               t_sVatNumber.tc_ParentRowid = tBusinessRelation.tc_Rowid.
    end.
    
    <M-31 run SetRowidConvert
       (input  t_sBusinessRelation.tc_Rowid (icOldRowid), 
        input  tBusinessRelation.tc_Rowid (icNewRowid), 
        output viFcReturnSuper (oiReturnStatus)) in BBusinessRelation>

    /* ================================================================= */
    /* override value for identity fields in input                       */
    /* ================================================================= */
    assign t_sBusinessRelation.BusinessRelation_ID = tBusinessRelation.BusinessRelation_ID

    /* ================================================================= */
    /* Update input to allow correct matching                            */
    /* ================================================================= */
           t_sBusinessRelation.tc_Rowid  = tBusinessRelation.tc_Rowid
           t_sBusinessRelation.tc_ParentRowid = ""

           opPrimeKey       = (if opPrimeKey = "" then "" else opPrimeKey + chr(4))
                            + string(tBusinessRelation.BusinessRelation_ID).
end.

<I-61 {tFcCloseAndStopInstance
     &CLASS           = "PGTM"}>

<Q-16 run AddressTypePrim  (Stop) in BAddressType >
<Q-17 run SafConceptPrim  (Stop) in BSafConcept >
<Q-18 run CountryPrim  (Stop) in BCountry >

if oiReturnStatus = -98
then oiReturnStatus = 0.