You need to sign in to do that
Don't have an account?
Help with Test Class Error: System.QueryException: List has no rows for assignment to SObject
Hi all,
I have this controller:
Any thoughts?
I have this controller:
public with sharing class ProfileDetailController { /*Initialization*/ public ProfileDetailController(ApexPages.StandardController stdController) { Id currentContactId = UserUtils.getCurrentUserContactId(); Id theProfileId = stdController.getRecord().Id; theProfile = [select Id, FirstName, LastName, Site_Year__c, Email, Phone, MailingCity, MailingState, Facebook_URL__c, Twitter_URL__c, Privacy_Setting__c from Contact where Id = :theProfileId]; settings = [select Display__c, Name from Breakthrough_Application_Settings__c where Active__c = true LIMIT 1]; isDisplayed = settings.Display__c; // render 'Public' view if this is rhe current user's profile (Contact) or the Privacy Setting is 'Public' isPublic = (theProfile.Id == currentContactId || theProfile.Privacy_Setting__c == 'Public'); } /*End Initialization*/ /*Properties*/ public Contact theProfile { get; private set; } public Boolean isPublic { get; private set; } public Breakthrough_Application_Settings__c settings { get; set; } public Boolean isDisplayed { get; set; } /*End Properties*/ /*Action Methods*/ /*End Action Methods*/ /*Helper Methods*/ /*End Helper Methods*/ }This is my test class:
@isTest private class ProfileDetailControllerTest { // get full profile regardless of Privacy Setting when viewing user's own profile static testMethod void viewOwnProfile() { Account a = AccountUtils.createBreakthroughOrgAccount('Breakthrough Alumni', true); Contact c = ContactUtils.createAlumniContact('My', 'Alumnus', 'myalumnus@org.test', a.Id, false); c.SYSTEM_Share_Community_User__c = true; insert c; User u = UserUtils.createAlumniCommunityUser(c, false, true); // hack to workaround sharing rule //ContactShare cs = new ContactShare(ContactId = c.Id, UserOrGroupId = u.Id, ContactAccessLevel = 'Read'); //insert cs; Test.startTest(); System.runAs(u) { Test.setCurrentPage(Page.ProfileDetail); Id theProfileId = stdController.getRecord().Id; ApexPages.StandardController stdController = new ApexPages.StandardController(c); ProfileDetailController ext = new ProfileDetailController(stdController); System.assertEquals(c.Id, ext.theProfile.Id); System.assertEquals('Private', ext.theProfile.Privacy_Setting__c); System.assertEquals(true, ext.isPublic); } Test.stopTest(); } // viewing profile of an alumnus with public Privacy Setting static testMethod void viewPublicProfile() { Account a = AccountUtils.createBreakthroughOrgAccount('Breakthrough Alumni', true); Contact c1 = ContactUtils.createAlumniContact('My', 'Alumnus1', 'myalumnus1@org.test', a.Id, false); Contact c2 = ContactUtils.createAlumniContact('My', 'Alumnus2', 'myalumnus2@org.test', a.Id, false); c1.SYSTEM_Share_Community_User__c = true; c2.SYSTEM_Share_Community_User__c = true; c2.Privacy_Setting__c = 'Public'; insert new List<Contact> { c1, c2 }; User u = UserUtils.createAlumniCommunityUser(c1, false, true); Breakthrough_Application_Settings__c setting = new Breakthrough_Application_Settings__c(); setting.Name = '2017'; setting.Display__c = true; setting.Active__c = true; insert setting; // hack to workaround sharing rule //ContactShare cs1 = new ContactShare(ContactId = c1.Id, UserOrGroupId = u.Id, ContactAccessLevel = 'Read'); //ContactShare cs2 = new ContactShare(ContactId = c2.Id, UserOrGroupId = u.Id, ContactAccessLevel = 'Read'); //insert new List<ContactShare> { cs1, cs2 }; Test.startTest(); System.runAs(u) { Test.setCurrentPage(Page.ProfileDetail); ApexPages.StandardController stdController = new ApexPages.StandardController(c2); ProfileDetailController ext = new ProfileDetailController(stdController); System.assertEquals(c2.Id, ext.theProfile.Id); System.assertEquals('Public', ext.theProfile.Privacy_Setting__c); System.assertEquals(true, ext.isPublic); System.assertEquals(true, setting.Display__c); } Test.stopTest(); } // viewing profile of an alumnus with private Privacy Setting static testMethod void viewPrivateProfile() { Account a = AccountUtils.createBreakthroughOrgAccount('Breakthrough Alumni', true); Contact c1 = ContactUtils.createAlumniContact('My', 'Alumnus1', 'myalumnus1@org.test', a.Id, false); Contact c2 = ContactUtils.createAlumniContact('My', 'Alumnus2', 'myalumnus2@org.test', a.Id, false); c1.SYSTEM_Share_Community_User__c = true; c2.SYSTEM_Share_Community_User__c = true; c2.Privacy_Setting__c = 'Private'; insert new List<Contact> { c1, c2 }; User u = UserUtils.createAlumniCommunityUser(c1, false, true); Breakthrough_Application_Settings__c setting = new Breakthrough_Application_Settings__c(); setting.Name = '2017'; setting.Display__c = true; setting.Active__c = true; insert setting; // hack to workaround sharing rule //ContactShare cs1 = new ContactShare(ContactId = c1.Id, UserOrGroupId = u.Id, ContactAccessLevel = 'Read'); //ContactShare cs2 = new ContactShare(ContactId = c2.Id, UserOrGroupId = u.Id, ContactAccessLevel = 'Read'); //insert new List<ContactShare> { cs1, cs2 }; Test.startTest(); System.runAs(u) { Test.setCurrentPage(Page.ProfileDetail); ApexPages.StandardController stdController = new ApexPages.StandardController(c2); ProfileDetailController ext = new ProfileDetailController(stdController); System.assertEquals(c2.Id, ext.theProfile.Id); System.assertEquals('Private', ext.theProfile.Privacy_Setting__c); System.assertEquals(false, ext.isPublic); System.assertEquals(true, setting.Display__c); } Test.stopTest(); } }I am looking for help on why I am getting this error thrown: System.QueryException: List has no rows for assignment to SObject : Class.ProfileDetailController.: line 19, column 1 Class.ProfileDetailControllerTest.viewOwnProfile: line 36, column 1
Any thoughts?
In Setup -> Security Controls -> Sharing Settings : Contacts
When Contact Sharing is set to "Controlled by parent", Contacts may not be shared independently from Accounts, so Contact Sharing Rules are not applied (even if they are defined).
https://help.salesforce.com/articleView?id=000004005&language=en_US&type=1
So if Contact Sharing is set to "Controlled by parent", you could share the account of the contact in the test class like that : Regards
All Answers
Id theProfileId = stdController.getRecord().Id;
ApexPages.StandardController stdController = new ApexPages.StandardController(c); Regards
Alain
The problem could be the user (not allowed for the contacts) :
c1.SYSTEM_Share_Community_User__c = true;
c1.Privacy_Setting__c = 'Public';
or
c1.Privacy_Setting__c = 'Private';
User u = UserUtils.createAlumniCommunityUser(c1, false, true);
System.runAs(u) {
or
System.runAs(u) {Without the constraint of the user, the sample code could work but that doesn't test the profiles.
Regards
Contact c = ContactUtils.createAlumniContact('My', 'Alumnus', 'myalumnus@org.test', a.Id, false);
c.SYSTEM_Share_Community_User__c = true;
insert c; // does this contact exist? there is no error, it is practically certain.
User u = UserUtils.createAlumniCommunityUser(c, false, true);
Minimal code: that works.
Differences: System.runAs(u) and Test.setCurrentPage(Page.ProfileDetail);
Not sure if it's best practice, but I put in a ContactShare rule to allow for this test class to run without error.
Could you try this in your test class:
System.assertEquals(ContactSharing.manualShareRead(c.Id, u.Id), true);
Regards
In Setup -> Security Controls -> Sharing Settings : Contacts
When Contact Sharing is set to "Controlled by parent", Contacts may not be shared independently from Accounts, so Contact Sharing Rules are not applied (even if they are defined).
https://help.salesforce.com/articleView?id=000004005&language=en_US&type=1
So if Contact Sharing is set to "Controlled by parent", you could share the account of the contact in the test class like that : Regards