project QadFinancials > class BDebtorEndUser > 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/bdebtorenduser.p)

MAIN_BLOCK:
do on error undo, leave:

    assign viLocalReturnStatus = 0
           oiReturnStatus      = -98.
        
    /* ==================================================================== *
     * To optimalize performance, keep the instances as open as possible    *
     * In case of API execution where the DataSave is done in the same run  *
     * instances can be kept open even after Additional Updates             *
     * ==================================================================== */
    assign vlIsKeepInstancesOpen = true.  

    /* ====================================== */
    /* Fill SharedSetID for excel integration */
    /* ====================================== */
    assign viSharedSetID = 0.
    if vcActivityCode <> "Create":U
    then do:
        <Q-78 run LookupSharedSetForCompany (all) (Read) (NoCache)
           (input viCompanyId, (CompanyId)
            input {&SHAREDSETTYPECODE-DEBTOR}, (SharedSetType)
            output dataset tqSharedSetForCompany) in BCompany >
        find first tqSharedSetForCompany no-error.
        if available tqSharedSetForCompany
        then assign viSharedSetID = tqSharedSetForCompany.tiSharedSet_ID.
    end. /* if vcActivityCode <> "Create":U */

    /* ============================================================== */        
    /* PART 1 Create addresses in BBusinessRelation                   */
    /* ============================================================== */        
    empty temp-table tApiEndUserBusinessRelation.
    empty temp-table tApiEndUserAddress.
    empty temp-table tApiEndUserContact.

    assign viBusinessrelationCount = 0
           viAddressCount = 0.

    /* Raise a Warning message if the Addresstype is supplied when the DebtorEndUserIsDebtor = true */
    if can-find (first t_sDebtorEndUser where
                       t_sDebtorEndUser.DebtorEndUserIsDebtor = true and
                      (t_sDebtorEndUser.tcAddressTypeCode  <> ? and t_sDebtorEndUser.tcAddressTypeCode <> "":U ))
    then do :
        /* WARNING */
        <M-7 run SetMessage
           (input  #T-96'If Use Customer field is selected, the supplied address and contact information will not be used.':150(47230124)T-96# (icMessage), 
            input  ? (icArguments), 
            input  ? (icFieldName), 
            input  ? (icFieldValue), 
            input  'W':U (icType), 
            input  3 (iiSeverity), 
            input  ? (icRowid), 
            input  'qadfin-945038':U (icFcMsgNumber), 
            input  '':U (icFcExplanation), 
            input  '':U (icFcIdentification), 
            input  '':U (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BDebtorEndUser>
            
        assign viLocalReturnStatus = 1.    
    end.     
    
    /* Raise a Warning message if the BusinessRelationcode is supplied when the DebtorEndUserIsDebtor = true */
    if can-find (first t_sDebtorEndUser where
                       t_sDebtorEndUser.DebtorEndUserIsDebtor = true and
                      (t_sDebtorEndUser.tcBusinessRelationCode <> ? and t_sDebtorEndUser.tcBusinessRelationCode <> "":U))
    then do :
        <M-52 run SetMessage
           (input  #T-91'If link to customer is ticked then specified business relation code will not be used, it will be defaulted from customer':255(445650948)T-91# (icMessage), 
            input  ? (icArguments), 
            input  ? (icFieldName), 
            input  ? (icFieldValue), 
            input  'W':U (icType), 
            input  3 (iiSeverity), 
            input  ? (icRowid), 
            input  'qadfin-319383':U (icFcMsgNumber), 
            input  '':U (icFcExplanation), 
            input  '':U (icFcIdentification), 
            input  '':U (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BDebtorEndUser>
            
        assign viLocalReturnStatus = 1 .
    end.
    /* Treat the address in BBusinessrelation but only when DebtorEndUserIsDebtor <> true */
    for each t_sDebtorEndUser where
             t_sDebtorEndUser.DebtorEndUserIsDebtor <> true
        break by t_sDebtorEndUser.tcBusinessRelationCode:
        
        if  vcActivityCode = "Create":U
            and t_sDebtorEndUser.tcAddressTypeCode <> {&ADDRESSTYPECODESYSTEM-ENDUSER} 
            and t_sDebtorEndUser.tcAddressTypeCode <> {&ADDRESSTYPECODESYSTEM-DELIVERY} 
            and t_sDebtorEndUser.tcAddressTypeCode <> {&ADDRESSTYPECODESYSTEM-HEADOFFICE}
            /*User can only create addresses of these types in EndUser create: Enduser, Ship-To, HeadOffice*/
        then do:
            <M-37 run SetMessage
               (input  #T-15'You can only link an end user to an address of type End User, Head Office, or Ship-To.':255(216878175)T-15# (icMessage), 
                input  t_sDebtorEndUser.tcAddressTypeCode (icArguments), 
                input  't_sDebtorEndUser.tcAddressTypeCode':U (icFieldName), 
                input  t_sDebtorEndUser.tcAddressTypeCode (icFieldValue), 
                input  'E':U (icType), 
                input  3 (iiSeverity), 
                input  t_sDebtorEndUser.tc_Rowid (icRowid), 
                input  'qadfin-110620':U (icFcMsgNumber), 
                input  '':U (icFcExplanation), 
                input  '':U (icFcIdentification), 
                input  '':U (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BDebtorEndUser>
            assign oiReturnStatus = -1.
            return.
        end.
        
        if first-of (t_sDebtorEndUser.tcBusinessRelationCode)
        then do:        
            create tApiEndUserBusinessRelation.
            assign 
                viBusinessrelationCount = viBusinessrelationCount + 1
                tApiEndUserBusinessRelation.businessrelationcode = t_sDebtorEndUser.tcBusinessRelationCode
                tApiEndUserBusinessRelation.businessrelation_id = t_sDebtorEndUser.tiAddressBusinessRelation_ID            
                tApiEndUserBusinessRelation.tc_rowid = string(viBusinessrelationCount).
        end.    

        for first tApiEndUserAddress
            where tApiEndUserAddress.AddressStreet1      = t_sDebtorEndUser.tcAddressStreet1
              and tApiEndUserAddress.AddressStreet2      = t_sDebtorEndUser.tcAddressStreet2
              and tApiEndUserAddress.AddressStreet3      = t_sDebtorEndUser.tcAddressStreet3 
              and tApiEndUserAddress.AddressZip          = t_sDebtorEndUser.tcAddressZip
              and tApiEndUserAddress.AddressCity         = t_sDebtorEndUser.tcAddressCity
              and tApiEndUserAddress.tcAddressTypeCode   = t_sDebtorEndUser.tcAddressTypeCode
              and tApiEndUserAddress.tc_parentrowid      = tApiEndUserBusinessRelation.tc_rowid:
        end.

        if not available (tApiEndUserAddress)
        then do:
            
            /* get the Address_ID of this ship-to if exits */      
             <Q-54 run DebtorEndUserByID (all) (Read) (NoCache)
                (input t_sDebtorEndUser.DebtorEndUserCode, (DebtorEndUserCode)
                 input ?, (DebtorEndUser_ID)
                 input viCompanyId, (CompanyId)
                 output dataset tqDebtorEndUserByID) in BDebtorEndUser>
                 
            find first tqDebtorEndUserByID where tqDebtorEndUserByID.tcDebtorEndUserCode = t_sDebtorEndUser.DebtorEndUserCode no-error.     
        
            create tApiEndUserAddress.
            assign 
               viAddressCount = viAddressCount + 1
               tApiEndUserAddress.Address_ID          =  if available tqDebtorEndUserByID
                                                        then tqDebtorEndUserByID.tiAddress_ID
                                                        else ? 
               tApiEndUserAddress.AddressName         = t_sDebtorEndUser.tcAddressName
               tApiEndUserAddress.AddressStreet1      = t_sDebtorEndUser.tcAddressStreet1
               tApiEndUserAddress.AddressStreet2      = t_sDebtorEndUser.tcAddressStreet2
               tApiEndUserAddress.AddressStreet3      = t_sDebtorEndUser.tcAddressStreet3
               tApiEndUserAddress.AddressTelephone    = t_sDebtorEndUser.tcAddressTelephone
               tApiEndUserAddress.AddressCity         = t_sDebtorEndUser.tcAddressCity
               tApiEndUserAddress.AddressZip          = t_sDebtorEndUser.tcAddressZip
               tApiEndUserAddress.AddressEMail        = t_sDebtorEndUser.tcAddressEMail
               tApiEndUserAddress.AddressFax          = t_sDebtorEndUser.tcAddressFax
               tApiEndUserAddress.AddressIsTemporary  = t_sDebtorEndUser.tlAddressIsTemporary
               tApiEndUserAddress.tcAddressTypeCode   = t_sDebtorEndUser.tcAddressTypeCode
               tApiEndUserAddress.AddressWebSite      = t_sDebtorEndUser.tcAddressWebSite
               tApiEndUserAddress.tcCountryCode       = t_sDebtorEndUser.tcCountryCode
               tApiEndUserAddress.TxzTaxZone          = t_sDebtorEndUser.tcTxzTaxZone
               tApiEndUserAddress.tcCountyCode        = t_sDebtorEndUser.tcCountyCode
               tApiEndUserAddress.tcStateCode         = t_sDebtorEndUser.tcStateCode
               tApiEndUserAddress.AddressFormat       = t_sDebtorEndUser.tiAddressFormat
               tApiEndUserAddress.AddressIsTaxable    = t_sDebtorEndUser.tlAddressIsTaxable
               tApiEndUserAddress.AddressIsTaxInCity  = t_sDebtorEndUser.tlAddressIsTaxInCity
               tApiEndUserAddress.AddressSearchName   = t_sDebtorEndUser.tcAddressSearchName
               tApiEndUserAddress.AddressTaxIDState   = t_sDebtorEndUser.tcAddressTaxIDState     
               tApiEndUserAddress.tc_ParentRowid      = tApiEndUserBusinessRelation.tc_rowid
               tApiEndUserAddress.BusinessRelation_ID = tApiEndUserBusinessRelation.businessrelation_id
               tApiEndUserAddress.tcLngCode           = t_sDebtorEndUser.tcLngCode
               tApiEndUserAddress.AddressTaxIdMisc1   = t_sDebtorEndUser.tcAddressTaxIdMisc1
               tApiEndUserAddress.AddressTaxIdMisc2   = t_sDebtorEndUser.tcAddressTaxIdMisc2
               tApiEndUserAddress.AddressTaxIdMisc3   = t_sDebtorEndUser.tcAddressTaxIdMisc3
               tApiEndUserAddress.TxclTaxCls          = t_sDebtorEndUser.tcTxclTaxCls
               tApiEndUserAddress.TxuTaxUsage         = t_sDebtorEndUser.tcTxuTaxUsage 
               tApiEndUserAddress.AddressTaxIDFederal = t_sDebtorEndUser.tcAddresstaxIDFederal
               tApiEndUserAddress.AddressTaxDeclaration = t_sDebtorEndUser.tiAddressTaxDeclaration
               tApiEndUserAddress.AddressIsTaxIncluded  = t_sDebtorEndUser.tlAddressIsTaxIncluded 
               tApiEndUserAddress.tc_rowid              = string(viAddressCount).  
         end. /* address not available */
            
        /* Get the contacts for this address */
        for each t_sDebtorEndUserContact 
            where t_sDebtorEndUserContact.tc_ParentRowid = t_sDebtorEndUser.tc_rowid:
            
            for first tApiEndUserContact
                where tApiEndUserContact.contactname = t_sDebtorEndUserContact.ContactName
                  and tApiEndUserContact.tc_ParentRowid = tApiEndUserAddress.tc_rowid:
            end.

            if not available (tApiEndUserContact)
            then do:
                create tApiEndUserContact.
                buffer-copy t_sDebtorEndUserContact to tApiEndUserContact.
                assign tApiEndUserContact.tc_ParentRowid =  tApiEndUserAddress.tc_rowid. 
            end.
        end.    /* for each contact */
        
    end. /* for each t_sDebtorEndUser */


    if can-find(first tApiEndUserBusinessRelation)
    then do:
        /* disable address replication - the address will be replicated as part of the EndUser replication */
        if not valid-handle(vhSessionInst)
        then do:
            <I-79 {bFcOpenInstance
                &CLASS           = "Session"}>           
        end.

        <M-80 run SetLogicalValue
           (input  'ReplicationEnabled':U (icName), 
            input  False (ilValue), 
            output viFcReturnSuper (oiReturnStatus)) in Session>     
        if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturnStatus = 0 then assign viLocalReturnStatus = viFcReturnSuper.
        if viLocalReturnStatus < 0 then leave MAIN_BLOCK.
        
        /* Create/update the address in the Businress Relation */
        if viBBusinessRelation6Id = 0 or
           viBBusinessRelation6Id = ?
        then do: 
            <I-83 {bFcStartAndOpenInstance
                 &ADD-TO-TRANSACTION   = "true"
                 &CLASS                = "BBusinessRelation"}>
            assign vlIsBBusinessRelationStartedHr = true
                   vlIsBBusinessRelationOpenedHr  = true.
        end.
        else if valid-handle(vhBBusinessRelation6Inst)
        then do: 
            <I-84 {bFcOpenInstance
                 &CLASS           = "BBusinessRelation"}>
            assign vlIsBBusinessRelationOpenedHr = true.
        end.  /* end if viBBusinessRelation6Id */
        
        <M-85 run MaintainAddressMulti
           (input-output tApiEndUserBusinessRelation (tApiBusinessRelation), 
            input-output tApiEnduserAddress (tApiAddress), 
            input-output tApiEnduserContact (tApiContact), 
            output viFcReturnSuper (oiReturnStatus)) in BBusinessRelation> 
        if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturnStatus = 0 then assign viLocalReturnStatus = viFcReturnSuper.
        if viLocalReturnStatus < 0 then leave MAIN_BLOCK.
                  
        /* re-enable address replication - the address will be replicated as part of the EndUser replication */
        if not valid-handle(vhSessionInst)
        then do:
            <I-87 {bFcOpenInstance
                 &CLASS           = "Session"}>
        end.
            
        <M-88 run SetLogicalValue
           (input  'ReplicationEnabled':U (icName), 
            input  ? (ilValue), 
            output viFcReturnSuper (oiReturnStatus)) in Session>  
        if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturnStatus = 0 then assign viLocalReturnStatus = viFcReturnSuper.
        if viLocalReturnStatus < 0 then leave MAIN_BLOCK.

    end. /* if can-find(first tApiEndUserBusinessRelation) */


    /* ============================================================== */        
    /* PART 2 : BmfgEndUser                                           */
    /* ============================================================== */        
    PROCESSBLOCK:
    for each t_sDebtorEndUser
        break by t_sDebtorEndUser.DebtorEndUserCode:
        assign virowID = virowID + 1.

        if t_sDebtorEndUser.SharedSet_ID = ? or
           t_sDebtorEndUser.SharedSet_ID = 0
        then assign t_sDebtorEndUser.SharedSet_ID = viSharedSetID.
        
        /* ================================================================= */
        /* override value for identity fields in input                       */
        /* ================================================================= */
        /* Verify the debtor code passed in                                  */
        <Q-92 run DebtorByDebtorIDCodeNoJoins (all) (Read) (NoCache)
           (input viCompanyId, (CompanyId)
            input '':U, (DebtorID)
            input t_sDebtorEndUser.tcDebtorCode, (DebtorCode)
            output dataset tqDebtorByDebtorIDCodeNoJoins) in BDebtor>   
                                              
        for first tqDebtorByDebtorIDCodeNoJoins:  
           assign
               t_sDebtorEndUser.SharedSet_ID = tqDebtorByDebtorIDCodeNoJoins.tiSharedSet_ID
               t_sDebtorEndUser.tiDebtorBusinessRelation_ID = tqDebtorByDebtorIDCodeNoJoins.tiBusinessRelation_ID.           
        end.      


        /* If DebtorEndUserIsDebtor=TRUE, the address is taken from the HO address of the customer */
        if t_sDebtorEndUser.DebtorEndUserIsDebtor = TRUE 
        then do:
            if t_sDebtorEndUser.tiDebtorBusinessRelation_ID <> 0 and t_sDebtorEndUser.tiDebtorBusinessRelation_ID <> ?
            then do :
            
                <Q-39 run AddressByAddressType (all) (Read) (NoCache)
                   (input t_sDebtorEndUser.tiDebtorBusinessRelation_ID, (BusinessRelationId)
                    input {&ADDRESSTYPECODESYSTEM-HEADOFFICE}, (AddressTypeCode)
                    output dataset tqAddressByAddressType) in BBusinessRelation>
                    
                find first tqAddressByAddressType where 
                           tqAddressByAddressType.tiBusinessRelation_ID = t_sDebtorEndUser.tiDebtorBusinessRelation_ID
                           no-error.
                if available tqAddressByAddressType
                then assign t_sDebtorEndUser.Address_ID                   = tqAddressByAddressType.tiAddress_ID
                            t_sDebtorEndUser.tiAddressBusinessRelation_ID = tqAddressByAddressType.tiBusinessRelation_ID
                            t_sDebtorEndUser.tcBusinessRelationCode       = tqAddressByAddressType.tcBusinessRelationCode.
            end. /* t_sDebtorEndUser.tiDebtorBusinessRelation_ID <> 0 */
        end.
        else do:
            /* Save the address id and business relation id values back into the EndUser record */            
            for first tApiEndUserBusinessRelation where
                      tApiEndUserBusinessRelation.BusinessRelationCode = t_sDebtorEndUser.tcBusinessRelationCode,
                first tApiEndUserAddress where
                      tApiEndUserAddress.addressstreet1  = t_sDebtorEndUser.tcAddressStreet1      and
                      tApiEndUserAddress.addressstreet2  = t_sDebtorEndUser.tcAddressStreet2      and
                      tApiEndUserAddress.addressstreet3  = t_sDebtorEndUser.tcAddressStreet3      and
                      tApiEndUserAddress.addresscity     = t_sDebtorEndUser.tcaddresscity         and
                      tApiEndUserAddress.AddressZip      = t_sDebtorEndUser.tcAddressZip          and
                      tApiEndUserAddress.tcAddressTypeCode   = t_sDebtorEndUser.tcAddressTypeCode and
                      tApiEndUserAddress.BusinessRelation_ID = tApiEndUserBusinessRelation.businessrelation_id:
    
                assign t_sDebtorEndUser.Address_ID = tApiEndUserAddress.Address_ID
                       t_sDebtorEndUser.tiAddressBusinessRelation_ID = tApiEndUserAddress.BusinessRelation_ID.    
            end. /* for first */

        end. /* else do */

        /* ================================================================= */
        /* Lookup object by logical key                                      */
        /* When in create mode, skip lookup.                                 */
        /* ================================================================= */
        if vcActivityCode <> "Create":U
        then do:
            /* Skip run of calculate until last record is reached (performance reason) */
            if last(t_sDebtorEndUser.DebtorEndUserCode)
            then assign vlCalculateSkip = false.
            else assign vlCalculateSkip = true.
            
            if  vcActivityCode = "Modify":U
                and t_sDebtorEndUser.tcAddressTypeCode <> {&ADDRESSTYPECODESYSTEM-ENDUSER}
                /*User can only modify the endUser type address*/
            then do:
                <M-33 run SetMessage
                   (input  #T-62'You can only modify addresses of type End User.':255(789044420)T-62# (icMessage), 
                    input  t_sDebtorEndUser.tcAddressTypeCode (icArguments), 
                    input  't_sDebtorEndUser.tcAddressTypeCode':U (icFieldName), 
                    input  t_sDebtorEndUser.tcAddressTypeCode (icFieldValue), 
                    input  'E':U (icType), 
                    input  3 (iiSeverity), 
                    input  t_sDebtorEndUser.tc_Rowid (icRowid), 
                    input  'qadfin-253722':U (icFcMsgNumber), 
                    input  '':U (icFcExplanation), 
                    input  '':U (icFcIdentification), 
                    input  '':U (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BDebtorEndUser>
                assign oiReturnStatus = -1.
                return. 
            end.
            if t_sDebtorEndUser.tcAddressTypeCode <> '':U 
                    and vcActivityCode =  "Modify":U
            then do:
                <Q-70 run DebtorEndUserByAddressType (all) (Read) (NoCache)
                   (input ?, (CompanyId)
                    input t_sDebtorEndUser.DebtorEndUserCode, (DebtorEndUserCode)
                    output dataset tqDebtorEndUserByAddressType) in BDebtorEndUser >
                /*get the original AddressTypeCode in the database.*/
                find first tqDebtorEndUserByAddressType where 
                    tqDebtorEndUserByAddressType.tcDebtorEndUserCode = t_sDebtorEndUser.DebtorEndUserCode no-error.
                if available tqDebtorEndUserByAddressType
                then do:
                    if tqDebtorEndUserByAddressType.tcAddressTypeCode <> t_sDebtorEndUser.tcAddressTypeCode
                    /*User can not modify the addressType of the address.If the Adddress type is modified, then give an error.*/
                    then do:
                        <M-65 run SetMessage
                           (input  #T-97'You cannot change the address type of an end user address.':255(814602195)T-97# (icMessage), 
                            input  t_sDebtorEndUser.tcAddressTypeCode (icArguments), 
                            input  't_sDebtorEndUser.tcAddressTypeCode':U (icFieldName), 
                            input  t_sDebtorEndUser.tcAddressTypeCode (icFieldValue), 
                            input  'E':U (icType), 
                            input  3 (iiSeverity), 
                            input  t_sDebtorEndUser.tc_Rowid (icRowid), 
                            input  'qadfin-496527':U (icFcMsgNumber), 
                            input  '':U (icFcExplanation), 
                            input  '':U (icFcIdentification), 
                            input  '':U (icFcContext), 
                            output viFcReturnSuper (oiReturnStatus)) in BDebtorEndUser>  
                        assign oiReturnStatus = -1.
                        return. 
                    end.
                end.
            end.
            assign vcFreeform = "for each DebtorEndUser where DebtorEndUser.DebtorEndUserCode = '":U + t_sDebtorEndUser.DebtorEndUserCode + "'":U
                                + " and DebtorEndUser.SharedSet_ID = ":U + string(t_sDebtorEndUser.SharedSet_ID).
            <M-93 run DataLoad
               (input  '':U (icRowids), 
                input  '':U (icPkeys), 
                input  '':U (icObjectIds), 
                input  vcFreeform (icFreeform), 
                input  true (ilKeepPrevious), 
                output viFcReturnSuper (oiReturnStatus)) in BDebtorEndUser>
            
            if viFcReturnSuper >= 0
            then find first tDebtorEndUser where 
                            tDebtorEndUser.DebtorEndUserCode = t_sDebtorEndUser.DebtorEndUserCode
                            no-error.
        end.  /* if vcActivityCode <> Create */

        /* ================================================================= */
        /* 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_sDebtorEndUser.
            else do:
                if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturnStatus = 0 then assign viLocalReturnStatus = viFcReturnSuper.
                if viLocalReturnStatus < 0 then leave MAIN_BLOCK.
        
                /* ================================================================= */
                /* Update input to allow correct matching                            */
                /* ================================================================= */
                assign 
                    t_sDebtorEndUser.tc_Rowid  = tDebtorEndUser.tc_Rowid
                    t_sDebtorEndUser.tc_ParentRowid = ""
                    opPrimeKey   = (if opPrimeKey = "" then "" else opPrimeKey + chr(4))
                                 + string(tDebtorEndUser.DebtorEndUser_ID).
            end.
            next.
        end.  /* if vcActivityCode = Delete */

        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-77'Object with key $1 does not exist.':100(48021)T-77#).
                <M-94 run SetMessage
                   (input  vcMessage (icMessage), 
                    input  t_sDebtorEndUser.DebtorEndUserCode (icArguments), 
                    input  'tDebtorEndUser.DebtorEndUserCode':U (icFieldName), 
                    input  t_sDebtorEndUser.DebtorEndUserCode (icFieldValue), 
                    input  'E':U (icType), 
                    input  3 (iiSeverity), 
                    input  t_sDebtorEndUser.tc_Rowid (icRowid), 
                    input  'QadFin-9782':U (icFcMsgNumber), 
                    input  '':U (icFcExplanation), 
                    input  '':U (icFcIdentification), 
                    input  '':U (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BDebtorEndUser>                      
                
                assign viLocalReturnStatus = -1.
                leave MAIN_BLOCK.
            end.  /* if vcActivityCode = Modify */       

            /* ================================================================= */
            /* When object not found or in create mode, create it.               */
            /* ================================================================= */
            <M-95 run AddDetailLine
               (input  'DebtorEndUser':U (icTable), 
                input  '':U (icParentRowid), 
                output viFcReturnSuper (oiReturnStatus)) in BDebtorEndUser>
            if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturnStatus = 0 then assign viLocalReturnStatus = viFcReturnSuper.
            if viLocalReturnStatus < 0 then leave MAIN_BLOCK.
                    
            /* ================================================================= */
            /* Update input to allow correct matching                            */
            /* ================================================================= */
            assign t_sDebtorEndUser.tc_Status = "N":U.
        end.  /* if vcActivityCode = Create */
        else do:
            /* ================================================================= */
            /* Update input to allow correct matching                            */
            /* ================================================================= */
            assign 
                t_sDebtorEndUser.tc_Status = "C":U
                t_sDebtorEndUser.tlUpdateAllSharedAddRecords= false.       
        end.

        /* clear out contacts as we will not be saving them in this component */
        for each t_sDebtorEndUserContact:
            delete t_sDebtorEndUserContact.
        end.

        /* insert code for subtables here */                         
        assign t_sDebtorEndUser.DebtorEndUser_ID = tDebtorEndUser.DebtorEndUser_ID
                
        /* ================================================================= */
        /* Update input to allow correct matching                            */
        /* ================================================================= */
        t_sDebtorEndUser.tc_Rowid       = tDebtorEndUser.tc_Rowid
        t_sDebtorEndUser.tc_ParentRowid = ""

        opPrimeKey       = (if opPrimeKey = "" then "" else opPrimeKey + chr(4))
                         + string(tDebtorEndUser.DebtorEndUser_ID).
    end. /* PROCESSBLOCK: */
end. /* of MAIN_BLOCK */

/* Stop instances */
if valid-handle(vhSessionInst)
then do:
    <I-35 {bFcCloseInstance
         &CLASS           = "Session"}>
end.

if viLocalReturnStatus < 0
then do:
    <M-45 run StopExternalInstances  (output viFcReturnSuper (oiReturnStatus)) in BDebtorEndUser>
end.

/* Error handling */
assign oiReturnStatus = viLocalReturnStatus.