- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2025 08:30 AM
Hey Guys!
I want to change the active tab when pressing the UI Action button called "Vilande", so that the tab with the "comments" section becomes the active one.
So here is my UI Action code:
function onClickVilande() {
// Gör comments-fältet obligatoriskt
g_form.setMandatory('comments', true);
// Kontrollera om fältet "comments" är ifyllt
if (g_form.getValue('comments') == '') {
alert('Vänligen skriv en allmän kommentar i fliken "Anteckningar" om orsaken till varför behov får status "Vilande" och tryck på knappen "Vilande" igen. ');
return false;
}
// Öppnar bekräftelsefönster
var answer = confirm('Behovet kommer att hamna i status "Vilande".');
// Om OK väljs, ändra status för posten
if (answer == true) {
gsftSubmit(null, g_form.getFormElement(), '7953459bb8c7dad0ebb68168641a48d2');
} else {
return false;
}
}
// Om det inte finns några webbläsarfel, ändra status
if (typeof window == 'undefined') {
current.state = 10;
current.update();
action.setRedirectURL(current);
}
// Anropa onClickVilande-funktionen när UI Action-knappen klickas
onClickVilande();
Here is the ID for the tab upon inspection:
Any ideas?
Thanks in advance!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2025 08:54 AM - edited 01-20-2025 09:01 AM
Hi @ronro2,
To make the "comments" tab active when the "Vilande" UI Action button is pressed, you need to include a function to activate the appropriate tab. In ServiceNow's client-side scripting, you can use g_tabs2.setActive() (for older tabs) or the appropriate method to activate a tab.
Here's your updated UI Action code:
function onClickVilande() { // Gör comments-fältet obligatoriskt g_form.setMandatory('comments', true); // Kontrollera om fältet "comments" är ifyllt if (g_form.getValue('comments') == '') { // Byt till fliken med "comments" g_tabs2sections.setactive(2); // Index of the "comments" tab (starts at 0) alert('Vänligen skriv en allmän kommentar i fliken "Anteckningar" om orsaken till varför behov får status "Vilande" och tryck på knappen "Vilande" igen.'); return false; } // Öppnar bekräftelsefönster var answer = confirm('Behovet kommer att hamna i status "Vilande".'); // Om OK väljs, ändra status för posten if (answer == true) { gsftSubmit(null, g_form.getFormElement(), '7953459bb8c7dad0ebb68168641a48d2'); } else { return false; } } // Om det inte finns några webbläsarfel, ändra status if (typeof window == 'undefined') { current.state = 10; current.update(); action.setRedirectURL(current); } // Anropa onClickVilande-funktionen när UI Action-knappen klickas onClickVilande();
Explanation:
g_tabs2.setActive(2):
- This activates the "comments" tab. Replace 2 with the correct index of the "comments" tab if necessary (indexing starts from 0).
Tab Activation:
- This ensures that when the user is prompted to fill in the "comments" field, they are automatically taken to the relevant tab.
Other Logic:
- The rest of the logic for mandatory fields, confirmation, and form submission remains unchanged.
Best regards,
Siddhesh Jadhav
Kindly mark my answer as helpful if it solves your query.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2025 08:44 AM
in your UI action's client side code set focus on any 1 field present in that form section
g_form.getControl('fieldName').focus();
But your UI action is client+server so when when user confirms the control will move to server side and record will get updated.
what's the use of setting that section as active or focusing on that section since form will get reloaded after saving?
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2025 08:54 AM - edited 01-20-2025 09:01 AM
Hi @ronro2,
To make the "comments" tab active when the "Vilande" UI Action button is pressed, you need to include a function to activate the appropriate tab. In ServiceNow's client-side scripting, you can use g_tabs2.setActive() (for older tabs) or the appropriate method to activate a tab.
Here's your updated UI Action code:
function onClickVilande() { // Gör comments-fältet obligatoriskt g_form.setMandatory('comments', true); // Kontrollera om fältet "comments" är ifyllt if (g_form.getValue('comments') == '') { // Byt till fliken med "comments" g_tabs2sections.setactive(2); // Index of the "comments" tab (starts at 0) alert('Vänligen skriv en allmän kommentar i fliken "Anteckningar" om orsaken till varför behov får status "Vilande" och tryck på knappen "Vilande" igen.'); return false; } // Öppnar bekräftelsefönster var answer = confirm('Behovet kommer att hamna i status "Vilande".'); // Om OK väljs, ändra status för posten if (answer == true) { gsftSubmit(null, g_form.getFormElement(), '7953459bb8c7dad0ebb68168641a48d2'); } else { return false; } } // Om det inte finns några webbläsarfel, ändra status if (typeof window == 'undefined') { current.state = 10; current.update(); action.setRedirectURL(current); } // Anropa onClickVilande-funktionen när UI Action-knappen klickas onClickVilande();
Explanation:
g_tabs2.setActive(2):
- This activates the "comments" tab. Replace 2 with the correct index of the "comments" tab if necessary (indexing starts from 0).
Tab Activation:
- This ensures that when the user is prompted to fill in the "comments" field, they are automatically taken to the relevant tab.
Other Logic:
- The rest of the logic for mandatory fields, confirmation, and form submission remains unchanged.
Best regards,
Siddhesh Jadhav
Kindly mark my answer as helpful if it solves your query.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2025 02:24 PM - edited 01-21-2025 12:44 AM
Hey man, do you know where I can find the index of the tabs? And by the way, the alert is not working anymore.
*EDIT* It works, I made the mistake of not using camelCase the right way. So never mind. Thank you mr!