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

'gr.company' will give us actual company name. I don't think that's correct, because we should provide a field name here, not the actual value. Is that right?

g_form.setReadOnly('fieldName', true);

 

but besides all: wow, great to know it's possible. These callbacks and dot-walking, nice! thanks. I'm new to servicenow and js 🙂

edit: I tried that anyway but still can't make it readOnly... 😞

@Beata IÅ‚owiecka 

The challenge which you will face is shown in the image below.

find_real_file.png

Above image shows that the caller field is dependent on company field, means once company field value is populated then you will only be able to select those users in the caller field who belong to that same company. So, if it is possible to remove that dependency, then the below script should work for you. 

Note: Test/Use it in your PDI only for better understanding.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        g_form.setReadOnly('company', false);
        g_form.clearValue('company');
        return;
    }

    g_form.getReference('caller_id', callBack);

    function callBack(caller) {
        g_form.setValue('company', caller.company);
        g_form.setReadOnly('company', true);
    }

}

Mahendra RC
Mega Sage

Hello @Beata IÅ‚owiecka 

Please check with below script. YOu should not run the Client script when your form is loading. I believe running the onChange Client script when form is loading is creating issue. Can you please check once with below script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
	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);
	});
}

If my answer helped you in any way then please do mark it as helpful. If it answered your question then please mark it correct and helpful. This will help others with similar issue to find the correct answer.

Thanks