ui action

carlosromero
Giga Contributor

i have created a field (checkbox) on change form and its doing the following when is set to true is taking away the request apporval action both the one from the top of the form and the one at the bottom of the form using this code

client script

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

    if (isLoading|| newValue === '') {

          return;

    }

if (g_form.getValue('u_vendor_change') == 'true' ) {

document.querySelectorAll("#state_model_request_assess_approval")[0].hide();

        document.querySelectorAll("#state_model_request_assess_approval")[1].hide();

}

   

else {

document.querySelectorAll("#state_model_request_assess_approval")[0].show();

        document.querySelectorAll("#state_model_request_assess_approval")[1].show();

       

    }

}

when is click save is saving the changes, however if i want to set to false again the checkbox is displaying an error message

find_real_file.png

does anyone know what i am doing wrong

1 ACCEPTED SOLUTION

Here is a try/catch code with your script :


try{


  if (g_form.getValue('u_vendor_change') == 'true' ) {


            document.querySelectorAll("#state_model_request_assess_approval")[0].hide();


            document.querySelectorAll("#state_model_request_assess_approval")[1].hide();


  }


  else {


            document.querySelectorAll("#state_model_request_assess_approval")[0].show();


            document.querySelectorAll("#state_model_request_assess_approval")[1].show();


  }


  }catch(error) {


            console.log("Error on vendor change script "+error);


  }


View solution in original post

13 REPLIES 13

chirag_bagdai
ServiceNow Employee
ServiceNow Employee

Hi,



Can you please check that "state_model_request_assess_approval" ID is present on HTML form or not ? and also please update .hide() with style.display = "none";




yes the state_model_request_assess_approval is presented on the form. i just change the code but still getting the error



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


    if (isLoading|| newValue === '') {


          return;


    }




if (g_form.getValue('u_vendor_change') == 'true' ) {



document.querySelectorAll("#state_model_request_assess_approval")[0].style.display = "none";


        document.querySelectorAll("#state_model_request_assess_approval")[1].style.display = "none";


}


   


else {


document.querySelectorAll("#state_model_request_assess_approval")[0].show();


        document.querySelectorAll("#state_model_request_assess_approval")[1].show();


       


    }


}



find_real_file.png


I think, the issue is that - you are trying to hide multiple elements by same ID using querySelectorAll.



querySelectAll can be use for "Class".



Can you please try below script :



jQuery("[id=state_model_request_assess_approval]").hide();


Laurie Marlowe1
Kilo Sage

Have you tried using g_form.setDisplay?   Displays field if true, hides field if false.   I'm not sure what version you are on, but it is discouraged to use DOM manipulation, as it may not be compatible with future upgrades.



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


    //If the page isn't loading


    if (!isLoading) {


    //If the new value isn't blank


    if (newValue != '') {


    g_form.setDisplay('priority', false);    


    }


    else


    g_form.setDisplay('priority', true);


    }


    }



Thanks,



Laurie



Please mark Correct, Helpful, or Like as applicable!