Oracle APEX item validation (Function returning boolean) ORA-01403

10,907

Is it possible for either of the two fields to be NULL or to have a value that does not appear in the PERSONEN table? I'm guessing that P68_WETV1 at least is optional.

If you call GetLeeftijd and pass in a NULL value, this query will return no rows

SELECT months_between(SYSDATE, CAST("PERSONEN"."GEBOORTEDATUM" AS DATE)) /12 
  INTO v_leeftijd
  FROM dual, "PERSONEN"
  WHERE "PERSONEN"."BSN" = bsn_nummer;

A SELECT INTO statement that returns anything other than 1 row is an exception (either no_data_found or too_many_rows). You probably want to either catch the exception in your function or check whether the field has data before calling the function in your validation. So, for example

create or replace Function GetLeeftijd(bsn_nummer number)
RETURN NUMBER
AS
v_leeftijd NUMBER := 18;

BEGIN
  BEGIN
    SELECT months_between(SYSDATE, CAST(PERSONEN.GEBOORTEDATUM AS DATE)) /12 
      INTO v_leeftijd
      FROM PERSONEN
     WHERE PERSONEN.BSN = bsn_nummer;
  EXCEPTION
    WHEN no_data_found
    THEN
      v_leeftijd := 0;
  END;

  RETURN v_leeftijd;  
END;

Note that I removed the Cartesian join to the dual table from your query-- it's not adding any value.

Share:
10,907
rkhu
Author by

rkhu

Updated on June 04, 2022

Comments

  • rkhu
    rkhu almost 2 years

    I am trying to validate a field in APEX.

    What it is supposed to do:
    Check if applicant is under aged (age < 18), if true, a legal representative is required.
    This will be checked with the persons BSN.

    Fields:
    P68_REKENINGHOUDER (Contains BSN of applicant).
    P68_WETV1 (Contains BSN of legal representative).

    Function:
    GetLeeftijd: (Gets the persons age, tested and works)

    create or replace Function GetLeeftijd(bsn_nummer number)
    RETURN NUMBER
    AS
    v_leeftijd NUMBER := 18;
    
    BEGIN
      SELECT months_between(SYSDATE, CAST("PERSONEN"."GEBOORTEDATUM" AS DATE)) /12 INTO v_leeftijd
      FROM dual, "PERSONEN"
      WHERE "PERSONEN"."BSN" = bsn_nummer;
    
    RETURN v_leeftijd;
    
    END;
    

    Item validation (Type: Function Returning Boolean ON Create)

    declare
    
    v_rekh number;
    v_wetv number;
    
    begin
    v_rekh := GetLeeftijd(:P68_REKENINGHOUDER);
    v_wetv := GetLeeftijd(:P68_WETV1);
    
      if v_rekh < 18 then
        if :P68_WETV1 is NULL then
          return false;
        else
          if v_wetv < 18 then
            return false;
          else
            return true;
          end if;
        end if;
      else
        return true;
      end if;
    end;
    

    What actually happens:
    Getting a no data found error.

    ORA-01403: Geen gegevens gevonden.
    Error   ERR-1021 Unable to run "function body returning boolean" validation. 
    

    Other info:
    Running Application Express 4.0.0.00.46
    The function above is not polished yet but it works for now.