project BLF > class Database Component > method apiDeleteInstance

Description

Generic procedure for deleting database objects. This procedure contains the entire flow of loading the object data, validating if it can be deleted, deleting and deleting all related objects (= cascading deletes).


Parameters


icKeyinputcharacterDatabase instance key.
possible values:
'Rowid=...'
'Primary=...'
'Identity=...'
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BCInvoiceAPMatching.CreateCInvoiceAPMatchingAllInOneTran
method BCInvoiceAPMatching.CreateCInvoiceAPMatchingOneInOneTran
method BConsolid.AdditionalUpdatesDelete
method BSharedSetMerge.MergeDeleteElement
method BSharedSetMerge.MergeFinish


program code (program3/database.p)

if icKey begins "Rowid=":U
then do:
    assign vcKey = substring(icKey,7,-1,"CHARACTER":U)
           vlOk  = (vcKey <> "" and vcKey <> ? and vcKey <> "?":U).
    
    if vlOk
    then do:
        <M-1 run DataLoad
          (input  vcKey (icRowids), 
           input  '' (icPkeys), 
           input  '' (icObjectIds), 
           input  '' (icFreeform), 
           input  no (ilKeepPrevious), 
           output viFcReturnSuper (oiReturnStatus)) in database>
        if viFcReturnSuper <> 0
        then oiReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0
        then return.
    end.
end.
else if icKey begins "Primary=":U
then do:
    assign vcKey = substring(icKey,9,-1,"CHARACTER":U)
           vlOk  = (vcKey <> "" and vcKey <> ? and vcKey <> "?":U).
    
    if vlOk
    then do:
        <M-2 run DataLoad
          (input  '' (icRowids), 
           input  vcKey (icPkeys), 
           input  '' (icObjectIds), 
           input  '' (icFreeform), 
           input  no (ilKeepPrevious), 
           output viFcReturnSuper (oiReturnStatus)) in database>
        if viFcReturnSuper <> 0
        then oiReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0
        then return.
    end.
end.
else if icKey begins "Identity=":U
then do:
    assign viId = integer (substring(icKey,10,-1,"CHARACTER":U)) no-error.
    assign vlOk = (viId <> 0 and viId <> ?).
    
    if vlOk
    then do:
        <M-3 run DataLoad
          (input  '' (icRowids), 
           input  '' (icPkeys), 
           input  string(viId) (icObjectIds), 
           input  '' (icFreeform), 
           input  no (ilKeepPrevious), 
           output viFcReturnSuper (oiReturnStatus)) in database>
        if viFcReturnSuper <> 0
        then oiReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0
        then return.
    end.
end.

if vlOk
then do:
    /* Set status to 'D' (+ validate) */
    <M-4 run DataDelete (output viFcReturnSuper (oiReturnStatus)) in database>
    if viFcReturnSuper <> 0
    then oiReturnStatus = viFcReturnSuper.
    if viFcReturnSuper < 0
    then return.
    
    /* Commit */
    <M-5 run DataSave (output viFcReturnSuper (oiReturnStatus)) in database>
    if viFcReturnSuper <> 0
    then oiReturnStatus = viFcReturnSuper.
    if viFcReturnSuper < 0
    then return.
end.
else do:
    <M-6 run SetMessage
          (input  'Invalid input.':U (icMessage), 
           input  '' (icArguments), 
           input  '' (icFieldName), 
           input  '' (icFieldValue), 
           input  'S':U (icType), 
           input  2 (iiSeverity), 
           input  '' (icRowid), 
           input  'BLF-301':U (icFcMsgNumber), 
           input  '' (icFcExplanation), 
           input  '' (icFcIdentification), 
           input  '' (icFcContext), 
           output viFcReturnSuper (oiReturnStatus)) in database>
    assign oiReturnStatus = -1.
    return.
end.


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 = "database".
create ttContext.
assign ttContext.propertyName = "methodName"
       ttContext.propertyValue = "apiDeleteInstance".
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/database.apideleteinstance.i.xsd", ?).
vhParameter = vhInputDS:get-buffer-handle("tParameterI").
vhParameter:buffer-create().
assign vhParameter::icKey = <parameter value>.

/* 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.