Copy Code Trigger Definition

              
CREATE trigger dbo.TR_BANKACCOUNT_GLACCOUNTIDPDACCOUNTSEGMENTID_VALIDREQUIRED on BANKACCOUNT for INSERT, UPDATE not for replication as
begin
  set nocount on;
  if dbo.UFN_INSTALLEDPRODUCTS_PRODUCTIS('0E85C527-E6E9-4C5F-A8E8-105FD0E18FE7') = 1
  BEGIN
        -- Check new GL business rule to turn off general ledger functionality...if 

        -- general ledger functionality is turned off allow a bank account to be

        -- saved without any cash account information.

        declare @PDACCOUNTSYSTEMID uniqueidentifier;
        select @PDACCOUNTSYSTEMID=I.PDACCOUNTSYSTEMID from inserted I;
        if dbo.UFN_PDACCOUNTSYSTEM_ALLOWGLDISTRIBUTIONS(@PDACCOUNTSYSTEMID) = 1 
          begin
                if exists(select I.ID from inserted I
                              where I.GLACCOUNTID is null and I.DEFAULTCASHACCOUNTTYPECODE = 0 and I.STATUSCODE = 1)
                BEGIN
                    RAISERROR ('CK_BANKACCOUNT_GLACCOUNTID', 16, 1)
                    ROLLBACK
                END
                if exists(select I.ID from inserted I
                                    where I.PDACCOUNTSEGMENTVALUEID is null and I.DEFAULTCASHACCOUNTTYPECODE = 1 and I.STATUSCODE = 1)
                BEGIN
                    RAISERROR ('CK_BANKACCOUNT_PDACCOUNTSEGMENTVALUEID', 16, 1)
                    ROLLBACK
                END
            end
        
        if exists(select I.ID from inserted I
            inner join deleted D on I.ID = D.ID
            inner join dbo.BANKACCOUNTTRANSACTION BAT on BAT.BANKACCOUNTID = I.ID
            left outer join dbo.BANKACCOUNTADJUSTMENT A on A.ID = BAT.ID
            left outer join dbo.BANKACCOUNTTRANSACTION BAT1 on BAT.ID = A.TRANSFERADJUSTMENTID
            where ((I.DEFAULTCASHACCOUNTTYPECODE = 0 and isnull(D.GLACCOUNTID, NEWID()) != isnull(I.GLACCOUNTID, NEWID())) 
                or (I.DEFAULTCASHACCOUNTTYPECODE = 1 and isnull(D.PDACCOUNTSEGMENTVALUEID, NEWID()) != isnull(I.PDACCOUNTSEGMENTVALUEID, NEWID())))
                and (BAT.POSTSTATUSCODE = 1 or isnull(BAT1.POSTSTATUSCODE, 0) = 1) and I.STATUSCODE = 1)
        begin
            raiserror('ERR_BANKACCOUNT_CANNOTCHANGECASHACCOUNT', 16, 1);
            rollback
        end
  END
 
 
end