on change and onload Client script is not working

tushar_ghadage
Tera Contributor

Hi all ,

 

I have requirement where 

when i open a new change task record default state is pending (-5) 

so when its in pending state i want to hide close state (3). 

and after i save the form then when i change the state to any other choice like open , cancelled or in progress 

then close should appear back again ( both choices are in state field )

 

for this I wrote on load and on change client script: 

function onLoad() {
// Delay to ensure choices are fully rendered
setTimeout(function () {
// Always add "Closed" in case it's missing
if (!gForm.getOption('state', '3')) {
gForm.addOption('state', '3', 'Closed');
}

// If state is 'Pending' (-3), remove "Closed"
if (gForm.getValue('state') == '-5') {
gForm.removeOption('state', '3');
}
}, 300); // Wait 300ms for form to finish rendering
 
-------------------------------------------------------------------
 
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) return;

// Always re-add "Closed" in case it was removed earlier
if (!gForm.getOption('state', '3')) {
gForm.addOption('state', '3', 'Closed');
}

// If user selects "Pending", hide "Closed"
if (newValue == '-3') {
gForm.removeOption('state', '3');
}
 
but both of them are not working
could any one suggest whats wrong here 
thanks!!
1 ACCEPTED SOLUTION

@tushar_ghadage 

Hope you are doing good.

Did my reply answer your question?

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

View solution in original post

5 REPLIES 5

Abbas_5
Tera Sage
Tera Sage

Hello @tushar_ghadage,

 

Business Rule for Subsequent State Changes:
  • Name: Show Close State on State Change
  • Table: Change Task
  • Advanced: True
  • When to run: After
  • Insert: True
  • Update: True
  • Condition: current.state.changes() (This ensures the rule runs only when the state field is updated)
  • Script: 
(function() {  if (current.state.getDisplayValue() != 'Pending') {    var closeState = g_form.getControl('close_state_field'); //Replace with your actual field name for the close state.    if (closeState) {      closeState.style.display = '';    }    // Or if using a choice list, you can use:    // g_form.setDisplay('close_state_field', true); //Replace with your actual field name for the close state.  }})();


If this is helpful, please hit the thumbs up button and accept the correct solution by referring to this solution in future it will be helpful to them.

Thanks & Regards,

Abbas Shaik

@Abbas_5 

Are you sure this script will work?

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

Ankur Bawiskar
Tera Patron
Tera Patron

@tushar_ghadage 

try this

1) create onLoad client script and write a helper function and the same can be invoked from onchange client script as well

Note: use correct state choice value & label to add

onLoad client script

function onLoad() {
    updateStateChoices(g_form.getValue('state'));
    g_form.setMandatory('state', true); // Optional: make state mandatory
}

function updateStateChoices(stateValue) {
    if (!g_form.getOption('state', '3')) {
        g_form.addOption('state', '3', 'Closed');
    }
    if (stateValue == '-5') {
        g_form.removeOption('state', '3');
    }
}

onChange of state

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) return;
    updateStateChoices(newValue);
}

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

@tushar_ghadage 

Hope you are doing good.

Did my reply answer your question?

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