I had a question regarding using recursion to maybe check to see all the references that is connected to a reference from a particular table. For example if you look at this snippet shown below:
Notice that there is a column called “Related”. I wanted to try to pull out all the information that is tied with the record in that “Related” field that are reference types themselves. If you look at the snippet below:
This snippet shows the information which I click on one of the “Related” record and if you look closely there is a reference type called “Sender” and also “Recipient” and I basically want to pull that out and then subsequent references in those as well. I was thinking maybe I can used recursion to do this and maybe this will kind of be like a tree traversal where the references themselves are essentially a node and I would have to go inside each node and pull out the reference information then eventually I will reach my child node at the end of one of the links and then I would have to go to another direction to pull out the other reference information. I am not too sure if there is a way for me to detect whether an object is a reference type either so I am hitting a bit of a plateau right now and would appreciate it greatly if someone can assist me on this matter.
Thank You,
Tazwar Belal
Hi @Tazwar_Belal.
You can check for an empty reference by doing $Sender.id == 0
.
George
Thanks george, I was able to implement that successfully. Now with that part being solved if I detected a reference and I wanted to go to that particular reference, how would I go about doing that? Ideally I wanted to automate retrieving all the tie ins for that related column instead of having to manually go and find it as my documents gets more linked.
Tazwar Belal
Your idea to use recursion makes sense. You’ll have to tweak the following snippet, but the idea should roughly be:
def visit_record(record):
if record.id == 0: return
# do something with `record`
visit_record(record.Sender)
visit_record(record.Recipient)
visit_record($Related)
George
If I was assuming there to be more links between each of the records is there a way to specify the reference I want because I see that from this code you sent, you got the “record.Sender” inside the visit_record() function but if I did not know that there was a Sender reference is there a way to get that “record.Sender” to be sent as a parameter to the function because what I am picturing is this snippet below:
Notice how I can get to Related → Sender or Recipient and then I can get to the references tied to those two and so I want to be able to get A, B, C, D in a way.
Tazwar Belal
It should be possible with more recursive functions. Using your sketch as an example, you could do something like:
def visit_related(record):
if record.id == 0: return
# do something with `record`
visit_sender(record.Sender)
visit_recipient(record.Recipient)
def visit_sender(record):
if record.id == 0: return
# do something with `record`
# visit left and right child records (A and B)
def visit_recipient(record):
if record.id == 0: return
# do something with `record`
# visit left and right child records (C and D)
visit_related($Related)
You’ll need a recursive function for each table. Note that I assume Sender
and Recipient
reference different tables; if they reference the same table, you don’t need separate visit_sender
and visit_recipient
functions.
George
Is there a way to pull out the information from that $Related column. Typically I can just do this manually by doing $Related.something but if I wanted to pull out all the information and maybe store it into a list is that possible? That way I can maybe run a loop through it to see if there is a reference within that list and then keep going down the level.
Tazwar Belal