+ Start a Discussion
JordanMigJordanMig 

Auto-population of "primary" contact from Opportunity Contact Roles to custom object

Does anyone know how to auto-populate a contact lookup or e-mail field on a custom object with the "primary" contact from Contact Roles on Opportunities? I want a user to be able to create a new record on the custom object and have it automatically pull the contact information of the "primary" contact from the Opporunity so I can build workflow to send an e-mail notification. Thanks!
JCoppedgeJCoppedge
You would need to build an apex trigger for this, as contact roles is a related list and not a direct lookup.  You can vote for my idea here to create this functionality: http://ideas.salesforce.com/article/show/10089945/Primary_contact_lookup_on_opportunity
Hank ThompsonHank Thompson

I know this is old, but I thought I would chime in as I had a similar problem and just wrote a Javascript Button to fix it.  I needed to send an email to the primary contact of an opportunity.  Here's the code I ended up writing (I added in a custom button on the event record and then showed the button in the activity history related list of the opportunity page layout):

 

 

{!requireScript("/soap/ajax/19.0/connection.js")}

function getBaseURL() {
//This function will return the base part of the current window's URL without the trailing slash.
//Example Return Value: "https://na1.salesforce.com"

	var fullURL = window.parent.location.href;
	var slashPosition = fullURL.indexOf("/", 9);
	var baseURL;
	
	if (slashPosition < 0)
		baseURL = fullURL;
	else
		baseURL = fullURL.substr(0, slashPosition);
		
	return baseURL;
}

function doQuery() {
	var result = sforce.connection.query("select Id, OpportunityId, ContactId, Role, IsPrimary from OpportunityContactRole where OpportunityId = '{!opportunity.id}' and IsPrimary = true");
	var records = result.getArray("records");

	if (records.length > 0)
		window.parent.location.href = getBaseURL() + "/_ui/core/email/author/EmailAuthor?p2_lkid=" + records[0].ContactId + "&rtype=003&p3_lkid={!opportunity.id}&retURL=%2F{!opportunity.id}";
	else
		if (confirm("ERROR: There is no Primary Contact specified yet for this opportunity. Click OK to specify one now."))
			window.parent.location.href = getBaseURL() + "/p/opp/ContactRoleEditUi/e?oppid={!opportunity.id}&retURL=%2F{!opportunity.id}";
}

doQuery();

 

 

Hope that helps someone out there.

 

 - Hank

SpunkySpunky

Thank you for this code. It was more than tremendously helpful for me especially given how difficult it is to associate the primary contact on the opportunity to anything else on the opp.

 

I used your code to create a custom link on the opp and it works like a charm.

 

What I need though is to put this link into an html section of the opportunity layout similar to the link below. How would I reference this path? I need a clickable link that invokes the same behavior the custom link does.

 

<td><a href="https://na6.salesforce.com/_ui/core/email/author/EmailAuthor?p2_lkid=0018000000iZoyM&rtype=003&template_id=00X80000001aIAJ&p3_lkid=0068000000Y6wOU&retURL=%2F0068000000Y6wOU" target="_blank">Meeting_Recap_Letter </a></td>

 

 Any help would be appreciated.

Kristin FKristin F
@Hank Thompson, thank you for sharing your custom button.  Works like a charm!
Anthony Grimes 1Anthony Grimes 1
@Hank Thompson,

Thanks for this, but I get an Unexpected Token Else error when I try to use the button

User-added image

Any thoughts?