Show/hide container start variable depending on variable choice in front end in a record producer

ronro2
Tera Contributor

Hello guys!

I wonder if there is any way to show/hide display title of a Container start in a record producer, in front-end? The end user should see the title of the Container start if a certain checkbox is true in the form, otherwise it shoudl be hidden. 

I am trying to show/hide with a client script, but it does not work. It the title remains static in the form in front end. Does it have something to do with the variable type? Or can it be achieved in some other way?

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }

    // Hämta referens till container start variabeln
    var containerStart = g_form.getControl('organisational_placement_container_start');

    // Visa eller dölj baserat på checkboxens värde
    if (newValue) {
        containerStart.style.display = 'block';
    } else {
        containerStart.style.display = 'none';
    }
}



ronro2_0-1742461834032.png


As you see in illustration above, the idea is that the title "Organisatorisk placering" (container start variable( is made visible when end user has  checked 'Placering i organisationsstrukturen' under 'Select type of information to change?' here.

Why this is important is because i want to be able to have sections in back end:

ronro2_1-1742463065761.png

 

Thank you in advance

4 REPLIES 4

Rohit  Singh
Mega Sage

Hi @ronro2 ,

 

You can use g_form.setVisible("variable_name",true) -> to show and g_form.setVisible("variable_name",false) -> Hide

Create on Change client script on field check box.

if(newValue =="true")
{
  g_form.setVisible("variable_name",true);
}
else
{
 g_form.setVisible("variable_name",false);
}

 

If my response helped, please hit the Thumb Icon and accept the solution so that it benefits future readers.

Regards,
Rohit

Ankur Bawiskar
Tera Patron
Tera Patron

@ronro2 

you will have to use DOM manipulation for this which is not recommended

You need to find out the sys_id of the container and then use:

Ensure Isolate Script = False for your client script so that it allows DOM manipulation



try {
$("container_c2a3b6f3c3fa9e90d8a538ca050131c8").hide();
} catch(error) {}

// Replace "c2a3b6f3c3fa9e90d8a538ca050131c8" with the sys_id of your Container Start variable.

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

ronro2
Tera Contributor

It was solved! 

A container start + end was needed. I missed the container end variable! 🙂 

@ronro2 

Glad to know.

Share the detailed steps and script so that it helps other members as well.

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