Dynamic related lists on forms

david_rosalia
Mega Contributor

This is a bit of a hack but it offers a great flexibility and potential.

This will enable you to have the related lists at the bottom of your forms be dependant on data present on the form.

Step 1:
Create a relationship record and select the advanced option.
Enter the form on which you want the related list to appear in the "Apply to" field:
for example:
answer='incident';

Do the same for the "Query from" field:
for example:
answer='incident'; /*the table here doesn't matter as it will be replaced by the code*/

Note: The apply to and Query from fields are meant for returning a table name from a script. Unfortunately, the script cannot "see" the contents of the data on your form (neither "current" nor "parent" is known from these fields or at least not in my tests) so the step 3 blow overrides this limitation.

Enter whatevery query criteria you want in the "Query with" field. Here you can reference your form and related list fields:
I use a condition builder on my form and enter the query here to make it really flexible:
for example:
current.addEncodedQuery(parent.u_condition_builder);

Step 2:
Add the related list to the form which is referred to in the "Apply to field" above

Step 3 (the hack): Add a calculated field to the form.
In the script of the calculated field add the following code :

// This condition must exist for this to work because the condition is run 2X on display
// The first time the value is empty and makes it fail
if(current.u_table_selector != ''){ // this is the field on my form which contains the related list table name
var relate = new GlideRecord("sys_relationship");
relate.addQuery('name','related records'); // The second parameter is the name of the relationship from step 1
relate.query();
relate.next();
// the following will give (for example) answer='incident'; in the relationship record in the Query from field
relate.query_from = "answer='" + current.u_table_selector + "';";
relate.update(); // modify the relationship record
}

And that's it! Now your forms can have dynamic related lists. I do not recommend this solution but just wanted to show that it is possible.

5 REPLIES 5

maximo77
Tera Contributor

No it doesn't work and will create issues in your instance.

 

Before your 'calulated field' updates the relationship record, the form already rendered - and the current object is undefined. Because your script didn't populated query_from with your calculated table name yet

 

now the hack: if you have 'calculated' or other field in your base (parent) table, that holds the name of the table you want to query the record from, use that script to access that, in query_from section:

 

var id = gs.getSession().getUrlOnStack();
var re = new RegExp('^(.+?).do\\?sys_id=([0-9a-f]{32})','i');
//gs.addInfoMessage('sys_id ' + id.match(re)[2] + ' on table ' + id.match(re)[1]);

var gr = new GlideRecord(id.match(re)[1]);
gr.get(id.match(re)[2]);

answer=gr.table;

 

So that creates artifical access to the base record overcoming this query_from limitation by accessing session data of user who views the form (and avoids the race condition)