Looping through list of Fields: can read from from field, or write to field, but not both

I’ve created a list of fields and I want to loop through the list to check the value in each field and, if the value is not correct, assign it a new value. This is for a form that is read in the Notes client (not the web). It gets executed during a button click - I am using the formula language.

I can get the code to either a) read the value stored in each field correctly, or b) write the value out to the field correctly, but not both.

If I build the list like so: listOfFIelds := statusField1 : statusField2 : statusField3;

… then I CAN read the value stored in the field: istOfFIelds[1] != “Approved”) will get the value stored in statusField1 and compare it to “Approved”

… but in this case I can NOT write the value out; @SetField(listOfFIelds[1] ; “Approved”) does not work - it seems to look up the value stored in statusField1 (say “Pending”) then tries (here is the problem) to find a field named “Pending” and assign the value “Approved” to that field.

If I build the list like so: listOfFIelds := “statusField1” : “statusField2” : “statusField3”;

… then I can NOT read the value stored in the field: istOfFIelds[1] != “Approved”) does what you expect - it compares the value “statusField1” to “Approved” and sees that they don’t match

… but in this case I CAN write the value out; @SetField(listOfFIelds[1] ; “Approved”) will properly assign the value “Approved” to the field named statusField1

Below is an example of how I am trying to this - it is quite a bit simpler than what I am really trying to achieve (which involves 3 separate array/lists.

listOfFIelds := “statusField1” : “statusField2” : “statusField3”;

REM { listOfFIelds := statusField1 : statusField2 : statusField3;};

n := 1;

@While(n <= @Elements(listOfFIelds);

@If(listOfFIelds[n] != "Approved";@SetField(listOfFIelds[n] ; "Approved");"");

n := n + 1

) ;

I could break down and abandon the idea of looping though the list/array of fields and do something like below, but that would be much messier.

@If(statusField1 != “Approved”;@SetField(statusField1 ; “Approved”);“”);

@If(statusField2 != “Approved”;@SetField(statusField2 ; “Approved”);“”);

@If(statusField3 != “Approved”;@SetField(statusField3 ; “Approved”);“”);

How can I use a list/array of fields and successfully read from and write to fields in the list?

Subject: typo?

My guess is that there is a typo somewhere in your “full version” of your code. This pared down version worked for me from a button on the document, and from an action in the view.

listOfFIelds := “Subject” : “Description”;

REM { listOfFIelds := statusField1 : statusField2 : statusField3;};

n := 1;

@While(n <= @Elements(listOfFIelds);

@If(listOfFIelds[n] != “Approved”;@SetField(listOfFIelds[n] ; “Approved”);“”);

n := n + 1

) ;

Subject: Here’s a better testcase - it shows the error more clearly

Hi Maria,

Thanks for taking the time. Even the simple example doesn’t work for me - it fails to detect the case where the value is already “Approved”. The example wasn’t very well constructed since it wouldn’t demonmstrate the problem well.

Below is a better example that shows the problem on my system. After you click the button you would expect the value of the field ‘Subject’ to be set to “Already Approved”, but it is not - it remains “Approved”. Does this new example show the problem for you?

@SetField(Subject ; “Approved”);

@SetField(Description ; “Bob”);

listOfFIelds := “Subject” : “Description”;

n := 1;

@While(n <= @Elements(listOfFIelds);

@If(listOfFIelds[n] = "Approved";@SetField(listOfFIelds[n] ; "Already Approved");"");

@If(listOfFIelds[n] != "Approved";@SetField(listOfFIelds[n] ; "Approved");"");

n := n + 1

) ;

Thanks again to anyone who can help out.

Subject: The formula is wrong

The fieldname in your @Setfield statements at the beginning of your formula should be in quotes… @Setfield(“subject”;“Approved”) or you could just say… Field Subject:=“Approved”;

Your listofFields variable just contains the strings “Subject” and “Description”. If you want the values of those fields (which is what you want to compare in the loop) you should not use quotes around them… listOfFIelds := Subject : Description;

This approach will work but you need to pay attention to the syntax.