How to Check if there is a record in a related list in Expense Line

mark141230
Tera Expert

Hi Everyone,

 

Seeking your assistance please, I have this requirement in which I need to set the Actual Cashflow field to read-only in the expense line if there is an associated or child expense line created.

 

I am trying to use client script but to no avail, not that good in scripting,

 

Please see script:

 


var gr = new GlideRecord('fm_expense_line'); // write your table name


gr.addQuery('sys_id',parent); // write appropriate query


gr.query();


if(gr.next()){


g_form.setReadOnly('u_actual_cashflow', true);


}

 

I use the "parent" field as a reference, if the sys_id match any record with parent information the same as the sys_id or the expense line that i am looking at, it will set to read-only the actual cashflow field.

Hope its clear.

 

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

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Mark,

So onload you want to check whether for this record any expense line record is present or not in the related list?

function onLoad(){

var gr = new GlideRecord('fm_expense_line');
gr.addQuery('parent',g_form.getUniqueValue()); // give the fieldName in fm_expense_line table which refers to this table; I believe parent is the field name
gr.query();
if(gr.next()){
g_form.setReadOnly('u_actual_cashflow', true);
}

}

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Ashutosh Munot1
Kilo Patron
Kilo Patron

HI,


You can use Client Script and Script Include as below:

CLIENT SCRIPT:

var ga = new GlideAjax('expenseUtils');
ga.addParam('sysparm_name', 'checkLine');
ga.addParam('sysparm_sysId',g_form.getUniqueValue());//newValue is assigned to
ga.getXML(HelloWorldParse); function HelloWorldParse(response) { var answer = response.responseXML.documentElement.getAttribute("answer"); if(answer)
g_form.setReadOnly('u_actual_cashflow', true);
else
g_form.setReadOnly('u_actual_cashflow', false);
}

SCRIPT INCLUDE:Client Callable

var id = this.getParameter('sysparm_sysId');

var gr = new GlideRecord('fm_expense_line');
gr.addQuery('parent',id); // give the fieldName in fm_expense_line table which refers to this table; I believe parent is the field name
gr.query();
if(gr.next()){
return true;
}else{

return false;

}


Thanks,
Ashutosh

 

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);
}