Code coverage help for Salesforce controller for VF page? - salesforce
I have a controller that is showing 0% code coverage, but I'm not sure why my test class isn't covering anything. Can anyone help?
Here is my controller:
public class R2MBizBookController{
public List<Buyer__c> listOfDeck {get; set;}
public List<Buyer__c> listOfNewThirty {get; set;}
public List<Buyer__c> listOfNewTW {get; set;}
public List<Buyer__c> listOfLegacy {get; set;}
public List<Buyer__c> listOfTQ {get; set;}
public List<Buyer__c> listOfAQ {get; set;}
public List<Buyer__c> listOfBQ {get; set;}
public List<Buyer__c> listOfCQ {get; set;}
public List<Buyer__c> listOfDQ {get; set;}
public List<Buyer__c> listOfEQ {get; set;}
public Buyer__c Live {get; set;}
public Buyer__c NewTW {get; set;}
public Buyer__c Viability {get; set;}
public Buyer__c LaunchPad {get; set;}
public Buyer__c TQ {get; set;}
public Buyer__c AQ {get; set;}
public Buyer__c BQ {get; set;}
public Buyer__c CQ {get; set;}
public Buyer__c DQ {get; set;}
public Buyer__c EQ {get; set;}
public R2MBizBookController() {
listofDeck = [Select id, name, Sales_Lead_Source__c, Routing_Status__c, Client_Status__c, Total_Re_Orders__c, Sales_Quarter__c, Funds_Collected_All_Time__c, Sales_LN__c, Commitment_c__c from Buyer__c WHERE Pipeline_Status__c = 'OnDeck' ORDER BY Name ASC];
listofNewThirty = [Select id, name, Sales_Lead_Source__c, Routing_Status__c, Client_Status__c, Total_Re_Orders__c, Sales_Quarter__c, Funds_Collected_All_Time__c, Sales_LN__c, Sales_Origination_Date__c from Buyer__c WHERE Pipeline_Status__c = 'New 30' ORDER BY Sales_Origination_Date__c DESC];
listofNewTW = [Select id, name, Sales_Lead_Source__c, Routing_Status__c, Client_Status__c, Total_Re_Orders__c, Sales_Quarter__c, Funds_Collected_All_Time__c, Sales_LN__c, Sales_Origination_Date__c from Buyer__c WHERE New_Live__c = TRUE ORDER BY Sales_Origination_Date__c DESC];
listofLegacy = [Select id, name, Sales_Lead_Source__c, Routing_Status__c, Client_Status__c, Total_Re_Orders__c, Sales_Quarter__c, Funds_Collected_All_Time__c, Sales_LN__c, Sales_Origination_Date__c from Buyer__c WHERE Pipeline_Status__c = 'Legacy' ORDER BY Sales_Origination_Date__c DESC];
listofTQ = [Select id, name, Sales_Lead_Source__c, Routing_Status__c, Client_Status__c, Total_Re_Orders__c, Sales_Quarter__c, Funds_Collected_All_Time__c, Sales_LN__c, Sales_Origination_Date__c from Buyer__c WHERE TQ_Pipeline__c = TRUE ORDER BY Sales_Origination_Date__c DESC];
listofAQ = [Select id, name, Sales_Lead_Source__c, Routing_Status__c, Client_Status__c, Total_Re_Orders__c, Sales_Quarter__c, Funds_Collected_All_Time__c, Sales_LN__c, Sales_Origination_Date__c from Buyer__c WHERE Pipeline_Status__c = 'AQ' ORDER BY Sales_Origination_Date__c DESC];
listofBQ = [Select id, name, Sales_Lead_Source__c, Routing_Status__c, Client_Status__c, Total_Re_Orders__c, Sales_Quarter__c, Funds_Collected_All_Time__c, Sales_LN__c, Sales_Origination_Date__c from Buyer__c WHERE Pipeline_Status__c = 'BQ' ORDER BY Sales_Origination_Date__c DESC];
listofCQ = [Select id, name, Sales_Lead_Source__c, Routing_Status__c, Client_Status__c, Total_Re_Orders__c, Sales_Quarter__c, Funds_Collected_All_Time__c, Sales_LN__c, Sales_Origination_Date__c from Buyer__c WHERE Pipeline_Status__c = 'CQ' ORDER BY Sales_Origination_Date__c DESC];
listofDQ = [Select id, name, Sales_Lead_Source__c, Routing_Status__c, Client_Status__c, Total_Re_Orders__c, Sales_Quarter__c, Funds_Collected_All_Time__c, Sales_LN__c, Sales_Origination_Date__c from Buyer__c WHERE Pipeline_Status__c = 'DQ' ORDER BY Sales_Origination_Date__c DESC];
listofEQ = [Select id, name, Sales_Lead_Source__c, Routing_Status__c, Client_Status__c, Total_Re_Orders__c, Sales_Quarter__c, Funds_Collected_All_Time__c, Sales_LN__c, Sales_Origination_Date__c from Buyer__c WHERE Pipeline_Status__c = 'EQ' ORDER BY Sales_Origination_Date__c DESC];
}
}
and here is my test class:
#isTest(seeAllData = true)
public class R2MBizBookControllerTest{
// Unit test Method
static testmethod void UnitTest() {
//Create your buyer record with required field
//Buyer__c b = new Buyer__c(Pipeline_Status__c = 'Legacy');
//insert b;
test.startTest();
R2MBizBookController ub = new R2MBizBookController();
test.stopTest();
}
}
would anyone be willing to help me beef this up?
it would mean the world to me.
thank you in advance!!!!!!
John
There are several best practices that you are breaking in your code. However, without knowing what this is being used for, it's hard to provide you with fixes.
As it's written, your test class should be providing 66% coverage of your controller not 0%. You need an average of 75% across all Triggers and Classes. You need to provide coverage for the 10 Buyer__c variables in your controller.
Also, be sure to deploy to production your Test Class with your Controller.
Related
How to combine one-to-many relationship and many-to-many relationship in Entity Framework Core
My requirement is: A post is created by a user (Author), many users can be tagged in a post (TaggedUsers). A user can create many post (CreatedPosts) and a user can be tagged in many posts (TaggedPosts). So I create 2 class of entities Post and User as below. public class Post { public int Id {get; set;} public string Title {get; set;} public string Body {get; set;} public User Author {get; set;} public ICollection<User> TaggedUsers {get; set;} } public class User { public int Id {get; set;} public string Username {get; set;} public ICollection<Post> CreatedPosts {get; set;} public Collection<Post> TaggedPosts {get; set;} } So how can I configure relationship (using Fluent API or Data Annotation) in Code-first approach. The database should have 3 tables: Users, Posts, PostTags. Users: Id Username Posts: Id User_id Title Body PostTags: Id Post_id User_id Thank you very much for your help.
Here is an example of what you're trying to do: class MyContext : DbContext { public MyContext(DbContextOptions<MyContext> options) : base(options) { } public DbSet<Post> Posts { get; set; } public DbSet<Tag> Tags { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Post>() .HasMany(p => p.Tags) .WithMany(p => p.Posts) .UsingEntity<PostTag>( j => j .HasOne(pt => pt.Tag) .WithMany(t => t.PostTags) .HasForeignKey(pt => pt.TagId), j => j .HasOne(pt => pt.Post) .WithMany(p => p.PostTags) .HasForeignKey(pt => pt.PostId), j => { j.Property(pt => pt.PublicationDate).HasDefaultValueSql("CURRENT_TIMESTAMP"); j.HasKey(t => new { t.PostId, t.TagId }); }); } } public class Post { public int PostId { get; set; } public string Title { get; set; } public string Content { get; set; } public ICollection<Tag> Tags { get; set; } public List<PostTag> PostTags { get; set; } } public class Tag { public string TagId { get; set; } public ICollection<Post> Posts { get; set; } public List<PostTag> PostTags { get; set; } } public class PostTag { public DateTime PublicationDate { get; set; } public int PostId { get; set; } public Post Post { get; set; } public string TagId { get; set; } public Tag Tag { get; set; } }
Unable to make the foreign key with non-primary key
I am trying to create a database called Donation within it it got this column IC its is and identity number value so I had already set it into IsUnique but somehow it still return the error message Donation_Customers_Target_Donation_Customers_Source: : The types of all properties in the Dependent Role of a referential constraint must be the same as the corresponding property types in the Principal Role. The type of property 'IC' on entity 'Donation' does not match the type of property 'CustomerID' on entity 'Customers' in the referential constraint 'Donation_Customers'. And here is the model of the Donation and Customers Customer [Key] public int CustomerID { get; set; } [Display(Name = "IC")] [Index(IsUnique = true)] [Required(AllowEmptyStrings = false, ErrorMessage = "IC is required")] [MinLength(14)][MaxLength(14)] public string IC { get; set; } [Display(Name = "First Name")] [Required(AllowEmptyStrings = false, ErrorMessage = "First name required")] public string FirstName { get; set; } Donation [Key] public int DonationId { get; set; } public DateTime? TransacTime { get; set; } public int Amount { get; set; } public string IC { get; set; } [ForeignKey("IC")] public Customers Customers { get; set; }
Convert query from SQL to Entity Framework code first approach
I want to convert my SQL query into Entity Framework code-first but unable to do it. This is my SQL query select * from tests where id in(select testid from PatientTests where PatientId=#id) This is Test Model from this model I want to fetch records. public class Tests { [Key] public int Id { get; set; } [Required] [Display(Name = "Test Name")] public string TestName { get; set; } [Display(Name = "Short Name")] public string ShortName { get; set; } [Display(Name="Technical Name")] public string TechName { get; set; } [Required] [Display(Name ="Test Price")] public float TestPrice { get; set; } [Display(Name = "Sub Department")] public int SubDeptId { get; set; } [Display(Name = "Center")] public int CenterId { get; set; } public string Separate { get; set; } [Display(Name = "Sub Department")] [ForeignKey("SubDeptId")] //relation of departments table public virtual SubDepartments subDepartments { get; set; } [Display(Name = "Centers")] [ForeignKey("CenterId")] //relation of departments table public virtual Centers centers { get; set; } } this is patient tests model public class PatientTest { [Key] public int Id { get; set; } [Display(Name ="Patient Id")] public int PatientId { get; set; } [Display(Name ="Test Id")] public int TestId { get; set; } [Display(Name ="Doctor")] public int DoctorId { get; set; } [Display(Name="Center")] public int CenterId { get; set; } [Display(Name = "Test")] [ForeignKey("TestId")] //relation of Tests table public virtual Tests Tests { get; set; } [Display(Name = "Doctor Reference")] [ForeignKey("DoctorId")] //relation of Doctors table public virtual Doctors Doctors { get; set; } [Display(Name = "Center Reference")] [ForeignKey("CenterId")] //relation of Centers table public virtual Centers Centers { get; set; } [Display(Name = "Patient")] [ForeignKey("PatientId")] //relation of Patient table public virtual Patient Patient { get; set; } } So I want record from tests table where id should be matched with patientTest table testid and only given patient Id record must be fetch.
Your Tests model seems to be missing a navigation property to PatientTest. It can still be done though. Guessing a bit here for how your context properties are named. var tests = context.PatientTests .Where(pt => pt.PatientId == patientId) .Select(pt => pt.Tests) .ToList();
Apex code-how to get the data of campaign Members from Subquery
I am trying to display four fields on my VF page Name, Status from (Campaign Members) and Subject and Last Modified Date from (Activity History) Object.I am not able to fetch the Name and Status fields from Campaign Members. Can anyone please tell me How to get the Name and Status fields from Campaign Members.Below is my Controller code. public with sharing class CampaignView { public Campaign camp {get; set; } public List<MemberWrapper> lMemberWrappers {get; set;} public DateTime startDate {get; set;} public CampaignView(ApexPages.StandardController controller) { camp = (Campaign)controller.getRecord(); lMemberWrappers = new List<MemberWrapper>(); getCampaignMembers(); startDate = null; for (Campaign c : [Select ID, (SELECT Id, CampaignId, Status FROM CampaignMembers where CampaignId = :camp.Id) FROM campaign WHERE id = :camp.Id ]) { for (Lead ld : CampaignMembers) { for (ActivityHistory ah : ld.getSObjects('ActivityHistories')) { lMemberWrappers.add(new MemberWrapper(ld.Name, ah.Subject, ah.LastModifiedDate, ld.CampaignMembers.get(0).Status)); } } } } private List<Lead> CampaignMembers; public List<Lead> getCampaignMembers() { CampaignMembers = [Select Id, Name, Phone, MobilePhone, Email, LastModifiedDate, (Select id, Campaign.Name, Contact.Phone, Lead.FirstName, Lead.LastName, Lead.Name, LeadID, ContactID, Lead.Phone, Lead.Email, Lastmodifieddate, Lead.LastmodifiedDate, Status, CampaignId, Campign_ID__c, Lead.MobilePhone From CampaignMembers where CampaignId = :camp.Id ), (Select Subject, Id, lastModifiedDate From ActivityHistories order by LastModifiedDate DESC LIMIT 1 ) From Lead where Id IN(select LeadId from campaignMember where campaignId = :camp.Id ) ]; return CampaignMembers; } public class MemberWrapper { public Object Status {get; set;} public String Name {get; set;} public String Subject {get; set;} public Datetime LastActivityHistory {get; set;} public MemberWrapper(String Name, String Subject, Datetime LastActivityHistory, Object Status ) { this.Name = Name; this.Subject = Subject; this.LastActivityHistory = LastActivityHistory; this.Status = Status; } } }
This will take you more than one query because of the ActivityHistory requirement. There are four levels of relationships in play here: Campaign -> CampaignMember -> Lead -> ActivityHistory, so you cannot perform this using just sub-queries. The following code should get you the info you need: Set<Id> leadIds = new Set<Id>(); List<Campaign> campaignList = [Select ID, (SELECT Id, Status, LeadId FROM CampaignMembers) FROM campaign WHERE id = :camp.Id ]; for (Campaign c : campaignList) { for (CampaignMember cm : c.CampaignMembers) { leadIds.add(cm.LeadId); } } Map<Id, Lead> leadMap = new Map<Id, Lead>([Select Id, Name, (Select Subject, LastModifiedDate from ActivityHistories Order By LastModifiedDate DESC limit 1) From Lead Where Id IN :leadIds]); for (Campaign c : campaignList) { for (CampaignMember cm : c.CampaignMembers) { Lead ld = leadMap.get(cm.LeadId); if (ld.ActivityHistories.size() > 0) { ActivityHistory ah = ld.ActivityHistories[0]; lMemberWrappers.add(new MemberWrapper(ld.Name, ah.Subject, ah.LastModifiedDate, cm.Status)); } } } I just skipped creating the MemberWrapper if there were no activities, but you could change it to just put blank values if you wish.
MVC-4 Assigning Employees and Manager to Department Scenerio and EF-CodeFirst
I have a Department Table with Fields public class Department { [ScaffoldColumn(false)] public int DepartmentId { get; set; } [Required(ErrorMessage = "*")] [StringLength(100)] public string DepartmentName { get; set; } public bool Active { get; set; } public int LocationID { get; set; } public virtual Location UsersCompanyLocation { get; set; } } and an Employee Table public class Employee { [ScaffoldColumn(false)] [Key] public int UserId { get; set; } [Required(ErrorMessage = "*")] [StringLength(50)] public string FirstName { get; set; } [StringLength(50)] public string LastName { get; set; } public string UserName { get; set; } [DataType(DataType.Password)] public string Password { get; set; } [Required(ErrorMessage = "*")] [StringLength(50)] public string IDCardNo { get; set; } [StringLength(50)] public string Address { get; set; } [StringLength(50)] public string City { get; set; } [StringLength(50)] public string Zip { get; set; } [StringLength(50)] public string Mobile { get; set; } [StringLength(50)] public string HomePhone { get; set; } public int? DesignationId { get; set; } public Designations EmployeeDesignation { get; set; } public int? DepartmentID { get; set; } public virtual Department UserDepartment { get; set; } [Display(Name = "Company", ResourceType = typeof(Translations.Translation))] public int CompanyID { get; set; } public virtual Company UserCompany { get; set; } public int? LocationID { get; set; } public virtual Location UsersCompanyLocation { get; set; } [StringLength(500)] [Display(Name = "Pic", ResourceType = typeof(Translations.Translation))] public string Pic { get; set; } [Required(ErrorMessage = "*")] [DataType(DataType.Date)] [Display(Name = "HireDate", ResourceType = typeof(Translations.Translation))] public DateTime HireDate { get; set; } [Required(ErrorMessage = "*")] [DataType(DataType.Date)] [Display(Name = "DateofBirth", ResourceType = typeof(Translations.Translation))] public DateTime DOB { get; set; } [Required(ErrorMessage = "*")] [StringLength(50)] [Display(Name = "MaritalStatus", ResourceType = typeof(Translations.Translation))] public string MaritialStatus { get; set; } [StringLength(50)] public string AccountNumber { get; set; } [Required(ErrorMessage = "*")] [StringLength(256)] [DataType(DataType.EmailAddress, ErrorMessage = "*")] public string PersonalEmail { get; set; } [Required(ErrorMessage = "*")] [StringLength(256)] [DataType(DataType.EmailAddress, ErrorMessage = "*")] public string OfficialEmail { get; set; } public int? RoleId { get; set; } public virtual Roles UserRole { get; set; } public int? EmploymentStatusID { get; set; } public virtual Employment EmploymentStatus { get; set; } } Now my Scenerio is thatI want To Assign one Employee as Manager to a Department and from the remaining pool of Employees, i may assign Employees to That Department. I am using code first EF technique, I am not able to make a proper class to work with this scenerio. Secondly I want to use List Box twice, first from which we may select just 1 employee as Manager and Second from which I want to select multiple employees from the remaing pool of Employees. Can any one help me in this scenerio I have done the database portion after using up the whole day I have to make another table (offcourse a class in EF CodeFirst) "DepartmentManager" public class DepartmentManager { [ScaffoldColumn(false)] public int ID { get; set; } public int DepartmentId { get; set; } public virtual Department AssignedDepartment { get; set; } public int UserID { get; set; } public virtual Employee ManagerialEmployee { get; set; } public virtual List<DepartmentEmployees> DepartmentEmployees { get; set; } This table has A UserID foreign key, Now for assignig employees to the Department, i will make another table "DepartmentEmployees" with composite keys public class DepartmentEmployees { [ScaffoldColumn(false)] [Key, Column(Order = 0)] public int ID { get; set; } [ScaffoldColumn(false)] [Key, Column(Order = 1)] public int EmployeeId { get; set; } } Now two things to ask, how to make foreign key UserID unique so that it may not be repeated, 2nd how to use List Box such that in one List Box just one Employee is Selected and in 2nd one, we may select the remainging employees, and how to fill the 2nd List Box such that an employee which has been selected as Manager doesn't appear in the 2nd List Box ??????????????