0

Code Simplification - Loop ?

Hello,

 

I am trying to simplify this code

let num := number(Deduction);
let rec := record('Taxes/Deductions',num);
let ths := this;
if rec.Calculation then
    ths.(Calculation := rec.Calculation)
end;
if rec.Percent then
    ths.(Percent := rec.Percent)
end;
if rec.'Annual Wage Base/Maximum Earnings' then
    ths.('Annual Wage Base/Maximum Earnings' := rec.'Annual Wage Base/Maximum Earnings')
end

Basically I have two very similar tables, the current working table, only num (Deduction) is a dynamic choice that selects the other table that has the identical fields. 

 

In Deduction, I have a trigger after update that will go through and copy the field details from 'Taxes/Deductions' table, only if values are present.

 

I currently have if then statements for each 'Taxes/Deductions'.Field checking for a value, if value is not null then it will copy the field details.

 

There has to be a way to loop through each field, and if not null, copy the data.

 

Can someone assist me in achieving this?

 

Thank you,

S. Moore

3 replies

null
    • Fred
    • 2 yrs ago
    • Reported - view

    Your thinking of using a for loop command is a good direction.

    Here is one possibility that you can put in a button:

    let t := this;
    let xChoice := number(Deduction);
    let xRec := record('Taxes/Deductions',xChoice);
    let n1check := if xRec.Calculation then 1 end;
    let n2check := if xRec.Percent then 2 end;
    let n3check := if xRec.'Annual Wage Base/Maximum Earnings' then 3 end;
    let array1 := [n1check, n2check, n3check];
    for loop1 in array1 do
        switch loop1 do
        case 1:
            Calculation := xRec.Calculation
        case 2:
            Percent := xRec.Percent
        case 3:
            'Annual Wage Base/Maximum Earnings' := xRec.'Annual Wage Base/Maximum Earnings'
        end
    end
    

    Lines 1 - 3 are similar to yours.

    Lines 4 - 6 check if the fields are true then assigns a value. otherwise the variable is empty.

    Line 7 creates an array of the check fields above. Only fields that are true will show up here.

    Lines 8 - 17 is the for loop command that takes the array from line 7 and uses the switch command to check each field that needs to be updated.

    I might have missed a translation into your DB names, so double check for my errors.

    • SMoore
    • 2 yrs ago
    • Reported - view

    Fred, 

     

    This is great. It is exactly what I was looking for, however, my goal was to simplify my code. I do think my if/then is a little simpler than the while loop. Only because each case (n1check) still needs an if/then. 

    However, this example does give me a much better understanding of loops within Ninox... Something I would love to be more familiar with. 

    • Cloud DevOps Fullstack Engineer
    • Martin_Mueller
    • 2 yrs ago
    • Reported - view

    S Moore I don't think using a loop makes sense here as those calculations are static. Loops are more for dynamic data like table rows or DMC.

Content aside

  • Status Answered
  • 2 yrs agoLast active
  • 3Replies
  • 77Views
  • 3 Following