0

Global function to work with out all parameters

Is it possible to have a global function that works with out having all parameters defined? Right now I can't see a way, but I'm just diving into global functions.

I have a sports DB and there are many calculations that are similar so I'm finally trying out global functions.

I have a function that is pretty simple:

function teamAvP(tID : number,yNum : number) do
    avg((select Results)[TeamID = tID and Year_calc = yNum].Penalty4Average_Count)
end

I want to be able to use this function to find all results of a tID and sometimes use the yNum parameter. I have a few more parameters that I want to throw in, but this is a good start.

Thanks,

12 replies

null
    • Ninox developper
    • Jacques_TUR
    • 1 yr ago
    • Reported - view

    It is not possible to set default parameters or omit a parameter when calling a function. Ninox expects the right number of parameters with the right type for each.

    But you can call a function with one (or more) parameters with a null value:

    teamAvP( this.id, number(void) );

    So, in your global function, you check if yNum is void or not. You have several ways to do this. Here are two examples :

    - create a test with two different processes : 

    function teamAvP2(tID : number,yNum : number) do
        if (ynum = void) then
            avg((select Results)[TeamID = tID].Penalty4Average_Count)
        else
            avg((select Results)[TeamID = tID and Year_calc = yNum].Penalty4Average_Count)
        end;
    end
    

    - include your test in the where clause (yNum = void or Year_calc = yNum)  : 

    function teamAvP2(tID : number,yNum : number) do
        avg((select Results)[TeamID = tID and (yNum = void or Year_calc = yNum)].Penalty4Average_Count)
    end;
    
    • Fred
    • 1 yr ago
    • Reported - view

    Ok, how does one do a null in a text field?

    I have this global function:

    function teamAvP(tID : number,yNum : number,cName : text,rType : text) do
        avg((select Results)[TeamID = tID and (yNum = void or Year_calc = yNum) and (cName = void or 'City Name' = cName) and (rType = void or 'Round Type' = rType)].Penalty4Average_Count)
    end;
    

    I want to leave rType, the 4th parameter, empty. So I've tried:

    let xY := record(Seasons,number(Season));
    teamAvP(Team.TeamID, xY.Year, City.'City Name', "")
    

    All other parameters are based on field selections either from dynamic choice or reference field. the rType is the only one that is hand coded.

    I've tried:

    ""

    text(void)

    text(null)

    None of the above work. What are my other options?

      • Ninox developper
      • Jacques_TUR
      • 1 yr ago
      • Reported - view

      Fred I just saw that you have to put text(void) : 

      function teamAvP(tID : number,yNum : number,cName : text,rType : text) do
          avg((select Results)[TeamID = tID and
              (yNum = void or Year_calc = yNum) and
              (cName = text(void) or 'City Name' = cName) and
              (rType = text(void) or 'Round Type' = rType)].Penalty4Average_Count)
      end;
      
      
      • Fred
      • 1 yr ago
      • Reported - view

      Yes, sorry for letting the code run long.

      What else should I put if not text(void). It works for cName just not rType.

      • Ninox developper
      • Jacques_TUR
      • 1 yr ago
      • Reported - view

      Fred I'm not sure what's wrong? The function call with text(void) or the filter?
      Can you tell me more so I know where to look? Or send me an example?

      • Ninox developper
      • Jacques_TUR
      • 1 yr ago
      • Reported - view

      Fred You can also use the cascade call with different parameters:

      function GlobalJS(Gjs : text) do
          #{:text return Gjs}#
      end;
      function selectA(title : text) do
          select Customer where Title like title
      end;
      function selectAB(title : text,firstName : text) do
          selectA(title)['First Name' like firstName]
      end;
      function selectABC(title : text,firstName : text,lastName : text) do
          selectAB(title, firstName)['Last Name' like lastName]
      end

       

      And you call the one you need:

      selectAB("Mrs", "L" ).---{'First Name'} {'Last Name'}---
      

      result : 
      Lisa Wilson
      Evelyn Thomas
      Natalie Marcus

       

      selectABC("Mrs", "L", "o" ).---{'First Name'} {'Last Name'}---

      result : 

      Lisa Wilson
      Evelyn Thomas

      • Ninox developper
      • Jacques_TUR
      • 1 yr ago
      • Reported - view

      Fred Does it work when you call teamAvP with void in the cName parameter?

    • Fred
    • 1 yr ago
    • Reported - view
    Jacques TUR said:
    Can you tell me more so I know where to look?

     Ok, so I have this global function:

    function teamAvP(tID : number,yNum : number,cName : text,rType : text) do
        avg((select Results)[TeamID = tID and
                            (yNum = void or Year_calc = yNum) and
                            (cName = void or 'City Name' = cName) and
                            (rType = void or 'Round Type' = rType)].Penalty4Average_Count)
    end;
    

    When I try any of the following for the 4th parameter I get an empty field:

    let xY := record(Seasons,number(Season));
    teamAvP(Team.TeamID, xY.Year, City.'City Name', "")
    or
    teamAvP(Team.TeamID, xY.Year, City.'City Name', text(void))
    or
    teamAvP(Team.TeamID, xY.Year, City.'City Name', text(null))
    

    If I put in a value I get results:

    let xY := record(Seasons,number(Season));
    teamAvP(Team.TeamID, xY.Year, City.'City Name', "1")
    

    All other parameters are based on field selections either from dynamic choice or reference field. the 4th parameter (rType) is the only one that is hand coded.

      • Ninox developper
      • Jacques_TUR
      • 1 yr ago
      • Reported - view

      Fred Like I said here you must to specify the type of void like this : (rType = text(void) or 'Round Type' = rType). With out it, Ninox try to found record with 'Round Type' = void.

    • Fred
    • 1 yr ago
    • Reported - view
    Jacques TUR said:
    Does it work when you call teamAvP with void in the cName parameter?

     cName doesn't work if I hard code text(void), but since the data comes from a reference field it seems to work fine without the cName = text(void).

    I'm sorry, my eyes glazed over the rType = text(void) changes you made. That was the solution staring me in the face. Thanks for being patient with me.

      • Ninox developper
      • Jacques_TUR
      • 1 yr ago
      • Reported - view

      Fred You are so patient with others, I think it is the least I can do. Thank you for the help you bring to this forum 🙏.

      It work right now ?

      • Fred
      • 1 yr ago
      • Reported - view

      Jacques TUR yes it does! thanks again. 

Content aside

  • Status Answered
  • 1 yr agoLast active
  • 12Replies
  • 251Views
  • 2 Following