function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
JayInSAJayInSA 

Contact to Contact relationship

Sorry....probably a pretty basic question for most of you out there, but I am still learning.

 

I am setting up a small database for enrollment at my school.  I have setup the parents names as the "Account" and the children as the "Contacts" that are then tied to that account.

 

It would be helpful to me if I could identify siblings directly on the Contacts page.  In other words, I would like to establish a lookup field on the Contact layout page so that I could easily that John Doe has 1 sister and 1 brother.  However, when I try to do this it setup in a parent-child relationship.

 

I would like to be able to do the lookup, select the sibling record and then have it automatically update the siblings record as well.  So it wouldn't be a parent-child relationship but rather a daughter-daughter relationship.

 

Any ideas?

 

Hope this makes sense.

Pradeep_NavatarPradeep_Navatar

Your business requirement is perfect but if you are trying to achieve this by just creating a lookup on the contact, it won't work. You need to write a trigger to achieve this functionality. Whenever you create a relationship, trigger will update it in the other record and same relationship will be created with both the contacts. However there is a limitation, you can only link two contacts (1 self +1 other) in this manner. For an example : In case you want to create relationship with two conatcts (1 self + 2 others), create two lookups say sibling1 and sibling2.

 

Hope this helps.

tantotanto

Looks like you are a non-profit. Are you using the Non-Profit Starter Pack from Salesforce?

mh218mh218

Take a look at these - involves some visualforce but not over the top in terms of complexity:

 

Sam Arjmandi's Blog Post

 

Salesfiorce Developer Guide

 

Salesforce Relationship Documentation

 

Off the top of my head your query would look something like this:

 

SELECT Account.Name, (SELECT Contact.FirstName, Contact.LastName FROM Account.Contacts) FROM Account

 

This will list the First Name and Last Name from the account of the current contact record.  If you have other fields you want included, make sure they are in your SELECT statement - i.e. if you have a graduation date field or a homeroom or guidance counselor identified on the contact.

 

Happy to help more if you need.

 

mh218mh218

OK, so I had a bit of free time on my hands - here's some visualforce code to get you started:

 

The page (which you then put in your contact page layout)

 

<apex:page standardController="Contact" extensions="siblingController">
  <apex:form >
    <apex:pageMessages />

      
    <apex:pageblock id="CustomList"  title="Siblings" >
     <apex:pageBlockTable value="{!sibs}" var="s" rendered="{!NOT(ISNULL(sibs))}">
       <apex:column value="{!s.Name}"/>
       <apex:column value="{!s.Age__c}"/>
       <apex:column>
       <apex:facet name="header"></apex:facet>
           <apex:commandLink >Link to {!s.FirstName}'s Contact Record
             <apex:param name="cid" value="{!s.id}"/>
           </apex:commandLink> 
       </apex:column>                
     </apex:pageBlockTable>   
     
     <apex:outputLabel value="No records to display" rendered="{!(ISNULL(sibs))}" styleClass="noRowsHeader"></apex:outputLabel>
     
     </apex:pageblock>
   </apex:form>
</apex:page>

 

 

And the controller:

 

public class siblingController {
    private List<Contact> sibs;
    private Account acct; Contact cntact;
    public siblingController(ApexPages.StandardController controller)
         {
                this.cntact= (Contact)controller.getRecord();
         }      

         public List<Contact> getSibs()
         {
                Contact con = [Select id, Account.id FROM Contact
                 WHERE id = :cntact.id];

                 if (con.Account == null)
                         return null;
                            
            sibs = [Select id, FirstName, Name, 
                           Age__c, Account.id, 
                           Account.Name

                    FROM Contact
                    WHERE Account.id = :con.Account.id];
                                         return sibs;      }
             }

 

If you have additional fields you want to include you need to add them to the controller where it defines sibs and then add the columns in the page accordingly.  In this case I'm referencing a custom Age field (Age__c)  as well as the standard Name.

 

 

Hope this helps.