How do I pass a value back from Client Side script to be used in the server side script?

jesterize
Tera Contributor

I need to set a UI Action on a form that asks why a certain action was done. I have successfully got the answer, but how do I get it back to server for use? 

I have a form button called Reject, action name RejectAN, with Client box checked, and OnClick set to runClientCode();. Below is my script:

//Client-side 'onclick' funciton
function runClientCode(){
     var answer=prompt('Please provide a justification for rejecting this request.');
     if (answer!=null)
          {
               gsftSubmit(null, g_form.getFormElement(), 'RejectAN');
          }
     else
          {
               return false;
          }
}

//Code that runs without 'onclick'
//Ensure call to server-side function with no browser errors
if(typeof window == 'undefined'){
     runBusRuleCode();
}

//server-side function
function runBusRuleCode(){
     current.u_approval='rejected';
     current.u_work_notes="Rejected because " + answer;
     current.update();
}

Code runs successfully, but answer is coming back with null. Any ideas?

1 ACCEPTED SOLUTION

Jon Barnes
Kilo Sage

Generally when I do this type of thing, I have to do it by setting the value of a field on the form and then it will be available to you in the server script via current.u_field_name. So you may have to add a new field to your table in order to collect your data. If you don't want it visible, you can hide it with a UI Policy.

and in your client script part, right above the gsftSubmit line, put

g_form.setValue('u_field_name', answer);

View solution in original post

6 REPLIES 6

It would, but unfortunately I am using work notes for another purpose. Although Jon's solution required another field, it work for me. Thanks for the help!

Brent Sutton
Mega Sage

Try this

//Client-side 'onclick' funciton
function runClientCode(){
	var answer=prompt('Please provide a justification for rejecting this request.');
	if (answer!=null) {
		var msg = "Rejected because " + answer;
		g_form.setValue("work_notes", msg);
		gsftSubmit(null, g_form.getFormElement(), 'RejectAN');
	}
	else {
		return false;
	}
}

//Code that runs without 'onclick'
//Ensure call to server-side function with no browser errors
if(typeof window == 'undefined'){
	runBusRuleCode();
}

//server-side function
function runBusRuleCode(){
	current.u_approval='rejected';
	//current.u_work_notes="Rejected because " + answer;
	current.update();
	action.setRedirectURL(current);
}

Let me know if it worked for you

Brent

P.S. If my suggestion helped then please mark as helpful and/or correct so other community members can benefit from this information.