Nested Loops in formula

I’m trying to use 2 @for loops in 1 field to send some emails.

How do i make the loop reset after it runs through the first time? Say i have 3 VP’s that each have 4 employees. I want the loop to go though the first VP and email each employee, Then i want it to move onto the second and email those employees, so on and so on.

The problem i have is the loop works but then when it moves to the second VP it is combining the first one’s employees also, then the third…

Any suggestions or ideas?

Thanks,

Kris

Subject: nested Loops in formula

Post your code – there’s no way to tell what you’re missing. The problem’s not difficult, so the chances are pretty good that you’ve just made a small mistake.

Subject: RE: nested Loops in formula

district :=“25”:“09”:“05”;

@For(z:= 1; z<= @Elements(district); z := z + 1;

dis :=@Subset(district[z];z);

n :=1;

stores :=@Unique(@If(@IsError(@DbLookup(“”:“”;“”:“gplookup.nsf”;“02LocationsByDistrictNumText”;dis;3)); @Failure(“0”);@DbLookup(“”:“”;“”:“gplookup.nsf”;“02LocationsByDistrictNumText”;dis;3)));

@For(n := 1; n <= @Elements(stores); n := n + 1;

flag :=@Text(stores[n])+“.01/23/08.END OF DAY”;

store :=@Text(stores[n]);

act :=@If(@IsError(@DbLookup(“”:“”;“”:“”;“02 ENTER SALES READS STORE DATE TIME”;flag;6)); @Failure(“0”);@DbLookup(“”:“”;“”:“”;“02 ENTER SALES READS STORE DATE TIME”;flag;6));

fin1 :=@Text(store)+" [a] “+@Text(act;“F(0),C”)+” [b] ";

html := html +fin1+@NewLine);

@MailSend(“billsmith@anwhere.com”;“”;“”;“Subject”;html;“”))

Subject: RE: nested Loops in formula

Hope this help’s.

district :=“25”:“09”:“05”;

@For(z:= 1; z<= @Elements(district); z := z + 1;

dis :=@Subset(district[z];z);

n :=1;

stores :=@Unique(@If(@IsError(@DbLookup(“”:“”;“”:“gplookup.nsf”;“02LocationsByDistrictNumText”;dis;3)); @Failure(“0”);@DbLookup(“”:“”;“”:“gplookup.nsf”;“02LocationsByDistrictNumText”;dis;3)));

@For(n := 1; n <= @Elements(stores); n := n + 1;

flag :=@Text(stores[n])+“.01/23/08.END OF DAY”;

store :=@Text(stores[n]);

act :=@If(@IsError(@DbLookup(“”:“”;“”:“”;“02 ENTER SALES READS STORE DATE TIME”;flag;6)); @Failure(“0”);@DbLookup(“”:“”;“”:“”;“02 ENTER SALES READS STORE DATE TIME”;flag;6));

fin1 :=@Text(store)+" [a] “+@Text(act;“F(0),C”)+” [b] ";

html := html +fin1+@NewLine);

@MailSend(“billsmith@anwhere.com”;“”;“”;“Subject”;html;“”))

Subject: RE: nested Loops in formula

Well, I can’t see how this formula is supposed to send different mails to different people, but I CAN see that you are never resetting the value of the “html” variable to “”, so the one person who is receiving mail will see a slightly longer message each time. That’s not the only problem, though. Never do this:

@If(@IsError(@DbLookup(“”:“”;“”:“gplookup.nsf”;“02LocationsByDistrictNumText”;dis;3)); @Failure(“0”);@DbLookup(“”:“”;“”:“gplookup.nsf”;“02LocationsByDistrictNumText”;dis;3))

You are doing the same lookup twice. Yes, it’s cached THIS TIME, but it’s a bad habit because you’re going to wind up doing it with a nocache lookup some day. Do this instead:

variable := @DbLookup(“”:“”;“”:“gplookup.nsf”;“02LocationsByDistrictNumText”;dis;3);

@If(@IsError(variable); @Set(“variable”; “”); “”);

Note that there is no @Failure in there. @Failure has meaning ONLY in field validation formulas (it does an @Prompt([OK]) and stops the UI save request). Don’t use @Failure anywhere else.

And variable[index] means the same as @Subset(@Subset(variable; index);-1). There’s no reason to use both and @Subset in the same statement.

Subject: RE: nested Loops in formula

cool, thanks for the response. I always appreciate learning something new or a better way to program.