USP_DATAFORMTEMPLATE_EDIT_OPPORTUNITYAMOUNTBRACKETS

The save procedure used by the edit dataform template "Opportunity Amount Brackets Edit Form".

Parameters

Parameter Parameter Type Mode Description
@CHANGEAGENTID uniqueidentifier IN Input parameter indicating the ID of the change agent invoking the procedure.
@OPPORTUNITYAMOUNTBRACKETS xml IN

Definition

Copy


                CREATE procedure dbo.USP_DATAFORMTEMPLATE_EDIT_OPPORTUNITYAMOUNTBRACKETS (
                    @CHANGEAGENTID uniqueidentifier = null,            
                    @OPPORTUNITYAMOUNTBRACKETS xml
                ) as
                    set nocount on;

                    if @CHANGEAGENTID is null
                        exec USP_CHANGEAGENT_GETORCREATECHANGEAGENT @CHANGEAGENTID output;

                    declare @CURRENTDATE datetime
                    set @CURRENTDATE = getdate()

                    declare @TempTbl table (
                        ID uniqueidentifier,
                        NAME nvarchar(100),
                        LOWERLIMIT money
                    );

                    insert into @TempTbl
                        select 
                            ID,
                            NAME,
                            LOWERLIMIT
                        from 
                            dbo.UFN_OPPORTUNITYAMOUNTBRACKETS_FROMITEMLISTXML(@OPPORTUNITYAMOUNTBRACKETS);

                    update @TempTbl set ID = newid() where (ID is null) or (ID = '00000000-0000-0000-0000-000000000000');

                    if @@Error <> 0
                        return 1;

                    -- delete any items that no longer exist in the XML table

                    delete from dbo.OPPORTUNITYAMOUNTBRACKET where OPPORTUNITYAMOUNTBRACKET.ID in 
                        (select ID from dbo.OPPORTUNITYAMOUNTBRACKET
                        except select ID from @TempTbl);

                    if @@Error <> 0
                        return 2;

                    -- update the items that exist in the XML table and the db

                    update OPPORTUNITYAMOUNTBRACKET set 
                        OPPORTUNITYAMOUNTBRACKET.ID=temp.ID,
                        OPPORTUNITYAMOUNTBRACKET.NAME=temp.NAME,
                        OPPORTUNITYAMOUNTBRACKET.LOWERLIMIT=temp.LOWERLIMIT,
                        OPPORTUNITYAMOUNTBRACKET.CHANGEDBYID = @CHANGEAGENTID,
                        OPPORTUNITYAMOUNTBRACKET.DATECHANGED = @CURRENTDATE
                    from 
                        dbo.OPPORTUNITYAMOUNTBRACKET 
                        inner join @TempTbl as temp on OPPORTUNITYAMOUNTBRACKET.ID = temp.ID
                    where 
                        (OPPORTUNITYAMOUNTBRACKET.ID<>temp.ID) or 
                        (OPPORTUNITYAMOUNTBRACKET.ID is null and temp.ID is not null) or 
                        (OPPORTUNITYAMOUNTBRACKET.ID is not null and temp.ID is null) or 
                        (OPPORTUNITYAMOUNTBRACKET.NAME<>temp.NAME) or 
                        (OPPORTUNITYAMOUNTBRACKET.NAME is null and temp.NAME is not null) or 
                        (OPPORTUNITYAMOUNTBRACKET.NAME is not null and temp.NAME is null) or 
                        (OPPORTUNITYAMOUNTBRACKET.LOWERLIMIT<>temp.LOWERLIMIT) or 
                        (OPPORTUNITYAMOUNTBRACKET.LOWERLIMIT is null and temp.LOWERLIMIT is not null) or 
                        (OPPORTUNITYAMOUNTBRACKET.LOWERLIMIT is not null and temp.LOWERLIMIT is null);

                    if @@Error <> 0
                        return 3;    

                    -- insert new items

                    insert into OPPORTUNITYAMOUNTBRACKET (
                        ID,
                        NAME,
                        LOWERLIMIT,
                        ADDEDBYID, 
                        CHANGEDBYID, 
                        DATEADDED, 
                        DATECHANGED
                    ) select 
                        ID,
                        NAME,
                        LOWERLIMIT,
                        @CHANGEAGENTID
                        @CHANGEAGENTID
                        @CURRENTDATE
                        @CURRENTDATE
                    from 
                        @TempTbl as temp
                    where 
                        not exists (select ID from dbo.OPPORTUNITYAMOUNTBRACKET as DATA where DATA.ID = TEMP.ID);

                    if @@Error <> 0
                        return 4;

                    return 0;