You need to sign in to do that
Don't have an account?
Srinivas Nimmagadda
Now When I select Name only then its related Opportunities has to pop up in the right output panel window.
Eg:
-- if Name = Company_1 is written then
in the below-left output panel that Account column have to be displayed.
in the right output panel that Account related opportunities have to be displayed.
-- if Name is empty then
in the below-left page block all Accounts have to be displayed.
in the right page block, this output panel must not be displayed.
How to get List of related Opportunities when an Account is selected
I am trying to achieve this by using SOQL.
Apex Class
public class soql_example_6 { //accountName public String accName {set;get;} // account rating public String accRating {set;get;} // query String public String query {set;get;} public String query2 {set;get;} // Result fetched public List<Account> accounts {set;get;} public List<Opportunity> opps {set;get;} public SOQL_Example_6(){ accounts = new List<Account>(); opps = new List<Opportunity>(); } // This search is for Accounts public void search(){ // Default query query='select id ,name,phone,rating,industry from Account'; // if accName and accRating values are entered // where name='TCS' and Rating ='Hot' if(accName!=''&&accRating!=''){ query =query+' where name=\''+accName+'\' and rating=\''+accRating+'\''; }else{ // if only name is entered // where name='TCS' if(accName!=''){ query=query+' where name=\''+accName+'\''; } else if(accRating!=''){ // if only rating is entered // where rating='Hot' query=query+' where rating=\''+accRating+'\''; } } accounts =Database.query(query); } // This search is for related Opportunities public void search2(){ // Default Opportunity query query2='select id,name,Account, Amount, CloseDate, StageName from Opportunity'; // If Account Name is selected // Fetch that Account Name ID to this query if(accName!=''){ // -------------- // Getting an error here in the below line. query2 = query2+'where ID=:\''+accId+'\' '; } } }VisualForce Page
<apex:page controller="SOQL_Example_6"> <apex:form > <br/><br/> <apex:outputPanel style="width:1000px; height:40px;padding:10px;border:1px solid grey;margin-top:50px;"> Name : <apex:inputText value="{!accName}" /> Rating : <apex:inputText value="{!accRating}" /> <apex:commandButton value="Search" action="{!search}" reRender="fm"/> </apex:outputPanel> <br/><br/><br/> <apex:outputPanel id="fm"> <apex:pageBlock> <apex:pageBlockSection columns="2" rendered="true"> <apex:outputPanel title="Account Details"> <apex:dataTable value="{!accounts}" var="a" rules="rows" width="600" frame="border" cellpadding="10" rendered="{!accounts.size>0}"> <apex:column value="{!a.name}" headerValue="Name" /> <apex:column value="{!a.Phone}" headerValue="Phone" /> <apex:column value="{!a.Industry}" headerValue="Industry" /> <apex:column value="{!a.Rating}" headerValue="Rating" /> </apex:dataTable> </apex:outputPanel> <apex:outputPanel title="Opportunity Details"> <apex:dataTable value="{!opps}" var="o" rules="rows" width="600" frame="border" cellpadding="10" rendered="{!opps.size>0}"> <apex:column value="{!o.name}" headerValue="Name" /> <apex:column value="{!o.Amount}" headerValue="Amount" /> <apex:column value="{!o.CloseDate}" headerValue="CloseDate" /> <apex:column value="{!o.stageName}" headerValue="StageName" /> </apex:dataTable> </apex:outputPanel> </apex:pageBlockSection> </apex:pageBlock> </apex:outputPanel> </apex:form> </apex:page>
Now When I select Name only then its related Opportunities has to pop up in the right output panel window.
Eg:
-- if Name = Company_1 is written then
in the below-left output panel that Account column have to be displayed.
in the right output panel that Account related opportunities have to be displayed.
-- if Name is empty then
in the below-left page block all Accounts have to be displayed.
in the right page block, this output panel must not be displayed.
Try this code:
Vf Page:
<apex:page controller="SOQL_Example_6">
<apex:form >
<br/><br/>
<apex:outputPanel style="width:1000px; height:40px;padding:10px;border:1px solid grey;margin-top:50px;">
Name : <apex:inputText value="{!accName}" />
Rating : <apex:inputText value="{!accRating}" />
<apex:commandButton value="Search" action="{!search}" reRender="fm"/>
<apex:commandButton value="Search Opportunity" action="{!search2}" reRender="fm"/>
</apex:outputPanel>
<br/><br/><br/>
<apex:outputPanel id="fm">
<apex:pageBlock>
<apex:pageBlockSection columns="2" rendered="true">
<apex:outputPanel title="Account Details">
<apex:dataTable value="{!accounts}" var="a" rules="rows" width="600" frame="border" cellpadding="10" rendered="{!accounts.size>0}">
<apex:column value="{!a.name}" headerValue="Name" />
<apex:column value="{!a.Phone}" headerValue="Phone" />
<apex:column value="{!a.Industry}" headerValue="Industry" />
<apex:column value="{!a.Rating}" headerValue="Rating" />
</apex:dataTable>
</apex:outputPanel>
<apex:outputPanel title="Opportunity Details">
<apex:dataTable value="{!opps}" var="o" rules="rows" width="600" frame="border" cellpadding="10" rendered="{!opps.size>0}">
<apex:column value="{!o.name}" headerValue="Name" />
<apex:column value="{!o.Amount}" headerValue="Amount" />
<apex:column value="{!o.CloseDate}" headerValue="CloseDate" />
<apex:column value="{!o.stageName}" headerValue="StageName" />
</apex:dataTable>
</apex:outputPanel>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:outputPanel>
</apex:form>
</apex:page>
Controller:
public class soql_example_6 {
public String accName { set; get; }
public String accRating { set; get; }
public String query { set; get; }
public String query2 { set; get; }
public List<Account> accounts { set; get; }
public List<Opportunity> opps { set; get; }
public SOQL_Example_6() {
accounts = new List<Account>();
opps = new List<Opportunity>();
}
public void search(){
query = 'SELECT Id, Name, Phone, Rating, Industry FROM Account';
if(accName != '' && accRating != '') {
query = query + ' WHERE Name =\'' + accName + '\' AND Rating = \'' + accRating + '\'';
}
else if(accName != '' && accRating == '') {
query = query + ' WHERE Name = \'' + accName + '\'';
}
else if(accRating != '' && accName == '') {
query = query + ' WHERE Rating = \'' + accRating + '\'';
}
system.debug('query------' + query);
accounts = Database.query(query);
system.debug('accounts------- ' + accounts);
}
public void search2(){
query2 = 'SELECT Id, Name, Amount, CloseDate, StageName from Opportunity';
if(accName != '') {
query2 = query2 + ' WHERE Account.Name = \'' + accName + '\' ';
system.debug('query2------' + query2);
opps = Database.query(query2);
system.debug('------- ' + opps);
}
}
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
All Answers
Try this code:
Vf Page:
<apex:page controller="SOQL_Example_6">
<apex:form >
<br/><br/>
<apex:outputPanel style="width:1000px; height:40px;padding:10px;border:1px solid grey;margin-top:50px;">
Name : <apex:inputText value="{!accName}" />
Rating : <apex:inputText value="{!accRating}" />
<apex:commandButton value="Search" action="{!search}" reRender="fm"/>
<apex:commandButton value="Search Opportunity" action="{!search2}" reRender="fm"/>
</apex:outputPanel>
<br/><br/><br/>
<apex:outputPanel id="fm">
<apex:pageBlock>
<apex:pageBlockSection columns="2" rendered="true">
<apex:outputPanel title="Account Details">
<apex:dataTable value="{!accounts}" var="a" rules="rows" width="600" frame="border" cellpadding="10" rendered="{!accounts.size>0}">
<apex:column value="{!a.name}" headerValue="Name" />
<apex:column value="{!a.Phone}" headerValue="Phone" />
<apex:column value="{!a.Industry}" headerValue="Industry" />
<apex:column value="{!a.Rating}" headerValue="Rating" />
</apex:dataTable>
</apex:outputPanel>
<apex:outputPanel title="Opportunity Details">
<apex:dataTable value="{!opps}" var="o" rules="rows" width="600" frame="border" cellpadding="10" rendered="{!opps.size>0}">
<apex:column value="{!o.name}" headerValue="Name" />
<apex:column value="{!o.Amount}" headerValue="Amount" />
<apex:column value="{!o.CloseDate}" headerValue="CloseDate" />
<apex:column value="{!o.stageName}" headerValue="StageName" />
</apex:dataTable>
</apex:outputPanel>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:outputPanel>
</apex:form>
</apex:page>
Controller:
public class soql_example_6 {
public String accName { set; get; }
public String accRating { set; get; }
public String query { set; get; }
public String query2 { set; get; }
public List<Account> accounts { set; get; }
public List<Opportunity> opps { set; get; }
public SOQL_Example_6() {
accounts = new List<Account>();
opps = new List<Opportunity>();
}
public void search(){
query = 'SELECT Id, Name, Phone, Rating, Industry FROM Account';
if(accName != '' && accRating != '') {
query = query + ' WHERE Name =\'' + accName + '\' AND Rating = \'' + accRating + '\'';
}
else if(accName != '' && accRating == '') {
query = query + ' WHERE Name = \'' + accName + '\'';
}
else if(accRating != '' && accName == '') {
query = query + ' WHERE Rating = \'' + accRating + '\'';
}
system.debug('query------' + query);
accounts = Database.query(query);
system.debug('accounts------- ' + accounts);
}
public void search2(){
query2 = 'SELECT Id, Name, Amount, CloseDate, StageName from Opportunity';
if(accName != '') {
query2 = query2 + ' WHERE Account.Name = \'' + accName + '\' ';
system.debug('query2------' + query2);
opps = Database.query(query2);
system.debug('------- ' + opps);
}
}
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
Greetings to you!
- I implemented in my Org . Please use the below code[Solved].
- Correction in query in Search2().
Class : -
public class soql_example_6 {
//accountName
public String accName {set;get;}
// account rating
public String accRating {set;get;}
// query String
public String query {set;get;}
public String query2 {set;get;}
// Result fetched
public List<Account> accounts {set;get;}
public List<Opportunity> opps {set;get;}
public Set<Id> accId {get;set;}
public soql_example_6(){
accounts = new List<Account>();
opps = new List<Opportunity>();
accId = new Set<Id>();
}
// This search is for Accounts
public void search(){
// Default query
try{
query='select id ,name,phone,rating,industry from Account';
// if accName and accRating values are entered
// where name='TCS' and Rating ='Hot'
if(accName!=''&&accRating!=''){
query =query+' where name=\''+accName+'\' and rating=\''+accRating+'\'';
}else{
// if only name is entered
// where name='TCS'
if(accName!=''){
query=query+' where name=\''+accName+'\'';
} else if(accRating!=''){
// if only rating is entered
// where rating='Hot'
query=query+' where rating=\''+accRating+'\'';
}
}
accounts =Database.query(query);
System.debug('accounts-->'+accounts);
for(Account inst: accounts){
System.debug('inst-->'+inst.Id);
accId.add(inst.Id);
}
System.debug('accId-->'+accId);
if(accId!=null){
search2();
}
} catch (Exception e){
System.debug('msg-->'+e.getMessage()+' Line-->'+e.getLineNumber());
}
}
// This search is for related Opportunities
public void search2(){
try{
// Default Opportunity query
query2='select id,name,AccountId, Amount, CloseDate, StageName from Opportunity Where AccountId IN: accId';
// If Account Name is selected
// Fetch that Account Name ID to this query
opps =Database.query(query2);
System.debug('opps-->'+opps);
} catch (Exception e){
System.debug('msg-->'+e.getMessage()+' Line-->'+e.getLineNumber());
}
}
}
VF Page : -
<apex:page controller="soql_example_6">
<apex:form >
<br/><br/>
{!opps}
<apex:outputPanel style="width:1000px; height:40px;padding:10px;border:1px solid grey;margin-top:50px;">
Name : <apex:inputText value="{!accName}"/>
Rating : <apex:inputText value="{!accRating}" />
<apex:commandButton value="Search" action="{!search}" reRender="fm"/>
</apex:outputPanel>
<br/><br/><br/>
<apex:outputPanel id="fm">
<apex:pageBlock>
<apex:pageBlockSection columns="2" rendered="true">
<apex:outputPanel title="Account Details">
<apex:dataTable value="{!accounts}" var="a" rules="rows" width="600" frame="border" cellpadding="10" rendered="{!accounts.size>0}">
<apex:column value="{!a.name}" headerValue="Name" />
<apex:column value="{!a.Phone}" headerValue="Phone" />
<apex:column value="{!a.Industry}" headerValue="Industry" />
<apex:column value="{!a.Rating}" headerValue="Rating" />
</apex:dataTable>
</apex:outputPanel>
<apex:outputPanel title="Opportunity Details">
<apex:dataTable value="{!opps}" var="o" rules="rows" width="600" frame="border" cellpadding="10" rendered="{!opps.size>0}">
<apex:column value="{!o.name}" headerValue="Name" />
<apex:column value="{!o.Amount}" headerValue="Amount" />
<apex:column value="{!o.CloseDate}" headerValue="CloseDate" />
<apex:column value="{!o.stageName}" headerValue="StageName" />
</apex:dataTable>
</apex:outputPanel>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:outputPanel>
</apex:form>
</apex:page>
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha.