I've got a List in Sharepoint 2010 and I've created a "related item" which has a link to my master list which is set to unique values (so for each item in my master list there is 0...1 child items).

I have got a standard "Related item View" section added to my DispForm.aspx which shows the related item and allows me to click it.

What I would like to do is replace the Related Item View with a button and then add a javascript function which would either open the related item OR would open the "NewForm.aspx" and pass the appropriate ID.

As far as I can tell what I need is some way of retrieving the Id of the related item, with that I can call "DispForm.aspx?ID="+id and if there is no Id then call "NewForm.aspx?MasterId="+MasterID.

The focus of the article is that if the user has already taken the survey, they're taken to the existing one to read it; if they haven't taken it, then I pass the Survey name into the NewForm as a querystring.

Let me know if you get stuck, or I'm missing that makes this example not quite fit for you.