You need to sign in to do that
Don't have an account?
Andy Morton 14
Code coverage on public apex class displaying information
I'm new to apex coding and have run into a bit of a problem which I'm hoping someone can help with.
Basically, I've been tasked with a Salesforce deployment for my company a part of which requires a customer portal for viewing cases. Following some early prototyping we decided to use a piece of apex programming to display the case list in a format that is suited to our needs. The list basically consists of a drop down box that has two options, depending on the option chosen the class will pull data from Salesforce. this is being used on a visualforce page that is embedded into the community site.
I've used this code in Sandox with no issues but when I try to deploy to Production it fails due to only 71% code coverage.
I've read up on it and believe I need to create a test class to run the code but when I'm trying to create one it displays the code as incorrect. This is the test class:
I'm not quite sure what I'm doing wrong or if there's an easier way to achieve the code coverage required. The tutorials I've read so far are for creating test classes that manipulate data whereas this is simply used to get data.
If anyone can help point me int he right direction it would be greatly appreciated.
Many thanks
Andy
Basically, I've been tasked with a Salesforce deployment for my company a part of which requires a customer portal for viewing cases. Following some early prototyping we decided to use a piece of apex programming to display the case list in a format that is suited to our needs. The list basically consists of a drop down box that has two options, depending on the option chosen the class will pull data from Salesforce. this is being used on a visualforce page that is embedded into the community site.
I've used this code in Sandox with no issues but when I try to deploy to Production it fails due to only 71% code coverage.
public class CommunityCaseList { public string options; public List<Case> getNewCases() { if(options=='Open') { List<Case> results = [SELECT id,CaseNumber,Status,Priority,Accountid,Account.Name,Contactid,contact.name,subject,CreatedDate,ClosedDate FROM Case WHERE Status != 'Closed' ORDER BY CreatedDate DESC]; return results; } else if(options == 'Closed'){ List<Case> results = [SELECT id,CaseNumber,Status,Priority,Accountid,Account.Name,Contactid,contact.name,subject,CreatedDate,ClosedDate FROM Case WHERE Status = 'Closed' ORDER BY CreatedDate DESC]; return results; } else { List<Case> results = [SELECT id,CaseNumber,Status,Priority,Accountid,Account.Name,Contactid,contact.name,subject,CreatedDate,ClosedDate FROM Case WHERE Status != 'Closed' ORDER BY CreatedDate DESC]; return results; } } public string getOptions() { return this.options; } public void setOptions(String Opt) { this.options=opt; } public list<SelectOption> getItems() { List<selectOption> options = new List<SelectOption>(); options.add(new Selectoption('Open','Open')); options.add(new Selectoption('Closed','Closed')); return options; } public pagereference preview() { return null; } }
I've read up on it and believe I need to create a test class to run the code but when I'm trying to create one it displays the code as incorrect. This is the test class:
@isTest private class CommunityCaseListTest { static testMethod void validateCommunityCaseList() { public string options; public List<Case> getNewCases() { if(options=='Open') { List<Case> results = [SELECT id,CaseNumber,Status,Priority,Accountid,Account.Name,Contactid,contact.name,subject,CreatedDate,ClosedDate FROM Case WHERE Status != 'Closed' ORDER BY CreatedDate DESC]; return results; } else if(options == 'Closed'){ List<Case> results = [SELECT id,CaseNumber,Status,Priority,Accountid,Account.Name,Contactid,contact.name,subject,CreatedDate,ClosedDate FROM Case WHERE Status = 'Closed' ORDER BY CreatedDate DESC]; return results; } else { List<Case> results = [SELECT id,CaseNumber,Status,Priority,Accountid,Account.Name,Contactid,contact.name,subject,CreatedDate,ClosedDate FROM Case WHERE Status != 'Closed' ORDER BY CreatedDate DESC]; return results; } } public string getOptions() { return this.options; } public void setOptions(String Opt) { this.options=opt; } public list<SelectOption> getItems() { List<selectOption> options = new List<SelectOption>(); options.add(new Selectoption('Open','Open')); options.add(new Selectoption('Closed','Closed')); return options; } public pagereference preview() { return null; } } }
I'm not quite sure what I'm doing wrong or if there's an easier way to achieve the code coverage required. The tutorials I've read so far are for creating test classes that manipulate data whereas this is simply used to get data.
If anyone can help point me int he right direction it would be greatly appreciated.
Many thanks
Andy
Use this test class with 100% coverage..
https://trailhead.salesforce.com/en/content/learn/modules/apex_testing/apex_testing_intro Hope this will help...
Thank you...
All Answers
Use this test class with 100% coverage..
https://trailhead.salesforce.com/en/content/learn/modules/apex_testing/apex_testing_intro Hope this will help...
Thank you...
Greetings to you!
Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.
Thanks and Regards,
Khan Anas