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

@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