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
indepUserindepUser 

Get a list of contacts associated with an opportunity in javascript

Hi,

 

I am building a custom button on the opportunity page which needs to get all the contacts associated with that opportunity. And then I need to get the contact details for this contact. How can I do this? I need to do this using Javascript, as I don't have s-control available on my edition of salesforce.

 

Appreicate your help.

 

Thanks!

 

SivarajanSivarajan

Hi,

 

Is it like that fetching all the contacts of Account that associated with Opportunity or Do you have any lookup of contact in opportunity?

indepUserindepUser

Sorry for incomplete information. I have a related list of contacts in the opportunity. And yes, I want to get a list of all contacts related to the opportunity. And I want to extract information from the contacts like their name, email etc.

 

Thanks!

SivarajanSivarajan

It's possible by creating a Visualforce page and attach that page to that custom button. 

 

Visualforce page:

 

<apex:page standardController="Opportunity" extensions = "OppRelatedContacts">

    <apex:form>

      <apex:pageblock title="Contacts related with {!Opportunity.Name}">

               <apex:pageblocktable value="{!contacts}">

                  <apex:column value="{!contacts.Name}">

                  <apex:column value="{!contacts.Email}">

               </apex:pageblocktable>

      </ apex:pageblock>

   </apex:form>

</apex:page>

 

 Controller:

 

 public class OppRelatedContacts

{

   private Opportunity opp; 
   public List<contact> contacts{get;set;}

   contacts = new List<contact>();


   public OppRelatedContacts(ApexPages.StandardController stdctrl)
   {
     opp = (Opportunity)stdctrl.getRecord();
     this.contacts= [select Name,Email from Contact where Opportunity__c =: opp.id]; 

   }

 

 

 

 

JuneBugJuneBug

Is there a reason you were asking about using JavaScript?  Are you needing to do something with the data client-side for some reason?  If you're just trying to display the data, using VisualForce is the correct path.  If there is truly some reason you need it client-side, you can get to it in JavaScript behind a button using the following:

 

 

{!REQUIRESCRIPT("/soap/ajax/18.0/connection.js")}

 

var qResult = sforce.connection.query("Select Name,Email from Contact where Opportunity__c ='{!Opportunity.Id}'");

if (qResult.size > 0)

{

   var records = qResult.getArray('records');

 

     //YOU COULD START A LOOP HERE AND WALK THROUGH ALL CONTACTS

     var ContactName = records[0].Name;

     var ContactEmail = records[0].Email;

}

venchinn2venchinn2
Hi Siva_org,
In this post i tried the apex class its throwing a error

No such column 'Opportunity__c' on entity 'Contact'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names. at line 10 column 21

Please let me know about this

Thanks.