Custom Relationship (Defined Related List)

Kalaiarasan Pus
Giga Sage

I am trying to created a custom related list (Relationship menu) and able to setup the fields, script etc.

I am trying to not show the related list if it has no records. So I have set the 'Omit if empty' to true on list control.

Below is the format of my related list query script currently.

if(validData)

{

addquery to show the required data

}

If I am leaving the else part blank, it is end up pulling all the records from the table.

How would I handle the else part to return no data other than adding a invalid query? Is there a elegant way to do this?

11 REPLIES 11

Sorry for the delay. It was a bank holiday here yesterday.



You may be best off with using an Advanced relationship a bit like Audit History (/sys_relationship.do?sys_id=366eda7b0a0004970a7d925c9258806b) and Audit Records (/sys_relationship.do?sys_id=702782ce0a0a0b1b007514c6daa62dd5).



For example, let's say I have a related list that I only want to apply to Incident and Problem, then the script I would use is


answer = (table_name == "incident" || table_name == "problem")



table_name would be a reference to the form that I'm trying to put this related list onto, so answer would evaluate to true and show if I'm looking at a form where it's table is incident or problem. You could work your logic of checking the table name here. Have a look the above examples and see if that works for you.



I have to be honest here. I've not strayed much into the territory of the advanced scripted relationships. If this doesn't work out for you then you may be stuck with that workaround of setting an invalid sys_id.



In hindsight, as has been mentioned previously, the relationships are really for querying and filtering from other tables (e.g. sys_attachment) based on the record you are looking at so that some records are returned. You may want to look at the requirement and see if it is possible to simplify the implementation. For example, if it's just a list of records to show and not interact with, perhaps you can put this into a UI Macro and add it to the form as a formatter.



Sorry I couldn't be of more help


OK. I just tried it and that script field is limited and you don't get access to the parent object, which is disappointing. [Sad face]



But, I noticed something interesting. You can make your sys_id query a lot more elegant by using an array. For example:


var ids = [];


var gr = new GlideRecord("table_goes_here");


if (gr.get(parent.parent)) { // whatever condition or loop you want


ids.push(gr.getUniqueValue()); //build up the list of sys_ids


}


current.addQuery("sys_id", "IN", ids);



(It's a silly script, I know)



I played around with it between incident and problem on my demo (though struggled with matching your scenario) and found that, if the array is empty, no results are returned. Then with the   "omit if empty" option is checked it will hide when there are no results.



This logic resembles that of creating an advanced reference qualifier. I guess you will need to rework the logic of your relationship script and build up the query this way.