How to make a field readOnly and mandatory together in client script?

Glenn9
Giga Contributor

Hi ,

I wanted to make a field 'due_date mandatory an read only in new state. So i written client script as needed but the field is only getting mandatory not read only in new state but went we open an existing problem field becomes both mandatory and read only. There are more conditions also for the field to be readonly it is there in the code. But the problem prevails in the New state.

Attaching the code written in client script.

Thanks in Advance.

Thanks,

Glenn

function onLoad() {
//    Type appropriate comment here, and begin script below
	var cdt = g_form.getValue('opened_at'); //First Date/Time field
	var dttype = 'day'; //this can be day, hour, minute, second. By default it will return seconds.

	var ajax = new GlideAjax('ClientDateTimeUtils');
	ajax.addParam('sysparm_name','getDateTimeBeforeNow');
	ajax.addParam('sysparm_fdt', cdt);
	ajax.addParam('sysparm_difftype', dttype);
	ajax.getXML(doSomething);

	function doSomething(response){
		var answer = response.responseXML.documentElement.getAttribute("answer");
		if(answer < 14){
			if(!g_user.hasRole('problem_manager')){
				g_form.setReadOnly('due_date',true);
			}
		}
		g_form.setMandatory('due_date',true);
	}
   
}
1 ACCEPTED SOLUTION

Glenn9
Giga Contributor

Sorry 

Actually was away for day,

the issue was that value wasn't populated so both mandatory and readonly wont work together.

So the solution i did was instead of making it mandatory gave Bussiness rules tovalidate and populate error message as to the field should be mandatory.

 

Thanks,

Glenn

View solution in original post

5 REPLIES 5

Glenn9
Giga Contributor

Sorry 

Actually was away for day,

the issue was that value wasn't populated so both mandatory and readonly wont work together.

So the solution i did was instead of making it mandatory gave Bussiness rules tovalidate and populate error message as to the field should be mandatory.

 

Thanks,

Glenn