- 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:16 PM
Hi @Amit ,
This UI policy makes sub product ready only. Requirement is if on change of Product, if there is no sub product then make the Sub product field read only.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2025 10:49 PM
Hi @Samiksha2
Could you clarify what you mean by "there is no any record in the reference field"?
Do you mean:
1. There are no existing records available when looking up values in the reference field?
or
2. The reference field itself is empty?
Understanding this will help in suggesting the right solution.
Cheers,
Tai Vu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2025 11:18 PM
Hi @Tai Vu ,
Yes you are right
There are no existing records available when looking up values in the reference field?- This is the requirement.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2025 11:19 PM
this is not feasible.
is that reference field having any reference qualifier on it which restricts the value based on some other field?
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:32 PM
@Samiksha2 You can try this then.
Script Include Client Callable
var ProductUtils = Class.create();
ProductUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getTotalSubProduct: function(){
var productId = this.getParameter('sysparm_product');
var noRecord = true;
var gaSubProduct = new GlideAggregate('<your_sub_product_table>'); //replace your sub-product table
gaSubProduct.addQuery('<product_field_name>', productId); //replace your product field name
gaSubProduct.addAggregate('COUNT');
gaSubProduct.query();
if(gaSubProduct.next()){
noRecord = gaSubProduct.getAggregate('COUNT') == 0;
}
return noRecord;
},
type: 'ProductUtils'
});
OnChange Client Script Product
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading) {
return;
}
if (newValue === '') {
g_form.setReadOnly('u_sub_product', false);
return;
}
var ga = new GlideAjax('ProductUtils');
ga.addParam('sysparm_name', 'getTotalSubProduct');
ga.addParam('sysparm_product', newValue);
ga.getXMLAnswer(function(response) {
g_form.setReadOnly('u_sub_product', response);
});
}
Cheers,
Tai Vu