USP_DATAFORMTEMPLATE_EDIT_ORGANIZATIONHIERARCHYPOSITIONEDIT_2

The save procedure used by the edit dataform template "Organization Hierarchy Position Edit Form".

Parameters

Parameter Parameter Type Mode Description
@ID uniqueidentifier IN The input ID parameter indicating the ID of the record being edited.
@CHANGEAGENTID uniqueidentifier IN Input parameter indicating the ID of the change agent invoking the procedure.
@NAME nvarchar(50) IN Position title
@SITEID uniqueidentifier IN Site
@BUSINESSUNITCODEID uniqueidentifier IN Business unit

Definition

Copy


                    CREATE procedure dbo.USP_DATAFORMTEMPLATE_EDIT_ORGANIZATIONHIERARCHYPOSITIONEDIT_2
                    (
                        @ID uniqueidentifier,
                        @CHANGEAGENTID uniqueidentifier = null,
                        @NAME nvarchar(50),
                        @SITEID uniqueidentifier,
                        @BUSINESSUNITCODEID uniqueidentifier
                    )
                    as
                    begin
                        set nocount on;

                        declare @CURRENTDATE datetime;
                        declare @OLDSITEID uniqueidentifier;

                        begin try

                            if @CHANGEAGENTID is null  
                                exec dbo.USP_CHANGEAGENT_GETORCREATECHANGEAGENT @CHANGEAGENTID output;

                            set @CURRENTDATE = GetDate();

                            select @OLDSITEID = SITEID 
                            from dbo.ORGANIZATIONPOSITION
                            where ID = @ID;

                            update dbo.ORGANIZATIONPOSITION
                            set NAME = @NAME,
                                SITEID = @SITEID,
                                BUSINESSUNITCODEID = @BUSINESSUNITCODEID,
                                CHANGEDBYID = @CHANGEAGENTID,
                                DATECHANGED = @CURRENTDATE
                            where ID = @ID;

                            if @SITEID <> @OLDSITEID or 
                                @SITEID is null and @OLDSITEID is not null or
                                @SITEID is not null and @OLDSITEID is null
                            begin
                                update dbo.ORGANIZATIONPOSITION
                                set SITEID = @SITEID,
                                    CHANGEDBYID = @CHANGEAGENTID,
                                    DATECHANGED = @CURRENTDATE
                                where ID <> @ID and dbo.UFN_ORGANIZATIONHIERARCHY_POSITIONISPARENT(ID, @ID) = 1
                                    and (SITEID = @OLDSITEID or
                                         SITEID is null and @OLDSITEID is null);
                            end

                        end try

                        begin catch
                            exec dbo.USP_RAISE_ERROR;
                            return 1;
                        end catch

                        return 0;
                    end