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
svernonsvernon 

Javascript custom button

I"m trying to create a custom button on a case (attached to an opportunity), to add over the contact roles which are associated to that oppty to the case. Here's the code I have but I'm not sure how to amend it to work for all contact roles and not just the primary.

 

{!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();
Hey_YouHey_You

Have you tried removing this portion of the query in the doQuery function:

and IsPrimary = true"