SET TERM ^ ;

CREATE OR ALTER trigger ttbpdv_cfe_b02i for tbpdv_cfe
active before insert or update position 2
AS
BEGIN

  IF(COALESCE(NEW.REPRESENTANTE_ID, 0) = 0) THEN
  BEGIN
      NEW.REPRESENTANTE_ID = FN_PARAMETRO('SISTEMA.REPRESENTANTE_PADRAO');

      NEW.REPRESENTANTE_COMISSAO = COALESCE((SELECT T.COMISSAO FROM TBREPRESENTANTE T WHERE T.CODIGO = FN_PARAMETRO('SISTEMA.REPRESENTANTE_PADRAO')), 0);
  END ELSE
  BEGIN
      NEW.REPRESENTANTE_COMISSAO = COALESCE((SELECT T.COMISSAO FROM TBREPRESENTANTE T WHERE T.CODIGO = NEW.REPRESENTANTE_ID), 0);
  END
END
^

SET TERM ; ^

