URGENT * Client script on Change not working on variable set in Catalog Item

JérômeM
Tera Contributor

Hello Community! 
I have an urgent issue that I need to solve. 
I created the following client script at catalog item level and it works fine. 
However, my users are in the need to add multiple rows, so I created a variable set and included the script in there, but it doesn't work anymore. 
Any idea why an onChange value doesn't work in variable set ? Should I do it differently ? 
See my code : 

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

   // Clear any previous messages
   g_form.hideFieldMsg('line_number_2');

   var lineNumber = g_form.getValue('line_number_2');

   if (lineNumber.indexOf('/OOB') > -1 ) {
      g_form.showFieldMsg('line_number_2', "This line is of type Out of Band", 'info', false);
   } else if (lineNumber.indexOf('/B') > -1 ) {
      g_form.showFieldMsg('line_number_2', "This line is of type Ethernet 100 Mbits/S", 'info', false);
   } else if (lineNumber.indexOf('/C') > -1 ) {
      g_form.showFieldMsg('line_number_2', "This line is of type Ethernet 1.000 Mbits/S", 'info', false);
   } else if (lineNumber.indexOf('/D') > -1 ) {
      g_form.showFieldMsg('line_number_2', "This line is of type Ethernet 10.000 Mbits/S", 'info', false);
   } else if (lineNumber.indexOf('/E') > -1 ) {
      g_form.showFieldMsg('line_number_2', "This line is of type Dark Fiber", 'info', false);
   } else if (lineNumber.indexOf('/F') > -1 ) {
      g_form.showFieldMsg('line_number_2', "This line is of type Ethernet 200 Mbits/S", 'info', false);
   } else if (lineNumber.indexOf('/G') > -1 ) {
      g_form.showFieldMsg('line_number_2', "This line is of type Ethernet 500 Mbits/S", 'info', false);
   } else if (lineNumber.indexOf('/H') > -1 ) {
      g_form.showFieldMsg('line_number_2', "This line is of type Ethernet 2.000 Mbits/S", 'info', false);
   } else {
      g_form.showFieldMsg('line_number_2', 'Type of the line is not specified yet', 'info', false);
   }
}


Thanks 
Jérôme

1 ACCEPTED SOLUTION

@JérômeM 

nope, I am referring create a fresh variable within that MRVS and deactivate the line_number_2

Then create fresh client script on that new variable and then see

Did the alert come?

Remember the onChange will run only when you type the details in variable and you click elsewhere on that modal, it won't trigger as and when you type.

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

9 REPLIES 9

@JérômeM 

did you try adding alert and see if that onchange is triggering?

Try creating a fresh single line text variable and then add the client script on it, disable the one which you already created

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

    alert('inside onchange');

    // Clear any previous messages
    g_form.hideFieldMsg('line_number_2');

    var lineNumber = g_form.getValue('line_number_2');

    if (lineNumber.indexOf('/OOB') > -1) {
        g_form.showFieldMsg('line_number_2', "This line is of type Out of Band", 'info', false);
    } else if (lineNumber.indexOf('/B') > -1) {
        g_form.showFieldMsg('line_number_2', "This line is of type Ethernet 100 Mbits/S", 'info', false);
    } else if (lineNumber.indexOf('/C') > -1) {
        g_form.showFieldMsg('line_number_2', "This line is of type Ethernet 1.000 Mbits/S", 'info', false);
    } else if (lineNumber.indexOf('/D') > -1) {
        g_form.showFieldMsg('line_number_2', "This line is of type Ethernet 10.000 Mbits/S", 'info', false);
    } else if (lineNumber.indexOf('/E') > -1) {
        g_form.showFieldMsg('line_number_2', "This line is of type Dark Fiber", 'info', false);
    } else if (lineNumber.indexOf('/F') > -1) {
        g_form.showFieldMsg('line_number_2', "This line is of type Ethernet 200 Mbits/S", 'info', false);
    } else if (lineNumber.indexOf('/G') > -1) {
        g_form.showFieldMsg('line_number_2', "This line is of type Ethernet 500 Mbits/S", 'info', false);
    } else if (lineNumber.indexOf('/H') > -1) {
        g_form.showFieldMsg('line_number_2', "This line is of type Ethernet 2.000 Mbits/S", 'info', false);
    } else {
        g_form.showFieldMsg('line_number_2', 'Type of the line is not specified yet', 'info', false);
    }
}

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

Hey @Ankur Bawiskar 
I tried already in a fresh field in a catalog variable and it was working perfectly. 
I have the need to run this in the variable set, and there, it stopped working. 
When I load the form however, it sets the last if value to Type of the line is not specified yet, it seems that only OnChange is not working in the variable set. 

@JérômeM 

nope, I am referring create a fresh variable within that MRVS and deactivate the line_number_2

Then create fresh client script on that new variable and then see

Did the alert come?

Remember the onChange will run only when you type the details in variable and you click elsewhere on that modal, it won't trigger as and when you type.

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

@JérômeM 

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

Thanks man! It works fine. 🙂