USP_DATAFORM_SAVE_SETALL_MAILPREFERENCE

The save procedure used by the add dataform template "Mail Preference Set All Add Form".

Parameters

Parameter Parameter Type Mode Description
@ID uniqueidentifier INOUT The output parameter indicating the ID of the record added.
@CONSTITUENTID uniqueidentifier IN Input parameter indicating the context ID for the record being added.
@CHANGEAGENTID uniqueidentifier IN Input parameter indicating the ID of the change agent invoking the procedure.
@SENDMAIL bit IN Send
@DELIVERYMETHODCODE tinyint IN Send by
@CONTACTTYPES xml IN Send to contact(s)
@ADDRESSID uniqueidentifier IN Selected address
@EMAILADDRESSID uniqueidentifier IN Selected address
@USESEASONALADDRESS bit IN Send to seasonal address when valid
@COMMENTS nvarchar(500) IN Comments
@GROUPCONTACTS xml IN
@CURRENTAPPUSERID uniqueidentifier IN Input parameter indicating the ID of the current user.
@SITEID uniqueidentifier IN Site
@DONOTSENDOTHERCHANNEL bit IN

Definition

Copy


                    CREATE procedure dbo.USP_DATAFORM_SAVE_SETALL_MAILPREFERENCE
                    (
                        @ID uniqueidentifier = null output,
                        @CONSTITUENTID uniqueidentifier,
                        @CHANGEAGENTID uniqueidentifier = null,
                        @SENDMAIL bit = null,
                        @DELIVERYMETHODCODE tinyint = null,
                        @CONTACTTYPES xml = null,
                        @ADDRESSID uniqueidentifier = null,
                        @EMAILADDRESSID uniqueidentifier = null,
                        @USESEASONALADDRESS bit = null,
                        @COMMENTS nvarchar(500) = null,
                        @GROUPCONTACTS xml = null,
                        @CURRENTAPPUSERID uniqueidentifier = null,
                        @SITEID uniqueidentifier = null,
                        @DONOTSENDOTHERCHANNEL bit = null
                    )
                    as
                        set nocount on;

                        declare @CURRENTDATE datetime;

                        if @ID is null
                            set @ID = newid();

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

                        set @CURRENTDATE = getdate();    

                        declare @USEPRIMARYADDRESS bit
                        if @ADDRESSID='00000000-0000-0000-0000-000000000001'
                        begin
                            set @USEPRIMARYADDRESS=1
                            set @ADDRESSID=null
                        end
                        else
                        begin
                            set @USEPRIMARYADDRESS=0
                        end

                        declare @USEPRIMARYEMAIL bit
                        if @EMAILADDRESSID='00000000-0000-0000-0000-000000000001'
                        begin
                            set @USEPRIMARYEMAIL=1
                            set @EMAILADDRESSID=null
                        end
                        else
                        begin
                            set @USEPRIMARYEMAIL=0
                        end

                        if @SITEID is null and dbo.UFN_SITEREQUIREDFORUSER(@CURRENTAPPUSERID) = 1
                            raiserror('ERR_SITEID_REQUIREDFORUSER', 13, 1);

                        begin try 

                            /* cache current context information */
                            declare @contextCache varbinary(128);
                            set @contextCache = CONTEXT_INFO();

                            /* set CONTEXT_INFO to @CHANGEAGENTID */
                            if not @CHANGEAGENTID is null
                                set CONTEXT_INFO @CHANGEAGENTID

                            -- Delete old preferences. If we do not have site access to the site-specific preferences, do not delete them.

                            -- This is valid behavior. You can have the same preference mail type (appeals or events) with different sites.

                            delete dbo.MAILPREFERENCE
                            where 
                                CONSTITUENTID=@CONSTITUENTID
                                and (
                                    (MAILTYPECODE <> 1 and MAILTYPECODE <> 2)
                                    or dbo.UFN_SITEALLOWEDFORUSERONFEATURE(@CURRENTAPPUSERID, SITEID, 'ea086b06-5044-47b8-813d-16ca5496267d', 1) = 1
                                )

                            /* reset CONTEXT_INFO to previous value */
                            if not @contextCache is null
                                set CONTEXT_INFO @contextCache

                            declare @i int
                            set @i = 0

                            declare @NEWID uniqueidentifier = @ID;

                            while @i < 9
                            begin

                                insert into dbo.MAILPREFERENCE
                                (
                                    ID,
                                    CONSTITUENTID,
                                    MAILTYPECODE,
                                    SENDMAIL,
                                    DELIVERYMETHODCODE,
                                    ADDRESSID,
                                    EMAILADDRESSID,
                                    USESEASONALADDRESS,
                                    USEPRIMARYADDRESS,
                                    USEPRIMARYEMAIL,
                                    SITEID,
                                    COMMENTS,
                                    DONOTSENDOTHERCHANNEL,
                                    ADDEDBYID,
                                    CHANGEDBYID,
                                    DATEADDED,
                                    DATECHANGED
                                )
                                values
                                (
                                    @NEWID,
                                    @CONSTITUENTID,
                                    @i,
                                    @SENDMAIL,
                                    @DELIVERYMETHODCODE,
                                    @ADDRESSID,
                                    @EMAILADDRESSID,
                                    @USESEASONALADDRESS,
                                    @USEPRIMARYADDRESS,
                                    @USEPRIMARYEMAIL,
                                    case when @i = 1 or @i = 2 then @SITEID else null end,
                                    @COMMENTS,
                                    @DONOTSENDOTHERCHANNEL,
                                    @CHANGEAGENTID,
                                    @CHANGEAGENTID,
                                    @CURRENTDATE,
                                    @CURRENTDATE
                                )

                                exec dbo.USP_MAILPREFERENCE_GETCONTACTTYPES_ADDFROMXML @NEWID, @CONTACTTYPES;
                                exec dbo.USP_MAILPREFERENCE_GETGROUPCONTACTS_ADDFROMXML @NEWID, @GROUPCONTACTS;

                                set @i = @i + 1

                                set @NEWID = newid();
                            end

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

                        return 0;