TR_CAMPAIGNSUBPRIORITY_IU_ORGANIZATIONAMOUNT

Trigger Definition


				
					CREATE trigger [dbo].[TR_CAMPAIGNSUBPRIORITY_IU_ORGANIZATIONAMOUNT] on [dbo].[CAMPAIGNSUBPRIORITY] 
					after insert, update 
					not for replication
					as
					begin
						set nocount on;
	
						-- if we try to save an amount without explicitly setting an organization amount and an exchange rate, 
						-- copy the amount to the organization amount.
						if update(GOAL)
						begin
							declare @ORGANIZATIONCURRENCYID uniqueidentifier = dbo.UFN_CURRENCY_GETORGANIZATIONCURRENCY(); 

							update
								dbo.CAMPAIGNSUBPRIORITY
							set
								CAMPAIGNSUBPRIORITY.ORGANIZATIONAMOUNT = CAMPAIGNSUBPRIORITY.GOAL,
								CAMPAIGNSUBPRIORITY.CHANGEDBYID = CAMPAIGNSUBPRIORITY.CHANGEDBYID,
								CAMPAIGNSUBPRIORITY.DATECHANGED = CAMPAIGNSUBPRIORITY.DATECHANGED
							from
								inserted
								inner join dbo.CAMPAIGNSUBPRIORITY on inserted.ID = CAMPAIGNSUBPRIORITY.ID
								inner join dbo.CAMPAIGNPRIORITY on CAMPAIGNSUBPRIORITY.CAMPAIGNPRIORITYID = CAMPAIGNPRIORITY.ID
								inner join dbo.CAMPAIGN on CAMPAIGNPRIORITY.CAMPAIGNID = CAMPAIGN.ID
							where
								CAMPAIGNSUBPRIORITY.CURRENCYEXCHANGERATEID is null
								and (CAMPAIGN.BASECURRENCYID is null or CAMPAIGN.BASECURRENCYID = @ORGANIZATIONCURRENCYID)
								and (
									CAMPAIGNSUBPRIORITY.ORGANIZATIONAMOUNT <> CAMPAIGNSUBPRIORITY.GOAL
									or CAMPAIGNSUBPRIORITY.GOAL = 0
								);
						end
					end