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
santhoshsanthosh 

How to list Contact Name using relationships from OpportunityContactRole object?

We would like to develop a ajax custom s-control to list contact roles, and respective activities in a different way than currently being listed in default related lists in Opportunity screen.  Though we were able to access the OpportunityContactRole object, we are not able to query Contact Name, Opportunity Name, and Role using relationship.
 
Can we achieve the above through a single query or we need to do multiple queries?  In case we have to multiple queries, what is the effecient way to do it?
DevAngelDevAngel
This might be exactly what you are looking for but should get you most of the way.

Select ContactId, Contact.FirstName, Contact.LastName, OpportunityId, Opportunity.Name, Role from OpportunityContactRole
santhoshsanthosh
Thanks for your prompt reply Dave.
 
Yes, I was looking for this query.  But I'm not able to move forward as I'm not able to display FirstName of Contact.  My code looks like this;
 
  
Code:
 //loop through the records and construct html string
     for (var i = 0; i < records.length; i++) {
          var contact = records[i];
          output += " " + contact.FirstName + "<br>";
     }

 
Above code does not work.  Can pls tell me as to how I can display contact information along with opportunity related data.
 
 
 
cheenathcheenath
Code looks good. What error are you getting?



santhoshsanthosh
Following is my query
Code:
sforce.connection.query("Select ContactId, Contact.FirstName, Contact.LastName, OpportunityId, Opportunity.Name, Role from OpportunityContactRole Where OpportunityId = '{!Opportunity.Id}'",

 
From above query if try to display ContactId, I get the result shown on the page.   Following is the code, which gives me the proper result.
Code:
function layoutResults(queryResult, source) {

     if (queryResult.size > 0) {
     var output = "";

     //get the records array
     var records = queryResult.getArray('records');

     //loop through the records and construct html string
     for (var i = 0; i < records.length; i++) {
          var contact = records[i];
          output += " " + contact.ContactId + "<br>";
     }

     //render the generated html string
     source.output.innerHTML = output;
     }
}

 
But if I try to display the FirstName, the get 'undefined' as result.  I just changed 'contact.ContactId' to 'contact.FirstName'.
 
 
cheenathcheenath
FirstName is in the parent object "Contact".

So to get FirstName you have to do:

contact.Contact.FirstName

I think you can call the top level object "opportunityContactRole" instead of  "contact". 





santhoshsanthosh
Thanks Manoj... I got it!