function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Christian HenningChristian Henning 

Permission Sets giving issues in Apex Test Code

I am getting this error from the Developer Console:
System.QueryException: List has no rows for assignment to SObject
For this specific line in my test:
PermissionSet ps = [SELECT Id FROM PermissionSet WHERE Name = 'My_Permission_Set'];

For my permission set (titled 'My_Permission_Set'), I just cloned CMA Admin User. Is that enough? What else am I missing? This is my apex code (not the test - but the original, I just need access to Contacts and Cases):
 
global with sharing class ContactController {
    @RemoteAction
    global static Contact[] getContactInfo(String recordId) {
        String contactId = [SELECT ContactId FROM Case WHERE Id= :recordId WITH SECURITY_ENFORCED].ContactId;
        List<Contact> selectedContact = [SELECT Id, Name, LOB__c, PlanName__c FROM Contact WHERE Id= :contactId WITH SECURITY_ENFORCED];  
        SObjectAccessDecision decision = Security.stripInaccessible(
            AccessType.READABLE, 
            selectedContact);
        return (Contact[])decision.getRecords();
    }
}

 
Best Answer chosen by Christian Henning
Derrick AbbeyDerrick Abbey

Have you tried just querying the permissionset object with the name?  I would suggest that you make sure that your query works in the developer console query editor.  You might have the wrong name.  You could run a query of all permission sets in your org.  Include the name, developerName, and Label in your query (you might want to limit the results to where profileId = null).  Then you can be sure your name is correct.


As far as your error, that just means that your query is not returning any records and you cannot assign a null value to an sobject in Apex.

All Answers

Derrick AbbeyDerrick Abbey

Have you tried just querying the permissionset object with the name?  I would suggest that you make sure that your query works in the developer console query editor.  You might have the wrong name.  You could run a query of all permission sets in your org.  Include the name, developerName, and Label in your query (you might want to limit the results to where profileId = null).  Then you can be sure your name is correct.


As far as your error, that just means that your query is not returning any records and you cannot assign a null value to an sobject in Apex.

This was selected as the best answer
mukesh guptamukesh gupta
Hi Christian,

You don't have a mentioned permission set in your org that's your are using in SOQL:-


The error "List has no rows for assignment to SObject" occurs when query doesn't return any rows.
 
PermissionSet ps = [SELECT Id FROM PermissionSet WHERE Name = 'My_Permission_Set'];

It would be safer to do the following:
 
PermissionSet[] psList= [SELECT Id FROM PermissionSet WHERE Name = 'My_Permission_Set'];
if (psList.size() > 0){
  //pID = psList[0].Id;
}


if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh


 
Suraj Tripathi 47Suraj Tripathi 47

Hi Christian,

The error you are getting due to your query doesn't return any row might be you give the wrong name & the permission set might not exist in your salesforce org. Please ensure you give the API Name not the label to match the Name.

PermissionSet ps = [SELECT Id FROM PermissionSet WHERE Name = 'API Namet'];

If you find the above solution helpful, please mark it as the Best Answer.

 

Thanks & Regards.

Suraj Tripathi.

Christian HenningChristian Henning
Thank you all - your answers have all been very helpful