Javascript Help

I need my web user to provide confirmation before submitting and I will set a field on the form with their name and compute the date. I have the following code in place, but the confirm() is not being presented to my users. I’ve looked at the code, but can’t see the error – I need another set of eyes.

Also, how do I set today’s date in a field using Javascript?

Thanks in advance…

Button on Form:

FIELD JobWorkflowState := “Pending Approval-1”;

@Command([FileSave]);

@Command([CloseWindow])

OnSubmit:

if (document.forms[0].JobWorkflowState.value == ‘Pending Approval-1’){

return confirm();

return validate();

}else{

return validate();

}

JSHeader:

function confirm() {

var answer = confirm (“Do you authorize the submission of this guarantee request?”)

if (answer){

document.forms[0].AppSigner.value = document.forms[0].createdBy.value;

return true;

}

else{

return false;

}

}

Subject: Javascript Help

There are three problems with the code.

The first is that you created a function named confirm, which is a reserved word. You need to change the name of the function.

The second problem is that you are “returning” values…which stops the script. Get rid of the “return”s in the onSubmit and replace them with If statements.

The last problem is the most difficult to fix.

This problem is that you are testing an HTML form element, while your updated field is still on the server.

In order to test the value, the document must be refresh. You must remember that you are working with two separate entities at the same time…the Notes document and the HTML form. If you update the Notes document, you must then refresh the HTML form if you expect to access those updates using JavaScript. That is impossible with your current process because the refresh will not happen until all the code in the button completes, and that won’t happen until the document completes the full save and close process. By then it’s too late.

The solution is to place the filesave and close commands in a separate button, which is activated from the onload event.

Take the onSubmit code and put it in onLoad

Create a new button at the bottom of the form that performs a filesave and close.

Edit the html properties of this button:

Name = btn_SaveClose

Style = display=none

Hide button from Notes users. The hiding of the button on the web is done with the display=none style.

Add the line:

document.forms[0].btn_SaveClose.click()

to the onload code in an appropriate place, and that should do it.

Now, having said that I would suggest that you create a more robust method of controlling what goes on in the onLoad. For example, use a separate “control” field to tell the onload when to do things.

So first, I would change your initial button to read:

FIELD JobWorkflowState := “Pending Approval-1”;

FIELD fld_NextStepFlag := “ProcessWorkflow”

“”

That’s all you need in the button. That act of clicking a button will cause the document to refresh and the onLoad will run.

Next, I would create the save and close button as described above.

Finally, I would have in the onLoad event for the form:

var NextStep = document.forms[0].fld_NextStepFlag.value

if (NextStep != “” ) {

document.forms[0].fld_NextStepFlag.value = ""  //reset fld_NextStepFlag 

switch (NextStep) {

	case " ProcessWorkflow ":	

		if (document.forms[0].JobWorkflowState.value == 'Pending Approval-1'){

			if ( funcconfirm() ) {

				if (validate() ) {

					document.forms[0].btn_SaveClose.click()

				}

			}else{

			validate()

			}

		}

		break



	case "SomeOtherProcess” :

		break

}

}

This process will allow you to control when the onLoad performs checks via the NextStepFlag field. You can load that switch statement up with all sorts of goodies! In fact, yoiu can call buttons that execute script agents, then process the results, and call more agents without needing any user interaction. Or, use this process to breakup long LotusScript processes that call for user interaction in the middle. I find it quite useful.

Oh, and you don’t need any semi-colons at the end of JavaScript statements. The semi-colon is a statement separator, used to put two separate statements on a single line. But you DO need a colon at the end of case statements.

Subject: RE: Javascript Help

You wrote: “Oh, and you don’t need any semi-colons at the end of JavaScript statements. The semi-colon is a statement separator, used to put two separate statements on a single line.”

JavaScript (or, rather, ECMA 262) ignores white space except with C+±style comments (//); there is no guarantee that the end of a text line marks the end of a statement. In this case, you have one statement per block ({}), and the closing brace is a statement terminator (just like the semicolon). Getting into and maintaining the habit of including semicolons as statement terminators is a positive reccomendation, not something to be discouraged.

And :“The second problem is that you are “returning” values…which stops the script. Get rid of the “return”s in the onSubmit and replace them with If statements.”

Of the three returns in the onsubmit code, only one (“return confirm()”) is misplaced and should be replaced with:

if(doConfirm()) {

return validate();

}

The other two are absolutely required. The onsubmit event will not prevent a form submission unless it is returned a value of false, and no value can be returned to the event listener unless the return keyword is used in the code executed by the event.

You’re right about the function name, though – I missed that. (Note that it has been edited to doConfirm() in the snippet above.)

Subject: RE: Javascript Help

“The other two [returns] are absolutely required.”

That’s right! I was already thinking onLoad when I wrote that! :slight_smile:

“there is no guarantee that the end of a text line marks the end of a statement.”

Oh yes there is. End-of-line = end-of-statement

I’ll check again, but I’m nearly 100% sure that the end of a line closes the statement on that line. The only two exceptions are string concatenation, which is a documented case, and the Block construct. But even within a block of statements, every statement ends at the end of the line. The semicolon only acts to separate multiple statements on a single line.

Since I’m used to LotusScript (where you never use semicolons) I never use semicolons in my JavaScript. So far, I have yet to have a problem or any confusion caused by a missing semicolon. They’re simply unnecessary.

Subject: RE: Javascript Help

I’ll just refer you to ECMA 262 7.9 for the semicolon thing. (Download at http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf if necessary.)

A number of statements, including return, always require explicit semicolons. “}” and some line terminators will implicity insert semicolons, but relying on “appears on a different line” behaviour is just kidding yourself, especially if you’re in an editor that wraps. The semicolon statement terminator is always there – relying on the interpreter to insert the semicolon for you is not a “best practices” approach.

Subject: RE: Javascript Help

Yeah…like I said…you don’t need them.

Subject: Javascript Help

I use

if (confirm(“Are you sure that all of the information that you entered is correct?”))

   {   

    form.submit();  

    }

Subject: Javascript Help

When you use a Formula Language button, the value of the field won’t be changed until the document is safely back at the server. That is, the value you’re testing for in your onsubmit doesn’t exist until the afteralreadysubmittedanditstoolate event. You’ll need to use JavaScript to set the value if you want to conditionally display the confirm. That being said, the only safe way to set the date is to do it back at the server. JavaScript methods will depend on the user’s workstation settings for date-string conversion, and since the value is a string until it hits the server, you can’t depend on the re-conversion to a date working properly. Use a WebQuerySave agent (or a funky computed field formula) to set the date.