No function should be needed for that.
With Contact::ID = Account::MaincontactID
You can simply refer to Contact::FirstName from any layout based on Account and you'll access the first name for the contact record that's linked to the current account record on that layout. You can even put the FirstName field from Contact directly on this layout if all you need is to display the name of the contact for the current account.
Thanks Phil! I forgot to mentioned that I wanted to use the custom functin on the layout that has no relationship assigned to the account nor contact.
Would it still be possible to write a custom function ?
Why not just add the relationship?
I don't think a custom function would work very well for this since without an established relationship, you'd need to perform a find to access the correct record. I think we discussed such a scripted report on another thread if memory serves correct.
I was told once that if you have many relationship built the performance will decrease so I thought of creating a custom function to handle the relationship...
Is that true though?
I don't think the total number of relationships in your file will significantly affect performance.
The complexity of your layout--which could require that many relationships be used for the layout (not the total in your file) might be a factor. (Summary fields, unstored calculations, conditional formatting and some popup menus with conditional value lists are all factors that can slow system responsiveness.)
But using an alternative approach will either be much slower or will update in response to changes much less frequently as well as requiring a greater complexity in the design of your system.