USP_PROSPECTPLAN_MAKEHISTORICAL

Executes the "Prospect Plan: Make Historical" record operation.

Parameters

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

Definition

Copy


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

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

                        declare @ALLSTEPSCOMPLETED bit;
                        declare @ALLOPPORTUNITIESCOMPLETED bit;

                        if exists(select 1 from PROSPECTPLAN PP 
                                    where PP.ID = @ID
                                        and exists(select 1 from INTERACTION 
                                            where PROSPECTPLANID = PP.ID and not STATUSCODE in(2,3,4,5)))
                            set @ALLSTEPSCOMPLETED = 0;
                        else
                            set @ALLSTEPSCOMPLETED = 1;                

                        if exists(select 1 from PROSPECTPLAN PP 
                                    where PP.ID = @ID
                                        and exists(select 1 from OPPORTUNITY 
                                            where PROSPECTPLANID = PP.ID and not STATUSCODE in(3,4,5)))                        
                            set @ALLOPPORTUNITIESCOMPLETED = 0;
                        else
                            set @ALLOPPORTUNITIESCOMPLETED = 1;

                        if @ALLSTEPSCOMPLETED = 0 and @ALLOPPORTUNITIESCOMPLETED = 0
                        begin
                          raiserror('BBERR_PROSPECTPLAN_STEPS_ALLSTEPSANDOPPORTUNITIESCOMPLETED',13,1)
                          return
                        end    

                        if @ALLSTEPSCOMPLETED = 0
                        begin
                          raiserror('BBERR_PROSPECTPLAN_STEPS_ALLSTEPSCOMPLETED',13,1)
                          return
                        end    

                        if @ALLOPPORTUNITIESCOMPLETED = 0
                        begin
                          raiserror('BBERR_PROSPECTPLAN_STEPS_ALLOPPORTUNITIESCOMPLETED',13,1)
                          return
                        end

                        update dbo.PROSPECTPLAN set 
                            ISACTIVE=0,
                            CHANGEDBYID=@CHANGEAGENTID,
                            DATECHANGED=getdate()
                        where
                            ID=@ID
                            and ISACTIVE=1;

                        return 0;
                    end;