Make a reference field read only

Samiksha2
Mega Sage

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.

Samiksha2_0-1738564271791.png

 

1 ACCEPTED SOLUTION

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.

View solution in original post

27 REPLIES 27

Anubhav24
Mega Sage
Mega Sage

 Hi @Samiksha2 ,

 

Assuming you are trying to fetch value at client side and then trying to make the field read only , can you try below if condition :

if (!sub)

{ g_form.setReadOnly('u_sub_product', true);} else {g_form.setReadOnly('u_sub_product', false); }

 

Please mark correct/helpful if my response helped you.

 

Hi @Anubhav24 ,

No it is making field readonly if record is there as well.

 

Thanks

Can you try to check with alerts to see if the value is there in "sub" variable , please post what this "sub" variable is returning after getvalue.

Amit Verma
Kilo Patron
Kilo Patron

Hi @Samiksha2 

 

Instead of Client Script, you can make use of UI Policy. Refer below screenshots for an example. You can customize it as per your requirement.

 

AmitVerma_0-1738564788739.png

AmitVerma_1-1738564819714.png

AmitVerma_2-1738564844046.png

 

Thanks and Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.