Not getting past else
The code below is in a button. The problem I have is it wont go past the else. I have confirmed the code works for the first if statement and the second if statement after the else (I put the second by itself and tested it).
So I think what is happening is if the first if statement is false, it just ends and doesnt evaluate the next if statement.
let currRec := this.Id;
let per := this.PeriodsTG;
if select PayChecks where Pay = currRec and text(Pay.Period) = "YTG" then
for j in select PayChecks where Pay = currRec and text(Pay.Period) = "YTG" do
let a := (select PayChecks)[text(Pay.Period) = "Base" and LineItem = j.LineItem];
j.(Amount := number(a.Amount) * per)
end
else
if select PayChecks where Pay = currRec and text(Pay.Period) = "DividendsYTG" then
for j in select PayChecks where Pay = currRec and text(Pay.Period) = "DividendsYTG" do
let a := (select PayChecks)[text(Pay.Period) = "DividendsBase" and LineItem = j.LineItem];
j.(Amount := number(a.Amount) * per)
end
end
end
8 replies
-
ok, so I guess you dont need the else. Once I took that out it worked. If you know why I dont need else let me know. thanks
-
If there will only be the two conditions "YTG" or "DividendsYTG", then you really don't need the second if-then. You can just have the for-loop for "DividendsYTG" execute after the else. I don't see why the else won't work even with the second if-then, but you could simply have an alert() fire after else to test it.
-
Trying to understand the context...
Is the button in the "PayChecks" table, or in another table?
if in another table, is there a relation whith the "PayChecks" table?
-
@Sean - there are more than those two conditions that can occur. The code is just for when those conditions exist, otherwise do nothing.
-
@Alain
PayChecks is a child of Pay. The button is in Pay table.
-
OK, from the context I suppose it is 1:N from Pay to PayChecks. And so, among the fields of the Pay table, there should be a field named (by default) "PayChecks". Since you have a relation, the first "select" can be replaced by the dereferencing of the relation, which simplifies the script. It would start with something like:
if Period = "YTG" then
for j in PayChecks do
If I read the code correctly, the second select (after let a :=) returns an array containing the handles to the records in PayChecks that depend on another record in Pay where the Period field has the value "Base", and whose LineItem field matches the one in the current (j) record. Is there something that guarantees that there can only be one record that matches?
-
@Alain
Regarding your question "Is there something that guarantees that there can only be one record that matches?", yes it is year but I hadnt put that in the code just yet. So essentially, the table Pay is a set of records that are unique with fields year and period. There wont be two records with the same year and period. The child table Paychecks has the detail makeup of the pay for that year and period (eg Gross pay, pretax withholding, post tax withholdings, taxes, etc).
-
Thank you for taking the time to write down that explanation. The code below should do what you expect at this point. It works by taking advantage of the existence of a relation between the two tables. There is no need for an "if" to enclose the "for", since the "for" won't do anything if there are no related records.
let Zper := PeriodsTG;
switch Period do
case "YTG":
for j in PayChecks do
let a := first((select PayChecks)[text(Pay.Period) = "Base" and LineItem = j.LineItem]);
j.(Amount := a.Amount * Zper)
end
case "DividendsYTG":
for j in PayChecks do
let a := first((select PayChecks)[text(Pay.Period) = "DividendsBase" and LineItem = j.LineItem]);
j.(Amount := a.Amount * Zper)
end
end
Content aside
- 4 yrs agoLast active
- 8Replies
- 564Views