USP_DATAFORMTEMPLATE_EDIT_PROSPECTCONSTITUENCY

The save procedure used by the edit dataform template "Prospect Constituency 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.
@DATEFROM date IN Date from
@DATETO date IN Date to

Definition

Copy

                    CREATE procedure dbo.USP_DATAFORMTEMPLATE_EDIT_PROSPECTCONSTITUENCY
                    (
                        @ID uniqueidentifier,
                        @CHANGEAGENTID uniqueidentifier = null,
                        @DATEFROM date,
                        @DATETO date
                    ) as
                        set nocount on;

                        declare @CURRENTDATE datetime;
                        set @CURRENTDATE = getdate();

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

                        if (@DATEFROM > @CURRENTDATE or @DATETO is not null) and exists (
                            select
                                1
                            from
                                dbo.OPPORTUNITY O
                                inner join dbo.PROSPECTPLAN PP on PP.ID=O.PROSPECTPLANID
                                inner join dbo.PROSPECTDATERANGE PDR on PDR.CONSTITUENTID = PP.PROSPECTID
                            where
                                PDR.ID = @ID and
                                (PDR.DATEFROM <= @CURRENTDATE or PDR.DATEFROM is null) and
                                (PDR.DATETO >= @CURRENTDATE or PDR.DATETO is null) and
                                O.STATUSCODE = 2
                        )
                        raiserror('This constituent has a response-pending opportunity and therefore must remain a prospect.', 13, 1);

                        begin try
                            update
                                dbo.[PROSPECTDATERANGE]
                            set
                                [DATEFROM] = @DATEFROM,
                                [DATETO] = @DATETO,
                                [CHANGEDBYID] = @CHANGEAGENTID,
                                [DATECHANGED] = @CURRENTDATE
                            where
                                [ID] = @ID;
                        end try
                        begin catch
                            exec dbo.USP_RAISE_ERROR;
                            return 1;
                        end catch

                        return 0;