0

Salute.

I'm trying to add to the related contact list page an indication of the role of each indirect contact. In the standard page "Related Contacts" this field is, but how make it in custom - I don't understand.

Apparently, through an appeal to Account Contact Relationship? How to add it to a SOQL query?

public List<Contact> Contacts { get{  
  return [SELECT Id, Name, Account.Name, Title, Email, Phone, Relations__c FROM 
  Contact LIMIT 1000];  
  } set;}  

--

<apex:pageBlock title="Related Contacts"> 

   <apex:pageBlockButtons location="both">
     <apex:form >
        <apex:commandbutton action="{!URLFOR($Action.AccountContactRelation.AddRelation)}" value="New Related Contact"/>
     </apex:form>
   </apex:pageBlockButtons>


  <apex:pageBlockTable value="{!Contacts}" var="c">  

      <apex:column headerValue="Name">
      <apex:outputLink value="/{!c.Id}">{!c.name}</apex:outputLink>
      </apex:column>  
      <apex:column value="{!c.Account.Name}"/>   
      <apex:column value="{!c.Title}"/> 
      <apex:column value="{!c.Email}"/>  
      <apex:column value="{!c.Phone}"/> 
      <apex:column value="{!c.Relations__c}"/>

  </apex:pageBlockTable>   
</apex:pageBlock>   

enter image description here

3
  • Hello, Welcome to SFSE, It's not clear what you are trying to achieve. Do you want to display all indirect contacts on the record? Commented Oct 6, 2019 at 11:17
  • @rahulgawale no, i'm already have a related contact page, but i don't now how to fill "Roles" column in that page. Roles is not a Contact field, so when i try get [SELECT Roles, ... FROM Contact LIMIT 1000] - this leads to an incorrect field error. I also tried SELECT AccountContactRelation.Roles - with the same success. I added a screenshot to make it clearer.
    – anabiozzze
    Commented Oct 6, 2019 at 12:04
  • It's like in standart Related Contacts page you having fields like "Influence agent", "Project Manager" to indirect contacts. I need it to my custom page.=)
    – anabiozzze
    Commented Oct 6, 2019 at 12:14

2 Answers 2

1

The schema you are dealing with is:

Contact -> AccountContactRelation -< AccountId

where AccountContactRelation has a field Role

or

Contact -> AccountContactRole -< Account

where AccountContactRole has a field Roles (multi-picklist)

Since you are trying to display a list of Contacts and their roles on a single line of VF , you are gong to need a wrapper class in your controller. I'm assuming the first example schema. Same principle applies to second example schema

public class ContactWithRoles { // wrapper class
  public Contact contact {get; private set;}
  public ContactWithRoles (Contact c) {this.contact = c;}

  public String getRoles() { // concat 0+ roles into string
     String[] result = new List<String>();
     for (AccountContactRelation acr: contact.AccountContactRelations) {
        result.add(acr.Role);
     }
     return String.join(result,';'); 
  }
}

public ContactWithRoles[] contactsWithRoles { // VF page refers to this collection in pageBlockTable
  get {
     if (contactsWithRoles == null) {  // lazy load
        contactsWithRoles = new List<ContactWithRoles>();
        for (Contact c =  [SELECT Id, Name, Account.Name, Title, Email, Phone,
                             (SELECT Role FROM AccountContactRelations) // relationship query
                            FROM Contact LIMIT 1000]) {
            contactsWithRoles.add(c);
     }
     return contactsWithRoles;
  } private set;
}

VF markup

 <apex:pageBlockTable value="{!contactsWithRoles}" var="cwr">  

   <apex:column headerValue="Name">
     <apex:outputLink value="/{!cwr.contact.Id}">{!cwr.contact.Name}</apex:outputLink>
    </apex:column>  
    <apex:column value="{!cwr.contact.Account.Name}"/>   
    ...
    <apex:column value="{!cwr.roles}"/>

2
  • Thank! After several refills, the APEX code worked. However, VF generates the error "Invalid field roles for SObject Contact" on the field "!cwr.contact.roles".
    – anabiozzze
    Commented Oct 7, 2019 at 21:02
  • oops - fixed; cwr.roles is what you need
    – cropredy
    Commented Oct 7, 2019 at 21:05
0

This SOQL query works for me to get Contacts and Roles

SELECT Id, Name, Account.Name, (SELECT Roles FROM AccountContactRelations), Email FROM Contact where accountid='<some account id'

1
  • 1
    Welcome to Salesforce Stack Exchange (SFSE). Your contribution is welcome. However, while your answer may technically answer the question in the title, it does not fully address the OP's actual question as detailed - which is, essentially, how to display the Role of the related contact on a VF page. The query you posted is very close to the one found inside the other answer - which is much more detailed regarding actually answering the full original questoin. And that answer was accepted more than two years ago, so I am unsure why you chose to add this answer now.
    – Moonpie
    Commented Feb 10, 2022 at 23:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .