project QadFinancials > class BMfgAddress > method ApiReplicateFromFinancialsDebtor

Description

Replicate Debtor to MFG/PRO Address


Parameters


tDebtorsToUpdateinputtemp-table
icDomainCodeinputcharacterWhen the domaincode is filled --> Replicate only to this domain.
If the domaincode is empty --> Replicate to all domains in this shared set.
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BMfgCustomer.AdditionalUpdates


program code (program9/bmfgaddress.p)

MAIN_BLOCK:
do on error undo, leave:

    /* ====================== */
    /* Check input parameters */
    /* ====================== */
    if not can-find(first tDebtorsToUpdate)
    then leave MAIN_BLOCK.
    
    /* ================== */
    /* Exception Handling */
    /* ================== */
    assign oiReturnStatus                  = -98
           viLocalReturnStatus             = 0
           vcListAdMstr                    = '':U.
    
    <M-43 run GetAddressTypeId
       (output viHeadOfficeAddressTypeId (oiHeadOfficeAddressTypeId), 
        output viFcReturnSuper (oiReturnStatus)) in BMfgAddress>

    if viFcReturnSuper < 0 or viFcReturnSuper > 0 and oiReturnStatus = 0 then assign viLocalReturnStatus = viFcReturnSuper.
    if viLocalReturnStatus < 0 then leave MAIN_BLOCK.
        
    CONVERTBLOCK:
    DO:
        /* =============================================================================== */
        /* Check which records need to be loaded                                           */
        /* Even though the customer does not exist, ad_mstr still has exist. Ad_mstr is    */
        /* domain level, supply and customer could use the same address                    */
        /* =============================================================================== */
        for each tDebtorsToUpdate
            break by tDebtorsToUpdate.SharedSet_ID:
        
            /* ============================================================================================== */
            /* Check if the Debtor needs to be replicated to one domain or to all domains of this shared set  */
            /* If icDomainCode is left blank, all domains for this shared set will be replicated.             */
            /* ============================================================================================== */
            if first-of(tDebtorsToUpdate.SharedSet_ID)
            then do:
                <Q-2 run DomainBySharedSetDomain (all) (Read) (NoCache)
                   (input tDebtorsToUpdate.SharedSet_ID, (SharedSetId)
                    input 0, (DomainID)
                    input icDomainCode, (DomainCode)
                    output dataset tqDomainBySharedSetDomain) in BDomain>
            end. /* if first-of(tDebtorsToUpdate.SharedSet_ID) */
            
            <Q-83 run DomainSharedSetForMfgAddress (all) (Read) (NoCache)
               (input tDebtorsToUpdate.SharedSet_ID, (SharedSetId)
                input icDomainCode, (DomainCode)
                input tDebtorsToUpdate.DebtorCode, (Address)
                output dataset tqDomainSharedSetForMfgAddress) in BDomain >                

            if can-find(first tqDomainSharedSetForMfgAddress)
            then do:
                for each tqDomainBySharedSetDomain where
                         tqDomainBySharedSetDomain.tlDomainPropertyIsSetupCompl:
    
                    /* ============================================================ */
                    /* Create a list of all tad_mstr records that need to be loaded */
                    /* ============================================================ */
                    if can-find(first tqDomainSharedSetForMfgAddress where
                                      tqDomainSharedSetForMfgAddress.tcDomainCode = tqDomainBySharedSetDomain.tcDomainCode and
                                      tqDomainSharedSetForMfgAddress.tcad_addr    = tDebtorsToUpdate.DebtorCode)
                    then assign vcListAdMstr = vcListAdMstr                           + 
                                               (if vcListAdMstr = '':U
                                                then '':U
                                                else chr(4))                          +
                                               tqDomainBySharedSetDomain.tcDomainCode +
                                               chr(2)                                 +
                                               tDebtorsToUpdate.DebtorCode.
                    if length(vcListAdMstr, "CHARACTER") > 30000
                    then do:
                        
                        /* ========================= */
                        /* Load the tad_mstr records */
                        /* ========================= */
                        <M-22 run DataLoad
                           (input  '':U (icRowids), 
                            input  vcListAdMstr (icPkeys), 
                            input  '':U (icObjectIds), 
                            input  '':U (icFreeform), 
                            input  true (ilKeepPrevious), 
                            output viFcReturnSuper (oiReturnStatus)) in BMfgAddress>    
                                                 
                         if viFcReturnSuper <> 0
                         then assign viLocalReturnStatus = viFcReturnSuper.
        
                         assign vcListAdMstr = '':U.   
        
                    end. /* length */                   
                                               
                end. /* for each tqDomainBySharedSetDomain: */
            end. /* if can-find(first tqDomainSharedSetForMfgAddress) */
        end. /* for each tDebtorsToUpdate */
        
        /* ========================= */
        /* Load the tad_mstr records */
        /* ========================= */
        if vcListAdMstr <> "":U and 
           vcListAdMstr <> ?
        then do :
            <M-4 run DataLoad
               (input  '':U (icRowids), 
                input  vcListAdMstr (icPkeys), 
                input  '':U (icObjectIds), 
                input  '':U (icFreeform), 
                input  true (ilKeepPrevious), 
                output viFcReturnSuper (oiReturnStatus)) in BMfgAddress>
            if viFcReturnSuper <> 0
            then do:
                assign viLocalReturnStatus = viFcReturnSuper.
                leave CONVERTBLOCK.
            end. /* if viFcReturnSuper <> 0 */
        end. /* if vcListAdMstr <> "":U and */


        /* ========================================= */
        /* Convert table tDebtorsToUpdate to table tad_mstr */
        /* ========================================= */
        for each tDebtorsToUpdate
            break by tDebtorsToUpdate.SharedSet_ID:
        
            /* ============================================================================================== */
            /* Check if the shipto needs to be replicated to one domain or to all domains of this shared set  */
            /* If icDomainCode is left blank, all domains for this shared set will be replicated.             */
            /* ============================================================================================== */
            if first-of(tDebtorsToUpdate.SharedSet_ID)
            then do:
                if tDebtorsToUpdate.SharedSet_ID = 0 or
                   tDebtorsToUpdate.SharedSet_ID = ?
                then do:
                    assign vcMessage           = trim(substitute(#T-17'The shared set ID for this customer (&1) was not defined.':255(69584)T-17#, trim(string(tDebtorsToUpdate.DebtorCode)) ))
                           viLocalReturnStatus = -3.
                    <M-6 run SetMessage
                       (input  vcMessage (icMessage), 
                        input  '':U (icArguments), 
                        input  '':U (icFieldName), 
                        input  '':U (icFieldValue), 
                        input  'E':U (icType), 
                        input  1 (iiSeverity), 
                        input  '':U (icRowid), 
                        input  'QadFin-8234':U (icFcMsgNumber), 
                        input  '':U (icFcExplanation), 
                        input  '':U (icFcIdentification), 
                        input  '':U (icFcContext), 
                        output viFcReturnSuper (oiReturnStatus)) in BMfgAddress>
                    leave CONVERTBLOCK.
                end. /* if tDebtorsToUpdate.SharedSet_ID = 0 or */
    
                <Q-7 run DomainBySharedSetDomain (all) (Read) (Cache)
                   (input tDebtorsToUpdate.SharedSet_ID, (SharedSetId)
                    input 0, (DomainID)
                    input icDomainCode, (DomainCode)
                    output dataset tqDomainBySharedSetDomain) in BDomain >            
            end. /* if first-of(tDebtorsToUpdate.SharedSet_ID) */
            
            /* Select the HEADOFFICE address */
            <Q-58 run AddressForReplication (all) (Read) (NoCache)
               (input tDebtorsToUpdate.BusinessRelation_ID, (BusinessRelationId)
                input {&ADDRESSTYPECODESYSTEM-HEADOFFICE}, (AddressTypeCode)
                input viHeadOfficeAddressTypeId, (AddressTypeId)
                output dataset tqAddressForReplication) in BBusinessRelation>
    
            find tqAddressForReplication where
                 tqAddressForReplication.tiBusinessRelation_ID = tDebtorsToUpdate.BusinessRelation_ID and
                 tqAddressForReplication.tcAddressTypeCode     = {&ADDRESSTYPECODESYSTEM-HEADOFFICE}
                 no-lock no-error.
            if not available tqAddressForReplication
            then do:
                assign vcMessage           = trim(#T-98'No address record was defined for this customer.':255(69663)T-98#)
                       viLocalReturnStatus = -1.
                <M-26 run SetMessage
                   (input  vcMessage (icMessage), 
                    input  '':U (icArguments), 
                    input  '':U (icFieldName), 
                    input  '':U (icFieldValue), 
                    input  'E':U (icType), 
                    input  1 (iiSeverity), 
                    input  '':U (icRowid), 
                    input  'qadfin-793625':U (icFcMsgNumber), 
                    input  '':U (icFcExplanation), 
                    input  '':U (icFcIdentification), 
                    input  '':U (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BMfgAddress>
                leave CONVERTBLOCK.
            end. /* if not available tqAddressForReplication */
                
            <Q-52 run ContactByAddress (all) (Read) (NoCache)
               (input tqAddressForReplication.tiAddress_ID, (AddressId)
                output dataset tqContactByAddress) in BBusinessRelation>
                                
            for each tqDomainBySharedSetDomain where
                     tqDomainBySharedSetDomain.tlDomainPropertyIsSetupCompl:

                /* ================================================================= */
                /* Create or update tad_mstr records based on tDebtorsToUpdate */
                /* ================================================================= */

                find tad_mstr where
                         tad_mstr.ad_addr   = tDebtorsToUpdate.DebtorCode and
                         tad_mstr.ad_domain = tqDomainBySharedSetDomain.tcDomainCode
                         no-error.

                if not available tad_mstr
                then do:
                    if tDebtorsToUpdate.tc_Status = "D"
                    then next.
    
                    <M-8 run AddDetailLine
                       (input  'ad_mstr':U (icTable), 
                        input  '':U (icParentRowid), 
                        output viFcReturnSuper (oiReturnStatus)) in BMfgAddress>
                    if viFcReturnSuper <> 0
                    then do:
                        assign viLocalReturnStatus = viFcReturnSuper.
                        leave CONVERTBLOCK.
                    end. /* if viFcReturnSuper <> 0 */                
                end. /* if not available tad_mstr */  
                else assign tad_mstr.tc_Status = "C":U.
    
                assign tad_mstr.ad_addr         = tDebtorsToUpdate.DebtorCode
                       tad_mstr.ad_domain       = tqDomainBySharedSetDomain.tcDomainCode
                       tad_mstr.ad_bus_relation = tqAddressForReplication.tcBusinessRelationCode
                       tad_mstr.ad_line1        = tqAddressForReplication.tcAddressStreet1
                       tad_mstr.ad_line2        = tqAddressForReplication.tcAddressStreet2
                       tad_mstr.ad_line3        = tqAddressForReplication.tcAddressStreet3
                       tad_mstr.ad_zip          = tqAddressForReplication.tcAddressZip
                       tad_mstr.ad_city         = tqAddressForReplication.tcAddressCity
                       tad_mstr.ad_state        = tqAddressForReplication.tcAddressState
                       tad_mstr.ad_county       = tqAddressForReplication.tcCountyCode
                       tad_mstr.ad_state        = tqAddressForReplication.tcStateCode
                       tad_mstr.ad_ctry         = tqAddressForReplication.tcCountryCode
                       tad_mstr.ad_country      = tqAddressForReplication.tcCountryDescription
                       tad_mstr.ad_lang         = tqAddressForReplication.tcBusinessRelationLngCode
                       tad_mstr.ad_name         = tqAddressForReplication.tcAddressName
                       tad_mstr.ad_sort         = tqAddressForReplication.tcAddressSearchName
                       tad_mstr.ad_address_id   = tqAddressForReplication.tiAddress_ID
                       tad_mstr.ad_mod_date     = tqAddressForReplication.ttLastModifiedDate
                       tad_mstr.ad_type         = "customer":U 
                       tad_mstr.ad_taxable      = tqAddressForReplication.tlAddressIsTaxable
                       tad_mstr.ad_tax_zone     = tqAddressForReplication.tcTxzTaxZone
                       tad_mstr.ad_taxc         = tqAddressForReplication.tcTxclTaxCls
                       tad_mstr.ad_tax_usage    = tqAddressForReplication.tcTxuTaxUsage 
                       tad_mstr.ad_gst_id       = tqAddressForReplication.tcAddressTaxIDFederal
                       tad_mstr.ad_pst_id       = tqAddressForReplication.tcAddressTaxIDState
                       tad_mstr.ad_misc1_id     = tqAddressForReplication.tcAddressTaxIDMisc1
                       tad_mstr.ad_misc2_id     = tqAddressForReplication.tcAddressTaxIDMisc2
                       tad_mstr.ad_misc3_id     = tqAddressForReplication.tcAddressTaxIDMisc3
                       tad_mstr.ad_format       = tqAddressForReplication.tiAddressFormat
                       tad_mstr.ad_userid       = tqAddressForReplication.tcLastModifiedUser 
                       tad_mstr.ad_phone        = substring(tqAddressForReplication.tcAddressTelephone, 1, 20, "CHARACTER")
                       tad_mstr.ad_fax          = substring(tqAddressForReplication.tcAddressFax, 1, 20, "CHARACTER")
                       tad_mstr.ad_email        = substring(tqAddressForReplication.tcAddressEMail, 1, 48, "CHARACTER")
                       tad_mstr.ad_tax_in_city  = tqAddressForReplication.tlAddressIsTaxInCity
                       tad_mstr.ad_tax_in       = tqAddressForReplication.tlAddressIsTaxIncluded
                       tad_mstr.ad_date         = (if tad_mstr.ad_date = ?
	 	                                           then today
	 	                                           else tad_mstr.ad_date).

                if tDebtorsToUpdate.tc_Status = "D":U
                then do:
                   /* check if address already exists */
                   <Q-39 run AddressListDetailByDomainCode (all) (Read) (NoCache)
                      (input tDebtorsToUpdate.DebtorCode, (AddressCode)
                       input tad_mstr.ad_domain, (domain)
                       input ?, (AddressType)
                       output dataset tqAddressListDetailByDomainCode) in BMfgAddressListDetail>
    
                   find first tqAddressListDetailByDomainCode
                       where tqAddressListDetailByDomainCode.tcls_addr = tDebtorsToUpdate.DebtorCode
                         and tqAddressListDetailByDomainCode.tcls_domain = tad_mstr.ad_domain
                         and tqAddressListDetailByDomainCode.tcls_type <> "customer":U 
                   no-lock no-error.
                     
                   if available tqAddressListDetailByDomainCode 
                   then assign tad_mstr.tc_Status = tad_mstr.tc_Status.      
                   else assign tad_mstr.tc_Status = tDebtorsToUpdate.tc_Status. 
                end.

                /* retrieve the contacts for this address from the financials database*/
                
                for each tqContactByAddress where
                         tqContactByAddress.tiAddress_ID = tqAddressForReplication.tiAddress_ID:

                    if tqContactByAddress.tlContactIsPrimary
                    then assign ad_attn  = tqContactByAddress.tcContactName
                                ad_phone = tqContactByAddress.tcContactTelephone
                                ad_fax   = tqContactByAddress.tcContactFax
                                ad_email = tqContactByAddress.tcContactEmail.
                    else if tqContactByAddress.tlContactIsSecondary
                         then assign ad_attn2  = tqContactByAddress.tcContactName
                                     ad_phone2 = tqContactByAddress.tcContactTelephone
                                     ad_fax2   = tqContactByAddress.tcContactFax
                                     ad_email2 = tqContactByAddress.tcContactEmail.  

                end.  /* end of for each contact */
            end. /* for each tqDomainBySharedSetDomain: */
        end. /* for each tDebtorsToUpdate */
    end. /* CONVERTBLOCK */
    
    VALIDATEBLOCK:
    DO:
        if viLocalReturnStatus < 0
        then leave VALIDATEBLOCK.
    
        /* ========== */
        /* ValidateBC */
        /* ========== */
        <M-14 run ValidateBC  (output viFcReturnSuper (oiReturnStatus)) in BMfgAddress>
        if viFcReturnSuper <> 0
        then assign viLocalReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0
        then leave VALIDATEBLOCK.
    
        /* ================== */
        /* Additional Updates */
        /* ================== */
        <M-15 run AdditionalUpdates  (output viFcReturnSuper (oiReturnStatus)) in BMfgAddress>
        if viFcReturnSuper <> 0
        then assign viLocalReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0
        then leave VALIDATEBLOCK.
        
        /* ======== */
        /* DataSave */
        /* ======== */
        <M-16 run DataSave  (output viFcReturnSuper (oiReturnStatus)) in BMfgAddress>
        if viFcReturnSuper <> 0
        then assign viLocalReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0
        then leave VALIDATEBLOCK.

    end. /* VALIDATEBLOCK */
end. /* MAIN_BLOCK */    
    
/* ================== */                        
/* Exception Handling */
/* ================== */
assign oiReturnStatus = viLocalReturnStatus.


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 = "BMfgAddress".
create ttContext.
assign ttContext.propertyName = "methodName"
       ttContext.propertyValue = "ApiReplicateFromFinancialsDebtor".
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/bmfgaddress.apireplicatefromfinancialsdebtor.i.xsd", ?).
vhParameter = vhInputDS:get-buffer-handle("tParameterI").
vhParameter:buffer-create().
assign vhParameter::icDomainCode = <parameter value>.

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