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
Andrew Wilson 30Andrew Wilson 30 

formula field to return contact name

I would like to create a formula field on the oportunity that looks up the "FirstName" of the related contact. To complicate matters we may have more than one contact related to an opportunity, so I want to get the primary contact, which may be assigned by another field called "PrimaryContact__c". 

Is this possible with a formula field? Do I need to write a soql query to accomplish this? What's the best way to make this work?

Thanks in advance for your help!

RKSalesforceRKSalesforce
Hi Andrew,

Please find Below My Analysis of your requirement:
This requirement can be achieved by using Triggers. One Trigger on Opportunity and One on Contact.
Trigger on Opportunity (Before Insert , Before Update):
Get AccountId of opportunity --> Query contact where AccountId in Opprtunity.AccountId And Primary = True ---> assign this contact.FirstName to NAme field.

Trigger On Contact(After Insert, after Update, before delete) 
Why we need this trigger? Ans : At the time of creation there may or May not be any primary Contact.
get AccountId if it is Primary Contact --> Query opportunities where AccountId = PrimaryContact.AccountId --> Assign Opportunity.ContactName = Contact.FirstName --> update Opportunities

Hope this will help you. Please let me know if helped by marking best answer.

Regards,
Ramakant
{!pramod_nishane}{!pramod_nishane}
Hi Andrew,
As per my analysis your requirement is not possible with a formula field.

You can go for Process Builder and can acheive your requirement. 
Here is the two options for your requirement.

A) If you dont want to show primary Contact related field on Opportunity page then you can acheive the same usingOpportunity Contact Role-
     There you can have multiple Contact to same Opportunity and can select only one as Primary Contact for that Opportunity.
    
B) But if you want o show primary Contact related field on Opportunity page then below is the solution using Process Builder -

     1) Created three (3) custom fields as - 
          a) Create Lookup of Opportunity (Opportunity__c) on Contact.
          b) Create Checkbox 'Is_Primary__c' on Contact.
          c) Create Text field 'Primary_Contact_Name__c' on Opportunity.

    2) Create Process Builder on Contact Object which will be fire when records get created and edited.

 User-added image
   
   3) Decision Block - here we will check for 'Is_Primary__c' is changed to True and  Opportunity should be associated with Contact.
       
User-added image


    4) We will have two (2) 'immidiate action'
       a) Update 'Primary_Contact_Name__c' on Opportunity with Contact's FirstName.
       
User-added image

       b) Update other Contact's (Contact Siblings) associated  with same opportunity.
           For example :- Consider  we have Contact 'C1' with 'Is_Primary__c' is 'True' and we assign Opportunity 'Opp1' to 'C1' in that case our                                             process builder will work and update FirstName of  C1 to Opp1 in 'Primary_Contact_Name__c' field.
                                     Now create new Contact 'C2' and mark 'Is_Primary__c'  to 'True' and assign Opportunity 'Opp1' to 'C2' then our process                                         builder will update FirstName of  C2 to Opp1 in 'Primary_Contact_Name__c' field and what about Contact 'C1'                                                         'Is_Primary__c' field?
                                     In that case 'Is_Primary__c' field of  'C1' should be set to 'False' because 'C2' is now primary contact for 'Opp1'.
                                     Here is that second update immidiate action for the same.

User-added image

Let me know in case of any concerns.

Please mark this answer as the solution/ best answer if it solves your purpose so that it can help other community members.

Thanks,
Pramod Nishane
Salesforce Consultant
Varasi LLC
www.varasi.com