Help with Client script When a Particular Configuration Item is choosen

Shree Nag
Tera Expert

Hello,

I have a requirement to make a state value( "Work In Progress" )  visible only when a particular configuration item is chosen on an incident form.

 

If a user choses "Abc" in configuration item on an incident form, Work In progress state ( value =-5) value should show up in State field.

Can you all help me with the client script for this please.

 

Appreciate your response.

 

-Thanks

Shree

 

1 ACCEPTED SOLUTION

Hi @Shree Nag 

 

Can you try the below script

 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    // If the form is loading or the new value is empty, do nothing
    if (isLoading) {
        var citem = g_form.getValue('cmdb_ci');
        return;
    }
    var citem = g_form.getDisplayBox('cmdb_ci').value;
    alert(citem);

    if (citem !== '' && citem.startsWith('Thi')) { // updated the code as per your need
        g_form.addOption('state', 'work_in_progress', 'Work In Progress');
    } else {
        g_form.removeOption('state', 'work_in_progress');

    }
}

 

 

Thanks and Regards

Sai Venkatesh

View solution in original post

24 REPLIES 24

Hi @Shree Nag 

 

The functionality is working fine my PDI and attached is the image.

 

with configuration item:

Screenshot (324).png

 

Without Configuration item:

 

Screenshot (325).png

 

Script Image:

 

cn299.PNG

 

Thanks and Regards

Sai Venkatesh

Thank You for trying @SAI VENKATESH .

Here is the new update. I right clicked on the state choices, and made "Work In progress"  inactive is true.

Here are my observations.

1. When there is no configuration item is selected, There is no Work in progress shown.

2. I enter the Configuration item "ABC", the Work in progress is shown in state field ( Yay !!!)

3. But when I remove the ABc configuration item, work in progress still shows in state.

 

We are almost there. The client script for Onchnage of Configuration item is:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    // If the form is loading or the new value is empty, do nothing
    if (isLoading || newValue === '') {
        return;
    }
   
    if (newValue !== '') {
        g_form.addOption('state', 'work_in_progress', 'Work In Progress');
    }
    else{
          g_form.remove('state', 'work_in_progress', 'Work In Progress');

    }
}
 

 

 

 

I'm assuming in the script below that the Client Script is set to run onChange of the CI field:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    //newValue contains the sys_id of the CI so we do NOT have to use g_form.getValue() to get it again
    //and we want to do this regardless of "isLoading" or if the CI is blank
    if (newValue == "sys_id of your CI") {
        g_form.addOption("state", "work_in_progress", "Work In Progress", 1);  //the "1" tells it where to show up in the list
    } else {
        g_form.removeOption("state", "work_in_progress");
    }
}

 

Using a hard-coded sys_id is not the best way to do it, especially if there are multiple CIs that might need the state value.

 

And you would want to keep the "Work in Progress" in your list of choices for list views and reporting/filtering purposes.

Ummm Looks Same on My side.

I will keep debugging.

 

Hi @Shree Nag 

 

Can you try the below script

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    // If the form is loading or the new value is empty, do nothing
    if (isLoading) {
        var citem = g_form.getValue('cmdb_ci');
        return;
    }
    var citem = g_form.getValue('cmdb_ci');
    alert(citem);

    if (citem !== '') {
        g_form.addOption('state', 'work_in_progress', 'Work In Progress');
    } else {
        g_form.removeOption('state', 'work_in_progress');

    }
}

 

It is working as expected by you

 

Thanks and Regards

Sai Venkatesh