How to create an automatic relation based on a number of different criteria

I’ve moved this question to this thread here for neatness reasons