USP_ORGANIZATIONPARENTHISTORY_DELETE

Executes the "Organization Parent History Delete Record Operation" record operation.

Parameters

Parameter Parameter Type Mode Description
@ID uniqueidentifier IN Input parameter indicating the ID of the record being deleted.
@CHANGEAGENTID uniqueidentifier IN Input parameter indicating the ID of the change agent invoking the delete.

Definition

Copy


CREATE procedure dbo.USP_ORGANIZATIONPARENTHISTORY_DELETE
(
    @ID uniqueidentifier,
    @CHANGEAGENTID uniqueidentifier
)
as begin
    set nocount on;

    exec dbo.USP_CHANGEAGENT_GETORCREATECHANGEAGENT @CHANGEAGENTID output;

    -- Clear the parent organization link if this is the record for the current parent

    declare @PARENTCORPID uniqueidentifier, @CURRENTPARENTCORPID uniqueidentifier, @CHILDCORPID uniqueidentifier;
    select
        @CHILDCORPID = ORGANIZATIONPARENTHISTORY.CHILDCORPID,
        @PARENTCORPID = ORGANIZATIONPARENTHISTORY.PARENTCORPID,
        @CURRENTPARENTCORPID = ORGANIZATIONDATA.PARENTCORPID
    from dbo.ORGANIZATIONPARENTHISTORY
    inner join dbo.ORGANIZATIONDATA on ORGANIZATIONPARENTHISTORY.CHILDCORPID = ORGANIZATIONDATA.ID
    where ORGANIZATIONPARENTHISTORY.ID = @ID;

    if @PARENTCORPID = @CURRENTPARENTCORPID
    begin
        declare @CURRENTDATE datetime;
        set @CURRENTDATE = getdate();

        update dbo.ORGANIZATIONDATA set
            PARENTCORPID = null,
            DATECHANGED = @CURRENTDATE,
            CHANGEDBYID = @CHANGEAGENTID
        where ID = @CHILDCORPID;

        --Using this function to set end dates on all invalid intermediate parent relationships based on the Parent Corporation change

        exec dbo.USP_RELATIONSHIPS_CREATEPARENTORGRELATIONSHIP @CHILDCORPID, null, @CURRENTDATE, @CHANGEAGENTID, @CURRENTDATE, 0, @PARENTCORPID;
    end

    -- use the system generated delete routine to allow proper recording of the deleting agent

    exec USP_ORGANIZATIONPARENTHISTORY_DELETEBYID_WITHCHANGEAGENTID @ID, @CHANGEAGENTID;
    return 0;

end