- 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:03 PM
Hi @Samiksha2 ,
Create new UI policy on Sub Products table , Condition Sub Product field as empty , In ui policy actions make Sub Product field as read-only true.
If my response helped, please mark it as the accepted solution ✅ and give a thumbs up👍.
Thanks,
Anand
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2025 11:10 PM
Hello @Samiksha2
What are you trying to achieve here, trying to understand usecase?
Thanks & Regards,
Viraj Hudlikar.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2025 11:36 PM
Hi @Viraj Hudlikar ,
In the Case table,
Based on Product I am populating sub product.
When I am clicking on Product I have to select the sub product on clicking on the reference button.
If I am running this script it always giving empty value as I have to select the reference field to fill the sub product.
var sub = g_form.getValue('u_sub_product'); if ((sub == null) || (sub == '')) { g_form.setReadOnly('u_sub_product', true); }
Is there any way to check without selecting the reference field if value is there or not in the list.
Thanks,
Sam
- 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-03-2025 06:53 AM