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
END;