Client script

tushar_ghadage
Tera Contributor

Hi guys , 

 

State choice = Closed should be hidden from the form if State = new, for Change Tasks

 

for that I have written on load client script :

 

function onLoad() {
   //Type appropriate comment here, and begin script below
    g_form.addOption('state','3');

   if(g_form.getValue('state') == '1')
   {
    g_form.removeOption('state','3');
   }
}
its working but for other option too showing the choice ' closed 'hidden ..
 
any correction guys I  need to make??
 
thankss !!
12 REPLIES 12

Screenshot (173).png

Ehab Pilloor
Mega Sage

Hi @tushar_ghadage,

 

Script needs correction to only remove when State is New. Also you would have to make OOB Closed choice inactive for this case as removeOption would remove it for future choices as well so we have to use addOption in script. 

Use this script:

function onLoad() {
    // First, always add the 'Closed' option to ensure it's present
    g_form.addOption('state', '3', 'Closed');

    // Then, if state is 'New', remove it
    if (g_form.getValue('state') == '1') {
        g_form.removeOption('state', '3');
    }
}

 

Regards,

Ehab Pilloor

its hiding it for other option as well 

Screenshot (172).png

Hi @tushar_ghadage, did you try saving the record and check?

It will not show on new record, remember its onLoad script, save and try it.

 

reshmakore57046
Tera Expert

Hello @tushar_ghadage ,

To achieve this efficiently, you can use a onLoad client script.

 

function onLoad() {
var closedValue = '3'; // Value for 'Closed'
var closedLabel = 'Closed'; // Update if label is different

// Reusable function to update the visibility of 'Closed' option
function updateClosedOption(stateValue) {
if (stateValue === '1') { // '1' = New
g_form.removeOption('state', closedValue);
} else {
if (!g_form.getOption('state', closedValue)) {
g_form.addOption('state', closedValue, closedLabel);
}
}
}

// Run once on form load
updateClosedOption(g_form.getValue('state'));

// Watch for changes to the state field
g_form.watch('state', function(newValue) {
updateClosedOption(newValue);
});
}

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Reshma Kore.