How to make field read-only if the parent record has child record.

mark141230
Tera Expert

Hi Everyone,

Badly needed your help. I have this requirement where I need to make the customize field I create "u_actual_cashflow"

to become read-only in the expense line if the parent expense line has a child expense line.

 

I have this script constructed, but when it didn't work, I found out that GlideRecord should not be used in client script.

 

function onLoad(){
var rec = new GlideRecord('fm_expense_line');
var syst = g_form.getValue('number');
rec.addQuery(syst, 'parent');
rec.query();
if (rec.next()){
g_form.setReadOnly('u_actual_cashflow', true);
}
else {
g_form.setReadOnly('u_actual_cashflow', false);
}
}

 

Hope you can help me, Thanks in Advance

1 ACCEPTED SOLUTION

Mustafa Hussain
ServiceNow Employee
ServiceNow Employee

You should use a Display Business Rule which runs the glide record and stores the value in the scratchpad. Then you can use the same scratchpad value in your client script to determine whether the field should be hidden or not

Display Business Rules 

Community Question related to this.

 

 

Business Rule:


Name: Custom - Is Parent
Table: fm_expense_line
When: display
Script:
(function executeRule(current, previous /*null when async*/) {
var gr = new GlideRecord('fm_expense_line');
gr.addQuery('parent',current.getUniqueValue());
gr.query();
if(gr.hasNext())
g_scratchpad.isParent = true;
else
g_scratchpad.isParent = false;

})(current, previous);

 

Client Script:



Name: Custom - Set Read Only
Type: onLoad
Table: Expense Line
Script:
function onLoad(){
  if (g_scratchpad.isParent)
    g_form.setReadOnly('u_actual_cashflow', true);
  else 
    g_form.setReadOnly('u_actual_cashflow', false);
}



 

View solution in original post

3 REPLIES 3

mamann
Mega Guru

GlideRecord really should not be used in a Client Script and you should look into using GlideAjax for this as it's more efficient.

You can find info on GlideAjax here.

https://docs.servicenow.com/bundle/madrid-application-development/page/app-store/dev_portal/API_refe...

 

As a note though, your code has an error at this line, due to your arguments being in the wrong order.

rec.addQuery(syst, 'parent');

Should be

rec.addQuery("parent", syst);

Mustafa Hussain
ServiceNow Employee
ServiceNow Employee

You should use a Display Business Rule which runs the glide record and stores the value in the scratchpad. Then you can use the same scratchpad value in your client script to determine whether the field should be hidden or not

Display Business Rules 

Community Question related to this.

 

 

Business Rule:


Name: Custom - Is Parent
Table: fm_expense_line
When: display
Script:
(function executeRule(current, previous /*null when async*/) {
var gr = new GlideRecord('fm_expense_line');
gr.addQuery('parent',current.getUniqueValue());
gr.query();
if(gr.hasNext())
g_scratchpad.isParent = true;
else
g_scratchpad.isParent = false;

})(current, previous);

 

Client Script:



Name: Custom - Set Read Only
Type: onLoad
Table: Expense Line
Script:
function onLoad(){
  if (g_scratchpad.isParent)
    g_form.setReadOnly('u_actual_cashflow', true);
  else 
    g_form.setReadOnly('u_actual_cashflow', false);
}



 

Hi @Mustafa Hussain 

I have a akin requirement but the twist here is.
If the phase field on project is "initiating" then planned start and end date on project task should be editable however if project phase is "executing" or "closing" then planned start and end date on project task should be read only.

Can you help me on this?