Formula code working inconsistently

I inherited a db with this code in a button on a form. It sets a status code and sends and email then saves the document. It works for me every time. The users show me examples where the email is sent, but the status isn’t changed. I don’t see the hole, do you?

END:=@If(@PostedCommand([FileSave]); @PostedCommand([FileCloseWindow]); “”);

Return := @Prompt([YesNo]; “Cancel this Time Off Request?”; “Are you sure?”);

@If(Return = 1;

@Do(@Command( [EditDocument]; “1” );

@SetField(“Status”;“Cancelled By Associate”);

@MailSend( Supervisor ; Assoc ; “” ; “Cancelled” ; “” ; "Click here for details => " ; [IncludeDoclink] );

END);“”)

Subject: Formula code working inconsistently

Yes. You put the document in edit mode, change the field, and send the email, but the change is never saved. If the user exits and chooses not to save, it’s not saved.

Subject: RE: Formula code working inconsistently

Why do you think that? The END statement is a save and a close. (And it always works for me) …

Subject: RE: Formula code working inconsistently

To state it very carefully: END is not part of the documented @Formula syntax. It should be nothing, but an empty temporary variable …

Subject: Formula code working inconsistently

My guess is that the users it’s not working for do not have edit rights to the document. Also, I don’t like the way the formula is written. END actually gets evaluated at the beginning of the statement, not at the end of the formula where it appears at the end if the IF statement. I would rather see it written like this:

Return := @Prompt([YesNo]; “Cancel this Time Off Request?”; “Are you sure?”);

@If(Return = 1; “”; @Return(“”));

@Command( [EditDocument]; “1” );

@SetField(“Status”;“Cancelled By Associate”);

@MailSend( Supervisor ; Assoc ; “” ; “Cancelled” ; “” ; "Click here for details => " ; [IncludeDoclink] );

@If(@PostedCommand([FileSave]); @PostedCommand([FileCloseWindow]); @PostedCommand([FileSave]))

Subject: RE: Formula code working inconsistently

Better, but if the save fails (because of network error or validation failure) you have sent off the email already. Also, the return value of @PostedCommand is not useful – because the command hasn’t executed yet, so we can’t tell whether it failed except on computed equipped with a “send a message back in time” chip (not yet standard equipment on most business PCs – mostly found on gaming systems). Here’s an improved version:

Return := @Prompt([YesNo]; “Cancel this Time Off Request?”; “Are you sure?”);

@If(Return = 1; “”; @Return(“”));

@Command( [EditDocument]; “1” );

FIELD Status := Cancelled By Associate";

@If(@Command([FileSave]);

@Do(

  @MailSend( Supervisor ; Assoc ; "" ; "Cancelled" ; "" ; "Click here for details => " ; [IncludeDoclink] );

  @PostedCommand([FileCloseWindow])

);

“”

)

Also, Harkpabst is right, END in a formula does nothing. It’s just a temporary variable, same as any other made-up name such as NULL, HULAHOOP or QUANK.