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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2020 08:40 PM
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):
-
adds 1 to the current value of "u_counter"
-
updates the record(if needed)
-
refreshes the page
Button2(On Click):
-
resets value of "u_counter" to 0
-
updates the record(if needed)
-
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.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2020 05:18 PM
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);
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2020 09:01 AM
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.