You need to sign in to do that
Don't have an account?
Compass 1
Can we use Select query with where condition inside Apex:Page
Can we use Select query with where condition inside Apex:Page instead of using Apex:Class.
I would like to fetch data from Opportunity for the selected columns using where condition.
I would like to fetch data from Opportunity for the selected columns using where condition.
No, I was not suggesting you call generateReport from your VF page, although I assume you will want to, otherwise what purpose does it serve? I was suggesting that you add a call to it from your test code to ensure you have full code coverage. Something like the line in bold.
Finally, what line(s) of code are not being executed in your controller? That would help me to determine where the problem is.
All Answers
Not exactly where condition, but you can achieve this within VF page using <apex:repeat> tag.. Does this help? Let me know.
Also above code snippet works only for one Account at a time (and its child Opportunities). If you want to query all opportunities, then I am afraid there is no other way in VF page. You should write query in Apex controller.
My requirement is to get the all opportunity by providing the where condition. I have tried through controller but when i running the test I am getting only 60% coverage. Could you advise what more needs to add in my test unit to get more than 75% so I can deploy the changes in Production.
====================================
public class topprospectsqry{
public Opportunity[] getOpportunityList() {
Opportunity[] opps = [SELECT
name,
account.name,
Facility__c,
Scope_Of_Work__c,
Pricing_Type__c,
BiddingStatus__c,
Estimated_Revenue_MM__c,
Contract_Award_Date__c,
Project_Go__c,
Project_Get__c
FROM
Opportunity
where
Top_Prospect__c='Yes'
order by Estimated_Revenue_MM__c desc
LIMIT 20
];
return opps;
}
public pageReference generateReport() {
return Page.TopProspectsRpt;
}
}
-------------------------------
@isTest
private class topprospecttest{
static testMethod void TestTopProspects(){
// Load the test data
Account acc = new Account();
acc.Name='Test of the Account1';
acc.type='Home';
insert acc;
// Load the test opportunities
List<Opportunity> opps = new List<Opportunity>();
for(Integer i=0;i<200;i++){
Opportunity opp = new Opportunity();
opp.AccountId = acc.Id;
opp.StageName = 'Prospecting';
opp.CloseDate = Date.today();
opp.Name = 'Test opp ' + i;
opps.add(opp);
}
insert opps;
// Test start here
Test.startTest();
topprospectsqry testcls = new topprospectsqry();
List<Opportunity> returned = testcls.getOpportunityList();
System.assertEquals(0,returned.size());
Test.stopTest();
}
}
=====================================
Top_Prospect__c=true so your query will return 0 Opportunities (unless there is a workflow rule or trigger that is setting that field behind the scenes?).
Also you should call generateReport on your StandardController. Then you can do some assertions on what you would expect to be in the returned PageReference object. It is not clear what to expect based on the code you have shared.
If am i am not misunderstood you meant I should add generateReport on my StandardController in my Apex Page?
Actually I am looking to create one Apex Page to display the selected fields from Opportunity after providing some filters. I just need to do a select query no need to insert and update any record.
Is there a way to fetch records directly while designing the Apex Page instead of using any extension.
As I am new in Apex development due to which I am not having clear idea on available functions.
Hope you got my requirements.
No, I was not suggesting you call generateReport from your VF page, although I assume you will want to, otherwise what purpose does it serve? I was suggesting that you add a call to it from your test code to ensure you have full code coverage. Something like the line in bold.
Finally, what line(s) of code are not being executed in your controller? That would help me to determine where the problem is.
As i said I am new Apex Development due to which i am not able to check wich line is not executed in my controller.
Find below the Apex Code which i have written,
-------------------------------------
public class topprospectsqry{
public Opportunity[] getOpportunityList() {
Opportunity[] opps = [SELECT
name,
account.name,
Facility__c,
Scope_Of_Work__c,
Pricing_Type__c,
BiddingStatus__c,
Estimated_Revenue_MM__c,
Contract_Award_Date__c,
Project_Go__c,
Project_Get__c
FROM
Opportunity
where
Top_Prospect__c='Yes'
order by Estimated_Revenue_MM__c desc
LIMIT 20
];
return opps;
}
public pageReference generateReport() {
return Page.TopProspectsRpt;
}
}
------------------------------------------------------
To achieve my this page i had written the given class and test unit.
But I am not able to get more than 75% of coverage and cannot deploy my this page in production.
I have created one customized field called "Top_Prospect__c" having drop value "Yes" or "No"
Is the code shown above the entire class being tested and entire unit test? or is there some code that is not displayed?
With your last code reference for my test unit after I have updated I am able to get now 100% coverage and I am able to deploy the page into production. Once again thanks for your great help.