Passing Variable Data from Client to Server Function in UI Action

David Cole1
Tera Expert

Referencing this question: https://community.servicenow.com/community?id=community_question&sys_id=67394325db5cdbc01dcaf3231f96...

 

I have created a UI action to interact with a RESTful server, and I am trying to allow it to use client/UI input to control a variable, numberOfLabels.

The REST integration is working perfectly, and will pass the right data if provided, however the solution in the above answer is not working when it comes to passing the client input to the server function. See below:

//Client-side 'onclick' function
function runClientCode(){
	
	//Prompt user for number of labels
	var numOfLabels = prompt("How many labels?", 1);
	
	//Error checking
	if(typeof(numOfLabels) == 'undefined' || numOfLabels == null) return false;
	
	//Set the value in the form
	g_form.setValue('numberOfLabels', numOfLabels.toString());
	
	//Call the UI Action and skip the 'onclick' function
	gsftSubmit(null, g_form.getFormElement(), 'print_label_inc'); //MUST call the 'Action name' set in this UI Action
}

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

function runServerCode(){
	try{
		
		//Creation first
		var r;
		
		//Determine where the labels should be printed based on assignment group
		var assignGroup = current.assignment_group.getDisplayValue();
		
		if(assignGroup == 'ITS - Desktop Support Tech Center') r = new sn_ws.RESTMessageV2('DSTC Label Printers', 'DSTC tickets');
		else r = new sn_ws.RESTMessageV2('DSTC Label Printers', 'Resnet tickets'); 

		//Set ticket number and user fields from ticket
		r.setStringParameterNoEscape('ticketNumber', current.getValue('number'));
		r.setStringParameterNoEscape('user', current.u_cf_user.getDisplayValue());
		r.setStringParameterNoEscape('numberOfLabels', current.numberOfLabels);

		//Execute and sto response
		var response = r.execute();
		//Determine response body
		var responseBody = response.haveError() ? response.getErrorMessage() : JSON.parse(response.getBody()).message;
		responseBody = JSON.parse(responseBody);
	}
	catch (ex) {
		var message = ex.message;
	}
}

I am setting the value in the form with:

g_form.setValue('numberOfLabels', numOfLabels.toString());

(The value must be ultimately be passed to the server as a string, ex: '1' instead of 1)

And attempting to retrieve it directly with

r.setStringParameterNoEscape('numberOfLabels', current.numberOfLabels);

However, when the request gets to the server, the numerOfLabels field is invariably 'undefined'. I am fairly new to SNow, so I am not sure if I need to pre-create this field (as it does not exist inside of the Incident table).

 

Thank you for any help in advance ^.^

1 ACCEPTED SOLUTION

Michael Jones -
Giga Sage

Are all of the values other than numberOfLabels defined when the request gets to the server?

I would say, for certain, that these lines: 

g_form.setValue('numberOfLabels', numOfLabels.toString());


r.setStringParameterNoEscape('numberOfLabels', current.numberOfLabels);

Would essentially do nothing if there is not a field on the actual current record to target (and numberOfLabels would not be a valid name on incident, you'd end up with u_numberoflabels). I'm not certain about the rest of the code, but I'd say yes, you need to add a field to incident and change those lines to reference that field name, at a minimum. 

I hope this helps!

If this was helpful, or correct, please be kind and mark the answer appropriately.

Michael Jones - Proud member of the GlideFast Consulting Team

I hope this helps!
Michael D. Jones
Proud member of the GlideFast Consulting Team!

View solution in original post

5 REPLIES 5

Michael Jones -
Giga Sage

Are all of the values other than numberOfLabels defined when the request gets to the server?

I would say, for certain, that these lines: 

g_form.setValue('numberOfLabels', numOfLabels.toString());


r.setStringParameterNoEscape('numberOfLabels', current.numberOfLabels);

Would essentially do nothing if there is not a field on the actual current record to target (and numberOfLabels would not be a valid name on incident, you'd end up with u_numberoflabels). I'm not certain about the rest of the code, but I'd say yes, you need to add a field to incident and change those lines to reference that field name, at a minimum. 

I hope this helps!

If this was helpful, or correct, please be kind and mark the answer appropriately.

Michael Jones - Proud member of the GlideFast Consulting Team

I hope this helps!
Michael D. Jones
Proud member of the GlideFast Consulting Team!

Hi Michael,

 

Thanks for your response! I added the field to the Form view as a String, and that solved the problem I was running into! The data is now getting to the server correctly 🙂

 

This may be better to ask in a new question, however the main goal was for the UI action to be the only 'extra' thing showing, but there is now a text field in the document which I'd rather not have there, as it's not really meant to be accessed by the user (directly, at least):

find_real_file.png

 

Would there be an efficient way to hide the display of the box, but still have the 

u_number_of_labels 

field accessible?

 

Thanks again,

David

Since you are using it to "hold" the value from your client-side call, it does need to exist on the form, but you can easily hide it from the user with a UI Policy. 

Right-click on the header of the form an select configure > UI Policies

Click New. 

Give it a name like "Hide Number Of Labels

Since you always want this to trigger, you can leave condition empty and right-click on the header again and select save to stay on the form for the UI action. 

Once the record is saved, at the bottom there will be a related list for UI Policy Actions, in that list click new. 

Select your field name and change visible to false and submit. 

That should hide the field when the form loads. 

I hope this helps!

If this was helpful, or correct, please be kind and mark the answer appropriately.

Michael Jones - Proud member of the GlideFast Consulting Team

I hope this helps!
Michael D. Jones
Proud member of the GlideFast Consulting Team!

You can use a UI policy with a UI policy action on this field to set visible to false.

Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022