The CreatorCon Call for Content is officially open! Get started here.

How to access parent object within "query from" of relationship

Subhrajit2
Giga Contributor

I am trying to create a scripted defined related list on a table which will display records from different archive tables based on a field value on the parent table. I am doing something like this.

 

Apply to: a particular table
sample statement: answer = "target_table";

Query from: Scripted
Sample statement:

var table = "ar_" + parent.u_select_table.toString();//u_select_table is a field on the "target_table" which contains a table name
answer = new TableUtils(table).tableExists() == true?table:false;

 

What I am suspecting is that the parent object is not accessible within "Query from" script. So, is there a way I can populate the "query from" table dynamically based on current parent record's field value?

5 REPLIES 5

snowenthusiast
Tera Contributor

Hi Subhrajit,

Were you able to accomplish this? I have the exact same requirement.

I have a need to dynamically select "query from" table based in choice selection on "parent" record.

Please let me know.

Thanks.

I am still looking for a solution and will update this post if I find anything. However, please feel free to share if you find a solution.

Regards,

Subhrajit

I have found a solution to the issue. Please try and let me know if it helps.

Subhrajit2
Giga Contributor

I have finally found a solution to this. Here is the detail.

Step 1: Create/modify an on-display business rule on target table <target_table> where you want the related list to load by storing the current record's table name to a session variable.

function putSessionData() {
var session = gs.getSession();
session.putClientData('sourceTable',current.u_select_table);//u_select_table is a field which contains the table name
}

 

Step 2: Modify the "Apply to" and "Query from" scripts of the scripted relationship as below

Apply to:

answer = table_name == "<target_table>"?true:false;//Ensure the related list appears only on the target_table.

 

Query from:

var session = gs.getSession();
answer = session.getClientData('sourceTable');//Based on target_table's u_select_table field which contains a table name

To limit which records to fetch from the source table, modify "Query with" script like below

(function refineQuery(current, parent) {

	// Add your code here, such as current.addQuery(field, value);
	var queryString = "<encoded_query>";
	current.addQuery('sys_id','IN', queryString);//current is the source table, parent is the target table
})(current, parent);

 

As the on-display BR will run before the related list is loaded, this solution will work and using the session variable will ensure it applies to current user session only.

 

Please  Mark as Correct Answer if it helps.