project QadFinancials > class BMfgAddress > method ApiReplicateFromFinancialsEndUSer

Description

Replicate DebtorEndUser to MFG/PRO Ad_mstr


Parameters


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


Internal usage


QadFinancials
method BMfgEndUser.AdditionalUpdates


program code (program9/bmfgaddress.p)

/* ====================== */
/* Check input parameters */
/* ====================== */
if not can-find(first tDebtorEndUsersToUpdate)
then return.

/* ===================================================================================== */
/* Overtake started instances                                                            */
/* ===================================================================================== */
if iiBBusinessRelationID <> 0 and
   iiBBusinessRelationID <> ?
then assign viBBusinessRelation7ID = iiBBusinessRelationID.

/* ================== */
/* Exception Handling */
/* ================== */
assign oiReturnStatus                  = -98
       viLocalReturnStatus             = 0
       vcListAdMstr                    = '':U.

CONVERTBLOCK:
DO:
    /* ===================================== */
    /* Check which records need to be loaded */
    /* ===================================== */
    for each tDebtorEndUsersToUpdate
        break by tDebtorEndUsersToUpdate.SharedSet_ID:
    
        /* ============================================================================================== */
        /* Check if the Account 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(tDebtorEndUsersToUpdate.SharedSet_ID)
        then do:
            <Q-37 run DomainBySharedSetDomain (all) (Read) (Cache)
               (input tDebtorEndUsersToUpdate.SharedSet_ID, (SharedSetId)
                input 0, (DomainID)
                input icDomainCode, (DomainCode)
                output dataset tqDomainBySharedSetDomain) in BDomain >
        end. /* if first-of(tDebtorEndUsersToUpdate.SharedSet_ID) */
            
        /* =============================================================================================== */
        /* Get all Mfg Addresses which are already defined in the database                                 */
        /* =============================================================================================== */
        <Q-4 run DomainSharedSetForMfgAddress (all) (Read) (NoCache)
           (input tDebtorEndUsersToUpdate.SharedSet_ID, (SharedSetId)
            input icDomainCode, (DomainCode)
            input tDebtorEndUsersToUpdate.DebtorEndUserCode, (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    = tDebtorEndUsersToUpdate.DebtorEndUserCode)
            then assign vcListAdMstr = vcListAdMstr                           + 
                                       (if vcListAdMstr = '':U
                                        then '':U
                                        else chr(4))                          +
                                       tqDomainBySharedSetDomain.tcDomainCode +
                                       chr(2)                                 +
                                       tDebtorEndUsersToUpdate.DebtorEndUserCode.
            if length(vcListAdMstr, "CHARACTER") > 30000
            then do:
                
                /* ========================= */
                /* Load the tad_mstr records */
                /* ========================= */
                 <M-56 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 address (like another Debtorh Ship To or          */
        /* End-user                                                                                        */
        /* =============================================================================================== */
        if tDebtorEndUsersToUpdate.tc_Status = "C":U
        then do:
            <Q-82 run AddressByFinancialAddress (all) (Read) (NoCache)
               (input tDebtorEndUsersToUpdate.Address_ID, (AddressID)
                output dataset tqAddressByFinancialAddress) in BMfgAddress >

            for each tqAddressByFinancialAddress where
                     tqAddressByFinancialAddress.tiad_address_id = tDebtorEndUsersToUpdate.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-599 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 tDebtorEndUsersToUpdate.tc_Status = "C":U */
    end. /* for each tDebtorEndUsersToUpdate */
    
    /* ========================= */
    /* Load the tad_mstr records */
    /* ========================= */
    if vcListAdMstr <> "":U and 
       vcListAdMstr <> ?
    then do :
        <M-51 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 tDebtorEndUsersToUpdate to table tad_mstr */
    /* ========================================= */
    for each tDebtorEndUsersToUpdate
        break by tDebtorEndUsersToUpdate.SharedSet_ID:
    
        /* ================================================================================================ */
        /* Check if the End User 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(tDebtorEndUsersToUpdate.SharedSet_ID)
        then do:
            if tDebtorEndUsersToUpdate.SharedSet_ID = 0 or
               tDebtorEndUsersToUpdate.SharedSet_ID = ?
            then do:
                assign vcMessage           = trim(substitute(#T-54'The shared set ID for customer end user (&1) was not defined.':255(69664)T-54#, trim(string(tDebtorEndUsersToUpdate.DebtorEndUserCode)) ))
                       viLocalReturnStatus = -3.
                <M-39 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-8239':U (icFcMsgNumber), 
                    input  '':U (icFcExplanation), 
                    input  '':U (icFcIdentification), 
                    input  '':U (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BMfgAddress>
                leave CONVERTBLOCK.
            end. /* if tDebtorEndUsersToUpdate.SharedSet_ID = 0 or */

            <Q-43 run DomainBySharedSetDomain (all) (Read) (Cache)
               (input tDebtorEndUsersToUpdate.SharedSet_ID, (SharedSetId)
                input 0, (DomainID)
                input icDomainCode, (DomainCode)
                output dataset tqDomainBySharedSetDomain) in BDomain >            
        end. /* if first-of(tDebtorEndUsersToUpdate.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-33 {bFcOpenInstance
                     &CLASS           = "BBusinessRelation"}>
            end.
            
            <M-11 run GetAddressWithID
               (input  tDebtorEndUsersToUpdate.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-96 run GetContactByAddress
               (input  tDebtorEndUsersToUpdate.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 =  tDebtorEndUsersToUpdate.Address_ID)
        then do:
            /* Get address details */
            <Q-68 run AddressById (all) (Read) (NoCache)
               (input tDebtorEndUsersToUpdate.Address_ID, (AddressID)
                output dataset tqAddressById) in BBusinessRelation >
            for each tqAddressById where
                     tqAddressById.tiAddress_ID = tDebtorEndUsersToUpdate.Address_ID:
                
                create tAddress.
                <M-74 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-73 run ContactByAddress (all) (Read) (NoCache)
               (input tDebtorEndUsersToUpdate.Address_ID, (AddressId)
                output dataset tqContactByAddress) in BBusinessRelation >
            for each tqContactByAddress where
                     tqContactByAddress.tiAddress_ID = tDebtorEndUsersToUpdate.Address_ID:
                create tContactsToUpdate.
                <M-57 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 =  tDebtorEndUsersToUpdate.Address_ID
             no-error.
        if not available tAddress
        then do:
            assign vcMessage           = trim(#T-14'Address record not available for this customer Ship-to.':255(69641)T-14#)
                   viLocalReturnStatus = -1.
            <M-94 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-491772':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 tDebtorEndUsersToUpdate */
            /* ================================================================= */
            find tad_mstr where
                 tad_mstr.ad_addr   = tDebtorEndUsersToUpdate.DebtorEndUserCode and
                 tad_mstr.ad_domain = tqDomainBySharedSetDomain.tcDomainCode
                 no-error.
            if not available tad_mstr
            then do:
                if tDebtorEndUsersToUpdate.tc_Status = "D"
                then next.

                <M-44 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        = tDebtorEndUsersToUpdate.DebtorEndUserCode
                   tad_mstr.ad_ref         = tDebtorEndUsersToUpdate.tcDebtorCode
                   tad_mstr.ad_domain      = tqDomainBySharedSetDomain.tcDomainCode.

            <M-75 run ReplicateFromFinancialsEndUserUpd  (output viFcReturnSuper (oiReturnStatus)) in BMfgAddress>
            if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturnStatus = 0 then assign viLocalReturnStatus = viFcReturnSuper.
            if viFcReturnSuper < 0 then leave CONVERTBLOCK.
        end. /* for each tqDomainBySharedSetDomain: */

        /* Update also related addresses */
        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-7 run ReplicateFromFinancialsEndUserUpd  (output viFcReturnSuper (oiReturnStatus)) in BMfgAddress>
            if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturnStatus = 0 then assign viLocalReturnStatus = viFcReturnSuper.
            if viFcReturnSuper < 0 then leave CONVERTBLOCK.
        end.                    
    end. /* for each tDebtorEndUsersToUpdate */
end. /* CONVERTBLOCK */

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

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

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

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

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