- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2025 10:31 PM
Hi All,
I want to make field read only when there is no any record in the reference field.
I have created a client script:
var sub = g_form.getValue('u_sub_product');
if ((sub == null) || (sub == '')) {
g_form.setReadOnly('u_sub_product', true);
}
But it is making field read only if records are there as well.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2025 05:51 AM
Hello @Samiksha2
I tried in PDI where over Case I have Product reference field and subproduct reference field, subproduct is dependent on product field and when click on magnifying glass it showcase all record which are mapped with product. Now as per your req if product is populated and if subproduct is not having such option with parent then field turns read-only. See below sample script and logic if it works for you.
Client Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading) {
var loadcall = new GlideAjax('ProductUtils');
loadcall.addParam('sysparm_name', 'getTotalSubProduct');
loadcall.addParam('sysparm_product', newValue);
loadcall.getXMLWait();
var loadAnswer = loadcall.getAnswer();
if(loadAnswer == 'true'){
g_form.setReadOnly('u_sub_product',true);
}else if(loadAnswer == 'false'){
g_form.setReadOnly('u_sub_product',false);
}
}
var ga = new GlideAjax('ProductUtils');
ga.addParam('sysparm_name', 'getTotalSubProduct');
ga.addParam('sysparm_product', newValue);
ga.getXMLWait();
var answer = ga.getAnswer();
if(answer == 'true'){
g_form.setReadOnly('u_sub_product',true);
}else if(answer == 'false'){
g_form.setReadOnly('u_sub_product',false);
}
}
Script Include which is client callable:
var ProductUtils = Class.create();
ProductUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getTotalSubProduct: function() {
var productId = this.getParameter('sysparm_product');
var isEmpty = "false";
var gaSubProduct = new GlideRecord('u_sub_product');
gaSubProduct.addEncodedQuery('u_actual_product.sys_id=' + productId + '^u_sub_productISNOTEMPTY');
gaSubProduct.query();
if (gaSubProduct.getRowCount() == 0) {
isEmpty = "true";
}
return isEmpty.toString();
},
type: 'ProductUtils'
});
If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.
Thanks & Regards
Viraj Hudlikar.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2025 11:46 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2025 11:53 PM
@Samiksha2 If it's editable in the list, you may want to consider ACL (field level) instead. Almost similar getTotalSubProduct function, requires minor adjustment to use in ACL advanced script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2025 11:56 PM
No @Tai Vu.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2025 12:09 AM
On selection of Product Sub product always be empty. user have to pick the record from the Sub product reference field.
Thats why the scripts are not working.
How to do that?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2025 10:59 PM
Hey @Samiksha2
Below is the revised code as (sub==null) won't work
var sub = g_form.getValue('u_sub_product');
if (!sub || sub == '') {
g_form.setReadOnly('u_sub_product', true);
} else {
g_form.setReadOnly('u_sub_product', false);
}
If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!
Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI
YouTube: https://www.youtube.com/@learnservicenowwithravi
LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/