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
kumar.fdc81.3902978579608325E12kumar.fdc81.3902978579608325E12 

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

Hi all,

I have written test class i am getting this error :-  System.QueryException: List has no rows for assignment to SObject

Test Class:-
========

@isTest
public class ContractSubtypeCarrythroughTest{
@isTest static void subtype(){

  ContractSubtypeCarrythroughTest csc = new ContractSubtypeCarrythroughTest();

        Profile p = [select id from profile where name='(TestUser)']; // This line getting error message
        UserRole r = [Select id from userrole where name='(TestUser)'];
     
     Account a = new Account(Name='Test', Type_Platform__c = 'Customer');
        insert a;
Contact con = new Contact(AccountId = a.Id, firstName = 'Test', lastName = 'Test');       
        insert con;       
   

//  UserRole ur = [Select PortalType, PortalAccountId From UserRole where PortalType =:'CustomerPortal' limit 1]; 
 


  User u= new User(alias = 'standt', email='standarduser@testorg.com', emailencodingkey='UTF-8',
                   lastname='Testing', languagelocalekey='en_US', profileid = p.Id,localesidkey='en_US',
                   timezonesidkey='America/Los_Angeles', username='standarduser@testorg.com' );
    insert u;                                 

   Opportunity O = new Opportunity(Name='TestPipeline',CloseDate=System.today(),Type='New Customer',Order_Type__c='New',
            Lead_Source_Category__c='Existing Customer',StageName='Create',Primary_Business_Driver__c='Test');
   insert O;
  
  Custom_Asset__c asset = new Custom_Asset__c(Current_Renewal_Period_Renewal_Type__c = 'TestRenewal', Most_Recent_Closed_Won_Time__c =System.Today(),
                                              Next_Renewal_Period_Renewal_Type__c = 'TestRenewal', End_User__c = 'u.id');
   insert asset;
  
  OpportunityLineItem oli = new OpportunityLineItem(Asset__c = 'asset.id');
  insert oli;                                                
}
}
Ramu_SFDCRamu_SFDC
If you are using developer console, can you please just run the below query and confirm if there are any results showing up?

select id from profile where name='(TestUser)'
Sonam_SFDCSonam_SFDC
remove the round brackets from the search string: 

SELECT Id,Name FROM UserRole WHERE NAME = 'EASTERN SALES TEAM'


kumar.fdc81.3902978579608325E12kumar.fdc81.3902978579608325E12
Hi Sonam,

I am getting same error.

Thanks
kumar
kumar.fdc81.3902978579608325E12kumar.fdc81.3902978579608325E12
Hi Ramu,

No Result is showing.

Thanks
kumar
Sonam_SFDCSonam_SFDC
Do you have a profile in your ORG with name: test user?

You need to use seealldata = true at the bigenning of this class, as you are trying to search for the profile form the ORG:

read more: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_seealldata_using.htm
kumar.fdc81.3902978579608325E12kumar.fdc81.3902978579608325E12
Hi Sonam,


 Now i am getting this error: System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_USERNAME, Duplicate Username.<br>Another user has already selected this username.<br>Please select another.: [Username]

@isTest(SeeAllData=true)
public class ContractSubtypeCarrythroughTest{
@isTest static void subtype(){

  ContractSubtypeCarrythroughTest csc = new ContractSubtypeCarrythroughTest();

        Profile p = [select id from profile where name='SumT Finance'];
       UserRole r = [Select id from userrole where name='ANZ CBM'];
     
     Account a = new Account(Name='Test', Type_Platform__c = 'Customer');
        insert a;
Contact con = new Contact(AccountId = a.Id, firstName = 'Test', lastName = 'Test');       
        insert con;       
   

//  UserRole ur = [Select PortalType, PortalAccountId From UserRole where PortalType =:'CustomerPortal' limit 1]; 
 


  User u= new User(alias = 'standt', email='standarduser@testorg.com', emailencodingkey='UTF-8',
                   lastname='Testing', languagelocalekey='en_US', profileid = p.Id,localesidkey='en_US',
                   timezonesidkey='America/Los_Angeles', username='test@gmail.com' );


    insert u;                             // This line getting error message   


   Opportunity O = new Opportunity(Name='TestPipeline',CloseDate=System.today(),Type='New Customer',Order_Type__c='New',
            Lead_Source_Category__c='Existing Customer',StageName='Create',Primary_Business_Driver__c='Test');
   insert O;
  
  Custom_Asset__c asset = new Custom_Asset__c(Current_Renewal_Period_Renewal_Type__c = 'TestRenewal', Most_Recent_Closed_Won_Time__c =System.Today(),
                                              Next_Renewal_Period_Renewal_Type__c = 'TestRenewal', End_User__c = 'u.id');
   insert asset;
  
  OpportunityLineItem oli = new OpportunityLineItem(Asset__c = 'asset.id');
  insert oli;                                                
}
}

Thanks
kumar
Sonam_SFDCSonam_SFDC
Kumar,

Looks like the new user you are trying to create with username: test@gmail.com already exists in your system. try changing the username to test123@gmail.com

User u= new User(alias = 'standt', email='standarduser@testorg.com', emailencodingkey='UTF-8',
                   lastname='Testing', languagelocalekey='en_US', profileid = p.Id,localesidkey='en_US',
                   timezonesidkey='America/Los_Angeles', username='test123@gmail.com' );


    insert u;

kumar.fdc81.3902978579608325E12kumar.fdc81.3902978579608325E12
Hi Sonam,

I tried but same error

Error Message System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_USERNAME, Duplicate Username.<br>Another user has already selected this username.<br>Please select another.: [Username]

Thanks
kumar
Sonam_SFDCSonam_SFDC
Kumar,

Check the following thread, to avoid this duplicate username error - this user followed a different approach - worth a try!
https://developer.salesforce.com/forums/ForumsMain?id=906F00000008kpXIAQ

Vatsal KothariVatsal Kothari
Hey Kumar,

you can try below code :

@isTest (SeeAllData=true)
public class ContractSubtypeCarrythroughTest{
	public static testMethod void subtype(){

      ContractSubtypeCarrythroughTest csc = new ContractSubtypeCarrythroughTest();
    
           Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 
           
           //UserRole r = [Select id from userrole where name='TestUser'];
         
            User u = new User(Alias = 'standt', Email='standarduser321@testorg.com', 
                  EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
                  LocaleSidKey='en_US', ProfileId = p.Id, 
                  TimeZoneSidKey='America/Los_Angeles', UserName='standarduser321@testorg.com');
    
      
                insert u;
        
            System.runAs(u) {
            
                 Account a = new Account(Name='Test', Type_Platform__c = 'Customer');
                    insert a;
                Contact con = new Contact(AccountId = a.Id, firstName = 'Test', lastName = 'Test');       
                    insert con;       
               
                
               Opportunity O = new Opportunity(Name='TestPipeline',CloseDate=System.today(),Type='New Customer',Order_Type__c='New',
                        Lead_Source_Category__c='Existing Customer',StageName='Create',Primary_Business_Driver__c='Test');
               insert O;
              
              Custom_Asset__c asset = new Custom_Asset__c(Current_Renewal_Period_Renewal_Type__c = 'TestRenewal', Most_Recent_Closed_Won_Time__c =System.Today(),
                                                          Next_Renewal_Period_Renewal_Type__c = 'TestRenewal', End_User__c = 'u.id');
               insert asset;
              
              OpportunityLineItem oli = new OpportunityLineItem(Asset__c = 'asset.id');
              insert oli;  
            }
        }
}

If this solves your problem, kindly mark it as the best answer.

Thanks,
Vatsal