- 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:20 PM
Hi @Ravi Gaurav ,
It is making the sub product field ready only all time.
On change of Product the sub product field is initially empty has to click on the button to choose the record. If the list is empty it should read only after selecting of Product.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2025 02:27 AM
Choose your client script type wisely.. choose onChange on subProd field it should work
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/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2025 02:47 AM
Not working.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2025 03:08 AM
Lot of discussion going on this thread.
But I would like to mention that your requirement is not a valid business requirement.
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2025 11:00 PM
Your business requirement it not clear.
Are you saying if there is no user in the reference field when user clicks the Lookup icon?
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader