Change active tab through UI Action

ronro2
Tera Contributor

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. 

ronro2_0-1737390477089.png


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: 

ronro2_1-1737390648095.png


Any ideas? 

Thanks in advance!

1 ACCEPTED SOLUTION

Siddhesh Jadhav
Kilo Sage

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:

  1. 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).
  2. Tab Activation:

    • This ensures that when the user is prompted to fill in the "comments" field, they are automatically taken to the relevant tab.
  3. 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.

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@ronro2 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Siddhesh Jadhav
Kilo Sage

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:

  1. 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).
  2. Tab Activation:

    • This ensures that when the user is prompted to fill in the "comments" field, they are automatically taken to the relevant tab.
  3. 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.

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!