help requesting for one onchange client script

Are Kaveri
Tera Contributor

Hello

 

I am working on below script

There is Request form.

Configuration item = Reference type

Impact = string type (Yes, No, Null)

 

when Configuration item = ABC

then donot allow Impact to Yes

 

for this i have written below script

 

Onchange Client script (Impact field)

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading) {
        return;
    }
else{
var ci = g_form.getValue('u_configuration_item').toString();

if(ci == 'ABC'){
  g_form.showFieldMsg('u_impact', "The impact should not be set to Yes when Configuration item is ABC", 'error');
}
}

 

I am not understanding how to check when impact is changing to throw error message.

9 REPLIES 9

Hi @Are Kaveri ,

 

var ci = g_form.getDisplayBox('u_configuration_item').value;

Above line will work.

 

Mark it as helpful and solution proposed if it serves your purpose.
Thanks,
Anand 

anshul_goyal
Kilo Sage

Hello @Are Kaveri,

Please refer to the code below and use it in an onChange Client Script:

anshul_goyal_0-1733477010791.png

 



function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    var ci = g_form.getValue('u_configuration_item');

    // Check if the configuration item is 'ABC' and if u_impact is set to 'Yes'
    if (ci == 'ABC' && newValue == 'Yes') {
        // Show the error message on the u_impact field
        g_form.showFieldMsg('u_impact', "The impact should not be set to Yes when Configuration item is ABC", 'error');
    }

}

 

Please mark my solution as Accepted and Helpful, if it works for you in any way!

Thanks

Juhi Poddar
Kilo Patron

Hello @Are Kaveri 

  • Write an onchange client script on impact field.

onChange Client script:

 

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

    // Get the values of the Configuration Item and Impact fields
    var ci = g_form.getValue('u_configuration_item'); // Reference field value
    var impact = g_form.getValue('u_impact'); // String field value

    // Check if the Configuration Item is "ABC" and Impact is set to "Yes"
    if (ci === 'ABC' && impact === 'Yes') {
        // Display an error message
        g_form.showFieldMsg('u_impact', "The impact should not be set to Yes when Configuration item is ABC", 'error');
        // Clear the Impact field value
        g_form.setValue('u_impact', '');
    }
}

 

After implementing this script, the Impact field will automatically display an error message and prevent the value from being set to "Yes" when the Configuration Item is "ABC."

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps others find the solution more easily and supports the community!"

 

Thank You
Juhi Poddar

 

Abhishek_Thakur
Mega Sage

Hello @Are Kaveri,

You can follow the below script.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var ci = g_form.getValue('u_configuration_item');
    if (ci == 'ABC' && newValue == 'Yes') {
        // Show the error message on the u_impact field
        g_form.showFieldMsg('u_impact', "The impact should not be set to Yes when Configuration item is ABC", 'error');
        return false;
    } else {
        return true;
    }
}​

 

 

Please mark my solution as accepted and give thumbs up, if it helps you.

 

Regards,

Abhishek Thakur

Anand Kumar P
Giga Patron
Giga Patron

Hi @Are Kaveri ,

 

var ci=g_form.getDisplayBox("u_configuration_item").value;

 

//as this field is reference field you will get sys_id, sys_id=ABC will not enter into if loop,  so use getDisplayBOX to get string value.

 

IMG_6602.jpeg

IMG_6603.jpeg

Mark it as helpful and solution proposed if it serves your purpose.
Thanks,
Anand