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

Last issue I am having is that how do I make the URL field hide again if a configuration item that does not have a URL field required. Lets say I select a configuration item that does need a URL required and the URL field appears on the form as intended, then I select a different configuration item that does not need a URL required, the URL field still stays there on the form as a required field and that's not ideal. What would I need to change in either script for the URL field to disappear if a configuration item that does not require it is selected without needing to refresh the page in order to hide the URL. 

@msekla ,i just checked in my pdi without refreshing also that is happening, for 2nd time if user select a ci which does not have url required true it will hide the URL field,  please check your code whether it is same as my code? check client script mainly

 

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

Regards,

Swathi Sarang

This is what my code looks like and everything is working perfectly except that the URL does not hide if I select a configuration item that does not need a URL.

Client Script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        g_form.setVisible('ccw_url', false);
        return;
 
    }
 
var sysid = g_form.getValue('cmdb');
var url = new GlideAjax('getUrlRequired');
url.addParam('sysparm_id',sysid);
url.getXML(getResponse);
}
function getResponse(response){
var ans = response.responseXML.documentElement.getAttribute('answer');
{
g_form.setVisible('ccw_url',true);
g_form.setMandatory('ccw_url',true);
}
}

Script Include:
var getUrlRequired = Class.create();
getUrlRequired.prototype = Object.extendsObject(AbstractAjaxProcessor, {
 
    getUrl: function() {
        var sysid = this.getParameter('sysparm_id');
        var gr = new GlideRecord('cmdb_ci');
        gr.addQuery('sys_id', sysid);
gr.query();
        if (gr.next()) {
 
var url = gr.getValue('u_ccw_url');
if(url == true || url == 'true'){
return true;
}
else if(url == false || url == 'false'){
return false;
}
 
        }
 
    },
 
    type: 'getUrlRequired'
});

@msekla your client script code is a little bit different please check the part,

 

in your code if, else if is missing please correct it

 

 

 

function getResponse(response){
var ans = response.responseXML.documentElement.getAttribute('answer');
{
g_form.setVisible('ccw_url',true);
g_form.setMandatory('ccw_url',true);
}
}

 

 

 

my code:

 

 

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

For some reason with your code the URL field never appears even if I select a configuration item that does need a URL but with mine it actually appears if I select a configuration item that does need a URL.