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
Ganesh BollinaGanesh Bollina 

How to fetch account details based on opportunity id?

I have a button on an opportunity, and able to fetch the id. so now based on this opportunity id, how can i fetch the account details associated to this opportunity.
Amit Chaudhary 8Amit Chaudhary 8
Please try below example.
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}
var qr = sforce.connection.query("SELECT Id, SyncedQuoteId , account.Name FROM Opportunity where Id='" + "{!Opportunity.Id}" + "'");
var records = qr.getArray("records"); 
// display the field from the query
alert("account.Name: " + qr.records.account.Name);
I hope this will help you
 
Ganesh BollinaGanesh Bollina
Hi Amit,
 On click of the button I am rendering a visualforce page.
window.location.href='/apex/VFP_OneButtonApp?id={!Opportunity.Id}'

And in my controller i am fetching this Id.
So in my controller based on this opportunity.id, I need to fetch the associated account.
Dhanya NDhanya N
Hi Ganesh,

In controller after fetching Opportunity Id, Use this query to fetch the associated account.
 
Select Id, account.name from Opportunity where Id =: OpportunityId ;

Thanks,
Dhanya
Amit Chaudhary 8Amit Chaudhary 8
Please try to update your Controller like below.
public class myControllerExtension 
{

    private final Opportunity opp;
    public myControllerExtension(ApexPages.StandardController stdController) 
	{
        this.opp = (Opportunity)stdController.getRecord();
		if( opp.id != null)
		{
			this.opp= [Select Id, account.name from Opportunity where Id =: opp.id ] ;
		}
    }
}

Let us know if this will help you