+ Start a Discussion

System.QueryException: List has no rows for assignment to SObject

The error is coming when i want to fetch customerUserTypes in Profile.

Set<String> customerUserTypes = new Set<String> {'CSPLiteUser', 'PowerPartner', 'PowerCustomerSuccess',   'CustomerSuccess'};
Account acc = new Account (
Name = 'newAcc1'
insert acc;
Contact con = new Contact (
AccountId = acc.id,
LastName = 'portalTestUser'
insert con;
Profile p = [select Id,name from Profile where UserType in :customerUserTypes limit 1];

User newUser = new User(
profileId = p.id,
username = 'newUser@yahoo.com',
email = 'pb@ff.com',
emailencodingkey = 'UTF-8',
localesidkey = 'en_US',
languagelocalekey = 'en_US',
timezonesidkey = 'America/Los_Angeles',
contactId = con.id
insert newUser;
Anyone please help me why this exception error is coming. And how to solve it.
The most obvious check that needs to be done is if there actually are any Profiles with the UserType in the list of values mentioned above and if the running user has access to the data based on the class sharing visibility. A quick run through the profiles for the sandbox you are working on should answer this.
Customer portal user will not have access to user object, so you are gett no row as SOQL result.