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
NaypersMclgxNaypersMclgx 

To add an Account column in a Oportunnity table (to see who owns the opp)

Hi, I hope somebody can help me.

 

I need to add an account column in a Oportunnity table, I'm  using a pageBlockTable that receive a list (List<Opportunity>), and I'm using a Controller.

 

I have this in my controller...

 

// CONTROLLER

 public Opportunity[] getMyObjectOpportunities() {
        
        String sortFullExp = sortExpression  + ' ' + sortDirection;
        List<Opportunity> itemsOpportunities;

            String sqlFilter = '';
                
            sqlFilter += ' SELECT Name, ';
            sqlFilter += ' stageName, '; 
            sqlFilter += ' closeDate, '; 
            sqlFilter += ' Id, ';  
            sqlFilter += ' Probability, ';  
            sqlFilter += ' Amount '; 
            sqlFilter += ' FROM Opportunity WHERE CreatedById = \'' + getUserId() + '\'';
            sqlFilter += ' ORDER BY ' + sortFullExp + ' LIMIT 1000';

            itemsOpportunities = DataBase.query(sqlFilter);

        return itemsOpportunities ;
}

 

Thak you.

Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

Hi,

 

Add ‘account.name’ field in your query for displaying the account name in page block table.

Like 

sqlFilter += ' SELECT Name, ';
sqlFilter += ' stageName, ';
sqlFilter += ' closeDate, ';
sqlFilter += ' Id, ';
sqlFilter += ' Account.name, ';
sqlFilter += ' Probability, ';
sqlFilter += ' Amount ';
sqlFilter += ' FROM Opportunity WHERE CreatedById = \'' + getUserId() + '\'';
sqlFilter += ' ORDER BY ' + sortFullExp + ' LIMIT 1000';

 

 

Use the below code snippet as reference:

--- Vf page-------------

<apex:page controller="accountwithopp" >

 <apex:pageBlock >

 <apex:pageBlockTable value="{!opp}" var="p">

 <apex:column >{!p.id}</Apex:column>

  <apex:column >{!p.account.name}</Apex:column>

 

 

 </apex:pageBlockTable>

 </apex:pageBlock>

</apex:page>

 

------------- apex controller ------------------

 

public class accountwithopp {

public list<Opportunity > opp{get;set;}

public accountwithopp ()

{

    opp=[select id, account.name from Opportunity  limit 1000];

}

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

 

All Answers

Navatar_DbSupNavatar_DbSup

Hi,

 

Add ‘account.name’ field in your query for displaying the account name in page block table.

Like 

sqlFilter += ' SELECT Name, ';
sqlFilter += ' stageName, ';
sqlFilter += ' closeDate, ';
sqlFilter += ' Id, ';
sqlFilter += ' Account.name, ';
sqlFilter += ' Probability, ';
sqlFilter += ' Amount ';
sqlFilter += ' FROM Opportunity WHERE CreatedById = \'' + getUserId() + '\'';
sqlFilter += ' ORDER BY ' + sortFullExp + ' LIMIT 1000';

 

 

Use the below code snippet as reference:

--- Vf page-------------

<apex:page controller="accountwithopp" >

 <apex:pageBlock >

 <apex:pageBlockTable value="{!opp}" var="p">

 <apex:column >{!p.id}</Apex:column>

  <apex:column >{!p.account.name}</Apex:column>

 

 

 </apex:pageBlockTable>

 </apex:pageBlock>

</apex:page>

 

------------- apex controller ------------------

 

public class accountwithopp {

public list<Opportunity > opp{get;set;}

public accountwithopp ()

{

    opp=[select id, account.name from Opportunity  limit 1000];

}

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

 

This was selected as the best answer
NaypersMclgxNaypersMclgx

 

Yes perfect!!!, thank you very much...