You need to sign in to do that
Don't have an account?

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;
}
}
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;
}
}
select id from profile where name='(TestUser)'
SELECT Id,Name FROM UserRole WHERE NAME = 'EASTERN SALES TEAM'
I am getting same error.
Thanks
kumar
No Result is showing.
Thanks
kumar
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
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
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;
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
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
you can try below code :
If this solves your problem, kindly mark it as the best answer.
Thanks,
Vatsal