How to Show/Hide URL Field When Required

msekla
Tera Contributor

I currently have a hidden URL field made and I want it to appear in my Generic Request form once I select a Configuration Item that has a specific URL value I put in the CMDB.

If the chosen Configuration Item has the "URL required" field set to "yes" on the CI listing, then:
1. Show the "URL" field on the Generic Request form (i.e. make it visible)
2. Make the "URL" field mandatory on the Generic Request form

 

I have attached a snippet of what I have so far and I don't know why it's not working as in when I select a Configuration Item that has the URL value that I put in the CMDB, that the script looks for, it should then make the URL field appear on my form and make it mandatory.

Please help and let me know what I should do in detail. Thank you.

1 ACCEPTED SOLUTION

@msekla , it might be because of setVisible so try replace this with setDisplay() 

 

g_form.setDisplay('url',true);

 

replace in both if and else if 

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang

 

View solution in original post

26 REPLIES 26

Hi @msekla ,

 

Few things to note down,

Url required field on cmdb_ci table should be of type True/false

Url variable on the Generic Request form should be of type Url (which i have used here)

remove all alert and info

Configuration Item variable is type of reference and referencing cmdb_ci table 

 

This is working in my pdi please give it a try and let me know if you still face any issue,

 

Script include : 

swathisarang98_0-1709159145282.png

 

 

 

var getUrlRequired = Class.create();
getUrlRequired.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getUrl: function() {
        var sysid = this.getParameter('sysparm_id');
      //  gs.info('Line num 6 ' + sysid);
        var gr = new GlideRecord('cmdb_ci');
        gr.addQuery('sys_id', sysid);
		gr.query();
        if (gr.next()) {
			
			var url = gr.u_url;  //url required field back end name
			//gs.info('line number 10'+ url);
			if(url == true || url == 'true'){
				return true;
			}
			else if(url == false || url == 'false'){
				return false;
			}

        }

    },

    type: 'getUrlRequired'
});

 

 

 

catalog Client script: Onchange:

swathisarang98_1-1709159230523.png

 

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        g_form.setVisible('url', false);
        return;

    }

	var sysid = g_form.getValue('configuration_item'); //configuration item variable backend name
	alert('sysid '+sysid);
	var url = new GlideAjax('getUrlRequired'); //script include name
	url.addParam('sysparm_name','getUrl'); //function name inside script include
	url.addParam('sysparm_id',sysid);
	url.getXML(getResponse);
}
function getResponse(response){
	var ans = response.responseXML.documentElement.getAttribute('answer');
	//alert('answer' + ans);
	if(ans == true || ans == 'true')
	{
		g_form.setVisible('url',true);
		g_form.setMandatory('url',true);
	}
	else if(ans == false || ans == 'false'){
		g_form.setMandatory('url',false);
		g_form.setVisible('url',false);
		
	}

}



  

 

 

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang

 

Where do I make the change where it doesn't make a pop-up for me to click next on and it just pops of the URL field on the form instantly as soon as a configuration item that needs a URL required.

@msekla could you please share the screen shot of the issue?

It doesn't let me for some reason, may I speak to you over teams if possible and I can share my screen? I would really appreciate it because I have been stuck on this project for quite a bit.

@msekla sorry i dont use teams you can try one more thing , for testing purpose i have added alert and logs so you can remove those,

 

so in code wherever alert or gs.info is there remove it and try in both client script and in script include 

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang