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

Runjay Patel
Giga Sage

HI @Samiksha2 ,

 

If this filed is dependent on product then cerate on change client script on product filed and then make ajax call to check whether value in sub product is there or not and return true and false. If false then make sub product readonly.

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

HI @Samiksha2 ,

 

Same i had suggested that you can achieve via onchange client script above. 

The community now supports multiple accepted solutions, so you can accept more than one answer.

 

KoteswaraVM
Tera Expert

Hi @Samiksha2 ,

Usually, UI Policies are referred to achieve these kind of requirements.

Please refer to the attached snips for your reference. 
Note: In the snips - Service field is a reference field on INCIDENT Table.

UI Policy: 

KoteswaraVM_0-1738567328439.png


UI Policy Action:

KoteswaraVM_1-1738567379075.png




If you want to achieve the requirement through client script, you can refer to the below script

 

Client Script Type: Onload

 

 //Type appropriate comment here, and begin script below
    g_form.setReadOnly('business_service', false);
    var service = g_form.getValue('business_service');
    if (service === "" || service === null)
        g_form.setReadOnly('business_service', true);

 

 

 

I hope this information helps you. If my response helped, please mark my response as Helpful.


Thanks,
Koteswara Vara Prasad M