
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-19-2022 04:36 AM
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
I also share what other scripts I found for incident table, maybe someone will notice sth.
I looked for it here:
client scripts:
ui policies:
data policies:
business rules:
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-19-2022 06:48 AM
ok, just write below UI Policy and check.
UI policy Action with Company field with Readonly = true

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-19-2022 06:39 AM
'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... 😞
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-19-2022 09:28 AM
The challenge which you will face is shown in the image below.
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);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-19-2022 05:24 AM
Hello
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