Dynamically creating fields on uidoc

The database I’m working on is to track assignment of resources (business cards, laptop, etc.) - the purpose being automated emails to resource admins during the hiring/termination process. The main document is Employee. The admin interface is a dialog box which allows them to select which resources they are administering and what the new status is.

My code for doing this is:

@For(x := 1; x <= @Elements(Options1); x = x + 1; {

    @SetField("status"+@ReplaceSubstring(Options1[x]; " "; ""); Values1)

});

Unfortunately this is returning the error: “The formula has exceeded the maximum allowable memory usage.” I don’t know why as that formula seems pretty simple. I assume it must be due to built-in recursion in the @ReplaceSubstring.

So then I tried an agent which would just update the back-end document and refresh the uidoc. Except I can’t get the uidoc to refresh after the document is saved. I have tried calling uidoc.Refresh and/or uidoc.Reload at the end of my code. I have tried calling ws.RefreshParentNote. I have tried putting @Command([ReloadWindow]) after the call to my dialog box. When I close my document and reopen it, the value has changed, but I’m pretty sure if I performed a save on the uidoc, or if I re-opened my admin dialog box, the edits would be lost.

Can anyone help me get this working in either Formula or an LS agent?