Field Transform Groovy Function with Switch Statement cannot handle variable

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • Field Transform Groovy Function with Switch Statement cannot handle variable

      Hello again,

      maybe someone of you ran into a similar problem and might have a hint for me.
      I'm struggling at the moment with a field transform.
      The groovy function there is supposed do the following
      Check if KPI (_input1) is Price -> then return value (_input2)
      check if KPI is amount -> then check if value = PlanSource then return PlanTarget [PlanSource and PlanTarget are both variables), else check value



      $PlanSource = 2416.0
      $PlanTarget = 600.0

      This should result in:

      I tried it with an if-statement as well as switch. Unfortunately both don't work. Somehow the function cannot assert the input with the variable Value. If I type in a value instead of the variable PlanSource, the snippet works.

      Both work in groovy webconsole:

      Java Source Code

      1. PlanSource = ${HPPlanSource}
      2. PlanTarget = ${HPPlanTarget}
      3. //KPI = Price -> Value
      4. if (_input1 == "Price") {
      5. return _input2;
      6. }
      7. // KPI = Amount
      8. // Amount = Plan of source -> Plan of Target Project
      9. else if (_input2 == PlanSource) {
      10. return PlanSource;
      11. }
      12. // Menge <> Plan -> Amount of SourceTable
      13. else {
      14. return _input2;
      15. }
      Display All

      Java Source Code

      1. HPPlanSource = ${HPPlanSource}
      2. HPPlanTarget = ${HPPlanTarget}
      3. switch(_input1) {
      4. // if KPI = Preis return input2 (Preis value)
      5. // if KPI = Menge
      6. // if input2(value) = HPPlanSource return HPPlanTarget
      7. // else input2 (Menge value)
      8. case "Preis":
      9. return _input2;
      10. break;
      11. case "Menge":
      12. switch(_input2){
      13. case HPPlanSource:
      14. return HPPlanTarget;
      15. break;
      16. default:
      17. return _input2;
      18. }
      19. default:
      20. return _input2;
      21. }
      Display All
      Does anybody have an idea?

      Thanks in advance.

    • I found the solution.
      In the end the issue was with the type. (to clarify: the values did not get replaced in the beginning, it did not run into an error)
      When I specified in the groovy job for _input2 as float it worked.

      I had to split up the extract to keep it simple in the ETL, since KPI (_input1) had also a comment element with string values (_input2).
      But after splitting that up and putting a table union in the end, it worked like a charm.