How to Update Field Value in Record via Button Click UI Macro in ServiceNow?

ziabytes
Kilo Contributor

Greetings lovely Ladies and Gentlemen.

How I can code the UI Macro to update the value of the "u_counter" field in a particular record?

Context:

Button1(On Click):

  1. adds 1 to the current value of "u_counter"

  2. updates the record(if needed)

  3. refreshes the page

Button2(On Click):

  1. resets value of "u_counter" to 0

  2. updates the record(if needed)

  3. refreshes the page

I know I can refresh the page with just adding window.location.reload(true); , so that is not too bad, but I can't figure how to do step 1 (and if needed, step 2) correctly. I have read and also observed how trying to use GlideRecord in a UI Macro isn't recommended as much due to issues with reliability and/or stability.

I want to note the the change to the value needs to be "permanent", at least in the sense that when I refresh the page I see the field populated with the new value.

Would be grateful for any help.

2 REPLIES 2

Kieran Anson
Kilo Patron

Hi,

I'd do something similar to the below.

Name: Update Counter

Action Name: update_counter

Client: true

OnClick: runClientCode()

function runClientCode(){
	gsftSubmit(null, g_form.getFormElement(), 'update_counter'); //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')
	runBusRuleCode();

//Server-side function
function runBusRuleCode(){
	current.u_counter ++;
	current.update();
	action.setRedirectURL(current);
}

Hi, Checking in on whether my reply resolved your issue? If so please Mark Correct and /or 👍 Helpful if you find my response worthy based on the impact.
By doing so you help other community members find resolved questions which may relate to an issue they're having.