project QadFinancials > class BMfgAddress > method ApiReplicateFromFinancialsShipTo

Description

Replicate DebtorShipto to MFG/PRO Ad_mstr


Parameters


tDebtorShiptosToUpdateinputtemp-table
icDomainCodeinputcharacterWhen the domaincode is filled --> Replicate only to this domain.
If the domaincode is empty --> Replicate to all domains in this shared set.
iiBBusinessRelationIDinputintegerBusiness relation instance ID
ihBBusinessRelationInstinputhandle
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BDebtorShipTo.AdditionalUpdates
method BDebtorShipTo.ApiReplicateFromDaemon


program code (program9/bmfgaddress.p)

MAIN_BLOCK:
do on error undo, leave:
/* ====================== */
/* Check input parameters */
/* ====================== */
if not can-find(first tDebtorShiptosToUpdate)
then return.

    /* ===================================================================================== */
    /* Overtake started instances                                                            */
    /* ===================================================================================== */
    assign viBBusinessRelation7ID   = iiBBusinessRelationID
           vhBBusinessRelation7Inst = ihBBusinessRelationInst.
    
    /* ================== */
    /* Exception Handling */
    /* ================== */
    assign oiReturnStatus                  = -98
           viLocalReturnStatus             = 0
           vcListAdMstr                    = '':U.

    CONVERTBLOCK:
    DO:
    
        /* ============================================================================== *
         * Replicate ship-to addresses which are linked to another customer               *
         * ============================================================================== */
        if can-find( first tDebtorShiptosToUpdate where
                           tDebtorShiptosToUpdate.DebtorShipToIsDebtor )
        then do:
            <M-59 run ReplicateFromFinancialsShipToDebtorLink
               (input  tDebtorShiptosToUpdate (tDebtorShiptosToUpdate), 
                input  icDomainCode (icDomainCode), 
                output viFcReturnSuper (oiReturnStatus)) in BMfgAddress>
            if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturnStatus = 0 then assign viLocalReturnStatus = viFcReturnSuper.
            if viLocalReturnStatus < 0 then leave MAIN_BLOCK.
        end.
    
    
        /* ============================================================================== */
        /* Check which records need to be loaded */
        /* We can skip those DebtorShipTo addresses, which are link to Customer and       */
        /* End-user addresses, as they are aldrey created in the operational database     */
        /* and cannot be changed in this case in the DebtorShipTo maintenance             */
        /* ============================================================================== */
        for each tDebtorShiptosToUpdate where
                 tDebtorShiptosToUpdate.DebtorShipToIsDebtor  = false and
                 (tDebtorShiptosToUpdate.DebtorEndUser_ID     =  0 or
                  tDebtorShiptosToUpdate.DebtorEndUser_ID     = ?)
            break by tDebtorShiptosToUpdate.SharedSet_ID:
    
            /* ============================================================================================== */
                /* Check if the record 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(tDebtorShiptosToUpdate.SharedSet_ID)
            then do:
                <Q-16 run DomainBySharedSetDomain (all) (Read) (Cache)
                   (input tDebtorShiptosToUpdate.SharedSet_ID, (SharedSetId)
                    input 0, (DomainID)
                    input icDomainCode, (DomainCode)
                    output dataset tqDomainBySharedSetDomain) in BDomain >
            end. /* if first-of(tDebtorShiptosToUpdate.SharedSet_ID) */
            
            /* =============================================================================================== */
            /* Get all Mfg Addresses of the DebtorShipTo which are already defined in the database             */
            /* =============================================================================================== */
            <Q-76 run DomainSharedSetForMfgAddress (all) (Read) (Cache)
               (input tDebtorShiptosToUpdate.SharedSet_ID, (SharedSetId)
                input icDomainCode, (DomainCode)
                input tDebtorShiptosToUpdate.DebtorShipToCode, (Address)
                output dataset tqDomainSharedSetForMfgAddress) in BDomain >
                
            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    = tDebtorShiptosToUpdate.DebtorShipToCode)
                then assign vcListAdMstr = vcListAdMstr                           + 
                                           (if vcListAdMstr = '':U
                                            then '':U
                                            else chr(4))                          +
                                           tqDomainBySharedSetDomain.tcDomainCode +
                                           chr(2)                                 +
                                           tDebtorShiptosToUpdate.DebtorShipToCode.
                                           
                if length(vcListAdMstr, "CHARACTER") > 30000
                then do:
                    
                    /* ========================= */
                    /* Load the tad_mstr records */
                    /* ========================= */
                      <M-36 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: */

            /* =============================================================================================== */
            /* Get all Mfg Addresses which are linked to the same Finance Ship-toaddress (like another Debtor  */
            /* Ship To or End-user                                                                             */
            /* Update operational addresses only when Ship-to address was changed                              */
            /* =============================================================================================== */
            if tDebtorShiptosToUpdate.tc_Status = "C":U
            then do:
                <Q-42 run AddressByFinancialAddress (all) (Read) (Cache)
                   (input tDebtorShiptosToUpdate.Address_ID, (AddressID)
                    output dataset tqAddressByFinancialAddress) in BMfgAddress >

                for each tqAddressByFinancialAddress where
                         tqAddressByFinancialAddress.tiad_address_id = tDebtorShiptosToUpdate.Address_ID:
                  
                    /* This record was already processed above*/
                    if can-find(first tqDomainSharedSetForMfgAddress where
                                      tqDomainSharedSetForMfgAddress.tcDomainCode = tqAddressByFinancialAddress.tcad_domain and
                                      tqDomainSharedSetForMfgAddress.tcad_addr    = tqAddressByFinancialAddress.tcad_addr)
                    then next.

                    assign vcListAdMstr = vcListAdMstr                           + 
                                          (if vcListAdMstr = '':U
                                           then '':U
                                           else chr(4))                           +
                                          tqAddressByFinancialAddress.tcad_domain +
                                          chr(2)                                  +
                                          tqAddressByFinancialAddress.tcad_addr.
                    if length(vcListAdMstr, "CHARACTER") > 30000
                    then do:
                        /* ========================= */
                        /* Load the tad_mstr records */
                        /* ========================= */
                          <M-97 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 tqAddressByFinancialAddress then */
            end. /* if tDebtorShiptosToUpdate.tc_Status = "C":U */
        end. /* for each tDebtorShiptosToUpdate where */
    
        /* ========================= */
        /* Load the tad_mstr records */
        /* ========================= */
        if vcListAdMstr <> "":U and 
           vcListAdMstr <> ?
        then do :
            <M-18 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 vcListVdMstr <> "":U and */    
        
        /* ========================================= */
        /* Convert table tDebtorShiptosToUpdate to table tad_mstr */
        /* ========================================= */
        for each tDebtorShiptosToUpdate where
                 tDebtorShiptosToUpdate.DebtorShipToIsDebtor = false and
                 (tDebtorShiptosToUpdate.DebtorEndUser_ID    =  0 or
                  tDebtorShiptosToUpdate.DebtorEndUser_ID    = ?)
            break by tDebtorShiptosToUpdate.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(tDebtorShiptosToUpdate.SharedSet_ID)
            then do:
                if tDebtorShiptosToUpdate.SharedSet_ID = 0 or
                   tDebtorShiptosToUpdate.SharedSet_ID = ?
                then do:
                    assign vcMessage           = trim(substitute(#T-29'The shared set ID for customer ship-to (&1) was not defined.':255(69639)T-29#, trim(string(tDebtorShiptosToUpdate.DebtorShipToCode)) ))
                           viLocalReturnStatus = -3.
                    <M-20 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-8212':U (icFcMsgNumber), 
                        input  '':U (icFcExplanation), 
                        input  '':U (icFcIdentification), 
                        input  '':U (icFcContext), 
                        output viFcReturnSuper (oiReturnStatus)) in BMfgAddress>
                    leave CONVERTBLOCK.
                end. /* if tDebtorShiptosToUpdate.SharedSet_ID = 0 or */

                <Q-21 run DomainBySharedSetDomain (all) (Read) (Cache)
                   (input tDebtorShiptosToUpdate.SharedSet_ID, (SharedSetId)
                    input 0, (DomainID)
                    input icDomainCode, (DomainCode)
                    output dataset tqDomainBySharedSetDomain) in BDomain >            
            end. /* if first-of(tDebtorShiptosToUpdate.SharedSet_ID) */
            
            /* ========================================================================= *
             *              Get Address and Contact data of the Mfg Address              *
             * ========================================================================= *
             * Address can be created newly, or existing one can be reused               *
             * ========================================================================= */
            /* First check, if the business relation component contains the address data */
            empty temp-table tAddress.
            empty temp-table tContactsToUpdate.
    
            if viBBusinessRelation7ID <> 0 and
               viBBusinessRelation7ID <> ?
            then do:
                if not valid-handle(vhBBusinessRelation7Inst)
                then do:
                    <I-80 {bFcOpenInstance
                         &CLASS           = "BBusinessRelation"}>
                    assign vlIsBBusinessRelationOpenedHr = true.
                end.
                
                <M-86 run GetAddressWithID
                   (input  tDebtorShiptosToUpdate.Address_ID (iiAddressID), 
                    input-output tAddress (tApiAddress), 
                    output viFcReturnSuper (oiReturnStatus)) in BBusinessRelation>
                if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturnStatus = 0 then assign viLocalReturnStatus = viFcReturnSuper.
                if viLocalReturnStatus < 0 then leave CONVERTBLOCK.
    
                <M-14 run GetContactByAddress
                   (input  tDebtorShiptosToUpdate.Address_ID (iiAddressID), 
                    input-output tContactsToUpdate (tApiContact), 
                    output viFcReturnSuper (oiReturnStatus)) in BBusinessRelation>
                if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturnStatus = 0 then assign viLocalReturnStatus = viFcReturnSuper.
                if viLocalReturnStatus < 0 then leave CONVERTBLOCK.
            end.
             
            if not can-find(first tAddress where
                                  tAddress.Address_ID =  tDebtorShiptosToUpdate.Address_ID)
            then do:
                /* Get address details */
                <Q-94 run AddressById (all) (Read) (NoCache)
                   (input tDebtorShiptosToUpdate.Address_ID, (AddressID)
                    output dataset tqAddressById) in BBusinessRelation >
                for each tqAddressById where
                         tqAddressById.tiAddress_ID = tDebtorShiptosToUpdate.Address_ID:
                    
                    create tAddress.
                    <M-48 run BufferCopy
                       (input  buffer tqAddressById:handle (ihFrom), 
                        input  buffer tAddress:handle (ihTo), 
                        output viFcReturnSuper (oiReturnStatus)) in BMfgAddress>
                    if viFcReturnSuper <> 0 then assign viLocalReturnStatus = viFcReturnSuper.
                    if viFcReturnSuper <  0 then leave CONVERTBLOCK.
    
                end.
    
                /* Get contact details */
                <Q-72 run ContactByAddress (all) (Read) (NoCache)
                   (input tDebtorShiptosToUpdate.Address_ID, (AddressId)
                    output dataset tqContactByAddress) in BBusinessRelation >
                for each tqContactByAddress where
                         tqContactByAddress.tiAddress_ID = tDebtorShiptosToUpdate.Address_ID:
                    create tContactsToUpdate.
                    <M-53 run BufferCopy
                       (input  buffer tqContactByAddress:handle (ihFrom), 
                        input  buffer tContactsToUpdate:handle (ihTo), 
                        output viFcReturnSuper (oiReturnStatus)) in BMfgAddress>
                end.
            end.
    
            find first tAddress where
                       tAddress.Address_ID =  tDebtorShiptosToUpdate.Address_ID
                 no-error.
            if not available tAddress
            then do:
                assign vcMessage           = trim(#T-77'Address record not available for this customer Ship-to.':255(69641)T-77#)
                       viLocalReturnStatus = -1.
                <M-46 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-657397':U (icFcMsgNumber), 
                    input  '':U (icFcExplanation), 
                    input  '':U (icFcIdentification), 
                    input  '':U (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BMfgAddress>
                leave CONVERTBLOCK.
            end.
    
            for each tqDomainBySharedSetDomain  where
                     tqDomainBySharedSetDomain.tlDomainPropertyIsSetupCompl:
                /* ================================================================= */
                /* Create or update tad_mstr records based on tDebtorShiptosToUpdate */
                /* ================================================================= */
                find tad_mstr where
                     tad_mstr.ad_addr   = tDebtorShiptosToUpdate.DebtorShiptoCode and
                     tad_mstr.ad_domain = tqDomainBySharedSetDomain.tcDomainCode
                     no-error.
                if not available tad_mstr
                then do:
                    if tDebtorShiptosToUpdate.tc_Status = "D"
                    then next.

                    <M-22 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.

                    /* Update data */
                assign tad_mstr.ad_addr        = tDebtorShiptosToUpdate.DebtorShipToCode
                       tad_mstr.ad_ref    = tDebtorShiptosToUpdate.tcDebtorCode
                       tad_mstr.ad_domain = tqDomainBySharedSetDomain.tcDomainCode.

                <M-2 run ReplicateFromFinancialsShipToUpd  (output viFcReturnSuper (oiReturnStatus)) in BMfgAddress>
                if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturnStatus = 0 then assign viLocalReturnStatus = viFcReturnSuper.
                if viFcReturnSuper < 0 then leave MAIN_BLOCK.
            end. /* for each tqDomainBySharedSetDomain: */
                
            /* Update also related data */
            for each tad_mstr where
                     tad_mstr.ad_address_id = tAddress.Address_ID and
                     tad_mstr.ad_addr      <> tDebtorShiptosToUpdate.DebtorShiptoCode:
                
                assign tad_mstr.tc_Status = (if tad_mstr.tc_Status = "":U then "C" else tad_mstr.tc_Status).
                
                <M-11 run ReplicateFromFinancialsShipToUpd  (output viFcReturnSuper (oiReturnStatus)) in BMfgAddress>
                if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturnStatus = 0 then assign viLocalReturnStatus = viFcReturnSuper.
                if viFcReturnSuper < 0 then leave MAIN_BLOCK.
            end.                    
        end. /* for each tDebtorShiptosToUpdate */
    end. /* CONVERTBLOCK */

    VALIDATEBLOCK:
    DO:
        if viLocalReturnStatus < 0
        then leave VALIDATEBLOCK.

        /* ========== */
        /* ValidateBC */
        /* ========== */
        <M-26 run ValidateBC  (output viFcReturnSuper (oiReturnStatus)) in BMfgAddress>
        if viFcReturnSuper <> 0
        then assign viLocalReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0
        then leave VALIDATEBLOCK.

        /* ================== */
        /* Additional Updates */
        /* ================== */
        <M-27 run AdditionalUpdates  (output viFcReturnSuper (oiReturnStatus)) in BMfgAddress>
        if viFcReturnSuper <> 0
        then assign viLocalReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0
        then leave VALIDATEBLOCK.
        
        /* ======== */
        /* DataSave */
        /* ======== */
        <M-28 run DataSave  (output viFcReturnSuper (oiReturnStatus)) in BMfgAddress>
        if viFcReturnSuper <> 0
        then assign viLocalReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0
        then leave VALIDATEBLOCK.
    end. /* VALIDATEBLOCK */
end. /* of MAIN_BLOCK */

/* Close components */
if vlIsBBusinessRelationOpenedHr and
   valid-handle(vhBBusinessRelation7Inst)
then do:
    <I-82 {bFcCloseInstance
         &CLASS           = "BBusinessRelation"}>
    assign vlIsBBusinessRelationOpenedHr = false.
end.

/* ================== */                        
/* 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 = "ApiReplicateFromFinancialsShipTo".
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.apireplicatefromfinancialsshipto.i.xsd", ?).
vhParameter = vhInputDS:get-buffer-handle("tParameterI").
vhParameter:buffer-create().
assign vhParameter::icDomainCode = <parameter value>
       vhParameter::iiBBusinessRelationID = <parameter value>
       vhParameter::ihBBusinessRelationInst = <parameter value>.

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