How to manage state flow using the state options on incident form

sony8
Tera Contributor

I have below requirement.

the state drop down values need to be managed based on each value.

drop down values are: New , In progress, Resolved and  Closed 

for the above values i have written below onchange of state script:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

var IncState = g_form.getValue('state');

if (IncState == '1') {

g_form.addOption('state', '1', 'New');

g_form.addOption('state', '2', 'In progress');
g_form.removeOption('state', '4', 'Resolved');
g_form.removeOption('state', '5', 'Closed');
} else if (IncState == '2') {

g_form.addOption('state', '1', 'New');

g_form.addOption('state', '2', 'In progress');
g_form.addOption('state', '4', 'Resolved');
g_form.removeOption('state', '5', 'Closed');

} else {

g_form.addOption('state', '1', 'New');

g_form.addOption('state', '2', 'In progress');
g_form.addOption('state', '4', 'Resolved');
g_form.addOption('state', '5', 'Closed');

}

}

 

 Here I am facing issue with onchange of state values.

when i change the state from new to In Progress without saving form, the value selected in state is in progress the values showing was New , In progress, Resolved ... Now without saving the form the user clicked on Resolved and was able to move to closed state..

 

Anyone having idea regarding isNewRecord method on onchange script.. 

expected output :

state: New --------option1 -New , option2 -In Progress

state: Inprogress -------- option1 -New , option2 -In Progress, option3 -Resolved

state: others(i.e. Resolved or Closed ) --- option1 -New , option2 -In Progress, option3 -Resolved, option4 - Closed

 

Please help me for checking old and New records for the same.

 

Thank you in Advance

 

 

1 ACCEPTED SOLUTION

I suggest you write onload client script instead of onchange client script, Change has onload logic and not on change logic, with this you will eliminate all these issues, You can refer below scripts from change but you may find it difficult to understand but I suggest you implement this on onload

https://instance/sys_script_client.do?sys_id=7ebdc3c15357101034d1ddeeff7b12e3&sysparm_record_target=...

 

https://instance/sys_script.do?sys_id=0aa20f7ec39132001488b731c1d3ae9e&sysparm_record_target=sys_scr...

Please hit like and mark my response as correct if that helps
Regards,
Musab

View solution in original post

8 REPLIES 8

Musab Rasheed
Tera Sage
Tera Sage

Hello,

If you want your code to run only for new Record then use g_form.isNewRecord()  function before your on change script starts.

Sample :

if(g_form.isNewRecord())

{

on change script

}

Also, check Change form, you can see similar logic over there

MusabRasheed_0-1669210849039.png

 

Please hit like and mark my response as correct if that helps
Regards,
Musab

@Musab Rasheed  can you please share the code for the same .. since my onchange was working but when we edit the state value it was behaving different for state = In progress we can select resolved option then user again check the state drop down getting closed option before saving form. In this case how to use that NewRecord method in my script? 

I suggest you write onload client script instead of onchange client script, Change has onload logic and not on change logic, with this you will eliminate all these issues, You can refer below scripts from change but you may find it difficult to understand but I suggest you implement this on onload

https://instance/sys_script_client.do?sys_id=7ebdc3c15357101034d1ddeeff7b12e3&sysparm_record_target=...

 

https://instance/sys_script.do?sys_id=0aa20f7ec39132001488b731c1d3ae9e&sysparm_record_target=sys_scr...

Please hit like and mark my response as correct if that helps
Regards,
Musab

@Musab Rasheed  working fine i have deactivated onchange and written it onload script same code.

Thank you so much