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
DY24DY24 

Auto Populating lookup field

I have a lookup field to the Contacts object on Opportunities object which is to define the Primary Point of Contact (dont want to use Contact Roles). There is another lookup field on that Contact record page which is supposed to be a lookup back to the Opportunity.

When a Lead gets converted to an Opportunity, I want my sales rep to use the lookup field (Primary Point of Contact) to select the contact. From there, I would like to have the Opportunity lookup field on the Contact Record page to be populated.

How can I configure this?
Waqar Hussain SFWaqar Hussain SF
This requirement can be accomplish using apex trigger on Contact role/Opportuntiy. When a new primary contact role/opportunity is created the trigger will run and will update the primary contact field on opportunity. 

See opportuntiy trigger example
trigger CopyPrimaryContact on Opportunity (before update) {

// THIS TRIGGER WILL OVERWRITE ANY CONTACT DEFINED IN THE CUSTOM FIELD OPPORTUNITY_CONTACT__C ON THE OPPORTUNITY OBJECT.
// SET THIS FIELD TO READ ONLY OR CHANGE THE FUNCTIONALITY BELOW TO AVIOD DATA BEEING OVERWRITTEN BY MISTAKE...


   List<OpportunityContactRole> contactRoleArray = [select ContactID, isPrimary from OpportunityContactRole where Role != 'Dealer Sales Rep' and OpportunityId IN :Trigger.newMap.keySet() ORDER BY isPrimary DESC, createdDate];

   for (Opportunity o : Trigger.new) {

       // CREATE ARRAY OF ALL CONTACT ROLES ON THIS OPPORTUNITY. THE REASON WHY WE DONT PICK THE PRIMARY CONTACT ROLE ONLY 
       // IS BECAUSE THE PRIMARY FLAG IS NOT SET WHEN LEADS ARE CONVERTED TO OPPORTUNITIES. ONLY WHEN CREATING OPPORTUNITIES
       // MANUALLY FROM THE CONTACT OBJECT THE IS PRIMARY CHECKBOX IS CHECKED...

       if (o.Opportunity_Contact__c == null && contactRoleArray.size() > 0) {

           // IF PRIMARY IS DEFINED THEN THIS WILL BE THE FIRST OBJECT. IF NOT THE FIRST ADDED CONTACT ROLE WILL BE ADDED...
           o.Opportunity_contact__c = contactRoleArray[0].ContactID;

       }
   }
 }

 
DY24DY24
@waqar Thanks for your answer. I actually have a Flow and Process builder set up where when the lookup field on the Opportunity page gets populated with the Primary Point of Contact, It automatically gets added to the Contact Roles list. The issue is that When I go back to the contact page for that individual, I want the custom Opportunity lookup to populate with the respective record. I know there will be an issue if the Contact is related to more than one opportunity and thats why we have the related list but the objective is to have the relationship established so that I can ultimately create a list view on the contacts page with the opportunity info as a column and the opportunity stage and close date. This way, i can run a mass email to all Opps closed within past 30 days. Hope that makes sense.