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
stollmeyerastollmeyera 

Trying to get AccountID from Opportunity VF Page

I am working on a VF page to display Account contacts in the Opportunity.  Within my class I have written a SOQL query to pull these contacts, but for some reason I can't reference the Opportunity.AccountID.  Within my query I am simply stating:

 

[select id, AccountID, FirstName, LastName, Phone, Email, Question_Answer__c, Role__c, Secondary_Role__c,Tertiary_Role__c, Create_Profile__c from Contact Where AccountID = :ApexPages.currentPage().getParameters().get('AccountID') ORDER BY LastName];

 

When I debug, the AccountID is passing as a null value.  

 

Any suggestions on how I can gather the Opportunity AccountID and use it within my select statement?

Best Answer chosen by Admin (Salesforce Developers) 
manuel-jose-condemanuel-jose-conde

the safest way is to query the fields you need on the opportunity (field accountId) in the constructor (or via an action method called in VF page as like it is in my example). Then you use that variable (myOpportunity.AccountId) in your query to get the contacts.

.

All Answers

manuel-jose-condemanuel-jose-conde

hi,

 

Is Opportunity the controller of the Visualforce page?

stollmeyerastollmeyera

Yes, I am using the Opportunity standard controller.  

manuel-jose-condemanuel-jose-conde

I would use an extension to the std controller to manage that.

 

Your page would look like this:

 

<apex:pageaction="{!refresh}" standardController="Opportunity"extensions="OpportunityExtensionController">

 

In the that class we would have something like this:

 

.

.

.

.

    

         

    public OpportunityExtensionController (ApexPages.StandardController stdController) {

        this.stdController = stdController;

        String id = stdController.getId();

        

        if(id == null || id == '')

        {

            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL,ERROR_ID_MISSING));

        }

        

    }

 

    public void refresh(){

 

        myOpportunity = [select id, AccountId, name                                

                        from Opportunity where id = :this.stdController.getId()]; 

    }

.

.

.

.

 

 

I hope it helps

stollmeyerastollmeyera

I actually am using an extension within my page.  It is below:

 

public class AccountContacts{

    public Account act {get; set;}
    private final Opportunity opp;
    
    public AccountContacts(ApexPages.StandardController myController) {
        opp=(Opportunity)myController.getrecord();
    }

    // Create the list for existing contacts
    Contact contact;
    List<Contact> contacts;
   
    
    public List<Contact> getContacts() {
        if (contacts == null) {
            contacts = new List<Contact>();
            contact = null;
            List<Contact> contacts2 = new List<Contact>();
            contacts2= [select id, AccountID, FirstName, LastName, Phone, Email, Question_Answer__c, Role__c, Secondary_Role__c,Tertiary_Role__c, Create_Profile__c
                        from Contact
                        Where AccountID = :ApexPages.currentPage().getParameters().get('AccountID')
                        ORDER BY LastName];
            for (Contact contact: contacts2) {
                contacts.add(contact);
            }
        }
        return contacts;
    }
}

 

The only issue is with the WHERE statement in my select query.  The 'AccountID' comes through as a null value...

taaataaa

Hi,

Check wether this work,

 

public class AccountContacts{

public string aId = ApexPages.CurrentPage().getParameters().get('id');

Public Opportunity oppty {set;get;}

public AccountContacts(ApexPages.StandardController myController) {

        oppty = new Opportunity();

          }  

public list<Contacts> getCon(){

        List<Contacts>cons = [select id, AccountID, FirstName, LastName, Phone, Email, Question_Answer__c, Role__c, Secondary_Role__c,Tertiary_Role__c, Create_Profile__c

WHERE AccountID =: aId ORDER BY CreatedDate DESC];

        return cons;

    }

   

    public void insertCon()

    {

               Oppty.Email=cons.Email;

        insert oppty;

        oppty = new Opportunity();

    }             

}

stollmeyerastollmeyera

@taaa, thanks for the input.  Unfortunately your controller is just pulling the ID of the standard object.  Because the oppty is my standard object, the below will simply just give me the oppty ID:

 

public string aId = ApexPages.CurrentPage().getParameters().get('id');

 Any other suggestions?  This seems so simple, yet so difficult considering it's a standard field on the opp...

manuel-jose-condemanuel-jose-conde

the safest way is to query the fields you need on the opportunity (field accountId) in the constructor (or via an action method called in VF page as like it is in my example). Then you use that variable (myOpportunity.AccountId) in your query to get the contacts.

.

This was selected as the best answer
stollmeyerastollmeyera

Unfortunately you are right, the only thing I could get working was a query...would rather have not used one, but at least its working!!  Thanks for all of the help,