How to make setReadOnly function work for client script

Beata I_owiecka
Kilo Guru

Hi,

I created a client script that is setting company field of an incident same as caller.company.

The problem is that I want to set company filed of an incident readOnly after that, and that's not working.

I searched for conflicting UI policies and scripts, but found none. And then I found a business rule that's doing the same but after saving or updating, deactivated it, but problem is still occurring. What else can I do? Maybe I overlooked sth? Please help.

 

my client script

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	var ga = new GlideAjax('UserCompany');   
    ga.addParam('sysparm_name', 'getUserCompany');   
	ga.addParam('sysparm_user_id', newValue);   
	ga.getXMLAnswer(function(answer){
		alert("answer: " + answer);
		g_form.setValue('company', answer);
		g_form.setReadOnly('company', true);
	});
}

my script include

var UserCompany = Class.create();
UserCompany.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
	
	getUserCompany: function() {
		var userRecord = new GlideRecord('sys_user');
		var user_id = this.getParameter('sysparm_user_id');
		userRecord.get(user_id);
		var companyId = userRecord.company.sys_id.getDisplayValue();
		return companyId;
	},

    type: 'UserCompany'
});

 

business rule I found

find_real_file.png

find_real_file.png

I also share what other scripts I found for incident table, maybe someone will notice sth.

I looked for it here:

find_real_file.png

client scripts:

find_real_file.pngfind_real_file.png

ui policies:

find_real_file.png

data policies:

find_real_file.png

business rules:

find_real_file.pngfind_real_file.pngfind_real_file.pngfind_real_file.pngfind_real_file.pngfind_real_file.png

1 ACCEPTED SOLUTION

ok, just write below UI Policy and check.

find_real_file.png

UI policy Action with Company field with Readonly = true

 

View solution in original post

17 REPLIES 17

I created UI policy instead of 

g_form.setReadOnly('company', true);

and it works. But I'm not that happy about it . Still don't know why client script didn't work. Strange. But at least it works. Thanks @Harshad Wagh  find_real_file.png

I guess you were creating the client script in your scope, just for test if you can create the client script in global scope and test?

Thanks

Harshad

AnirudhKumar
Mega Sage
Mega Sage

I guess you have created a new field called Company on the incident table?

If so, that is not required. You could bring the caller's company field by simply dot-walking on the incident form. That way you do not need autopopulate it nor do you have to make it readonly. Both would automatically happen.

 

Anyway, you setReadOnly script looks alright to me.

just try this silly alternative: 

g_form.setReadOnly('company', 'true');

If that doesn't work, then i guess your company field is mandatory, which is why system is unable to make it readonly, coz that would be conflict

Hi AnirudhKumar,

 
No, I havent created the Company field, it's a core field of an incident table.

It's also required, I have to auto-populate it by a client script onChange, that's my task requirement.

g_form.setReadOnly('company', 'true');

I already tried this alternative, but didn't worked.

 

Thanks for feedback!

Muhammad Khan
Mega Sage
Mega Sage

@Beata IÅ‚owiecka 

Use Form-Layout and bring the dot-walked company field on the form and use this onChange() client script which is on caller_id field, it should work for you.

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

	g_form.getReference('caller_id', callBack);
	
	function callBack(gr){
		g_form.setReadOnly('gr.company', true);
	}

}