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
Gage Staruch 9Gage Staruch 9 

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

I'm not quite sure what I'm doing wrong. Any help is appreciated!

I keep getting this error: System.QueryException: List has no rows for assignment to SObject

 

My trigger:
 

trigger ActiveAccount on Account (before insert, before update, after insert) {
    for(account a: trigger.new)
    {
        if (a.Active_Orchestrate_Platform__c == True || a.cb1 == True || a.cb2 == True || a.cb3 == True || a.cb4 == True || a.cb5 == True)
        {
   a.cb6 = True;
        }
    }
}

My test Class:

 

@isTest
private class ActiveAccountTest{
    static testmethod void validateActiveAccount()
    {
        // Create your test data
        Account acc = new Account();
        acc.name= 'test';
        insert acc;     
        
        Opportunity oppObj = new Opportunity(Name = 'TestOpp',AccountID = acc.Id,Amount = 2000,CloseDate=Date.today(),StageName='A - Purchase / Close Won', MarketingGeneratedType__c = 'Sales');
		insert oppObj;

		Product2 newProd = new Product2 (Name = 'Product ABC', ProductLine__c ='ABC Products');
		insert newProd;

        ID lstpbe = [SELECT Product2.Id, Product2.Name
                               FROM PriceBookEntry 
                               WHERE Pricebook2Id IN (SELECT Id 
                                                      FROM PriceBook2 
                                                      WHERE Name = 'ABC Products')].id;
		PriceBookEntry pbEntry = new PriceBookEntry(
    	UnitPrice = 1,
    	Product2Id = lstpbe,
   		IsActive = true);

		insert pbEntry ;

		OpportunityLineItem oppLine = new OpportunityLineItem(pricebookentryid=pbEntry.Id,TotalPrice=2000,Quantity = 1,OpportunityID = oppObj.Id);
		insert oppLine;
        
        Test.startTest();
            Account acct4 = [Select cb6 from Account limit 1];
        Test.stopTest();
         }
}
ShirishaShirisha (Salesforce Developers) 
Hi Gage,

Greetings!

This error occurs whenever we try to access an empty list.So,can you please check if the below SOQL query is returning any records:

SELECT Product2.Id, Product2.Name FROM PriceBookEntry WHERE Pricebook2Id IN (SELECT Id FROM PriceBook2 WHERE Name = 'ABC Products'

Please follow the below document for more details on troubleshooting the issue.

https://help.salesforce.com/HTViewSolution?id=000159853

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
Gage Staruch 9Gage Staruch 9
Hi @Shirisha, 

I should have probably specified, the list isnt empty. It actually returns 1 record, which is exactly the record ID that I need. 
ShirishaShirisha (Salesforce Developers) 
Hi Gage,

Can you please try by printing the value for lstpbe by adding the system.debug() statement in the class to confirm,if it has any value.

Also,I can see that you are trying to store the Id in one variable.So,can you please follow the below approach to proceed further.

https://developer.salesforce.com/forums/?id=906F00000008oGJIAY

Thanks&Regards,
Shirisha Pathuri
Gage Staruch 9Gage Staruch 9

@Shirisha, 

When I tired to print the value for lstpbe, I got this error:

 

System.QueryException: List has more than 1 row for assignment to SObject

Agustin BAgustin B
Hi Gage, with that error you have more than 1 record, you could try to specify more the condition on the query or if it is the same do something like this:
1) ID lstpbe = [SELECT Product2.Id, Product2.Name FROM PriceBookEntry WHERE Pricebook2Id IN (SELECT Id FROM PriceBook2 WHERE Name = 'ABC Products') LIMIT 1].Id
2) ID lstpbe = [SELECT Product2.Id, Product2.Name FROM PriceBookEntry WHERE Pricebook2Id IN (SELECT Id FROM PriceBook2 WHERE Name = 'ABC Products')][0].Id
Gage Staruch 9Gage Staruch 9

Hi @Agustin, 

Thank you for your comment. It looks like I am still having this issue. Below is the updated code

@isTest
private class ActiveAccountTest{
    static testmethod void validateActiveAccount()
    {
        // Create your test data
        Account acc = new Account();
        acc.name= 'test';
        insert acc;     
        
        Opportunity oppObj = new Opportunity(Name = 'TestOpp',AccountID = acc.Id,Amount = 2000,CloseDate=Date.today(),StageName='A - Purchase / Close Won', MarketingGeneratedType__c = 'Sales');
		insert oppObj;

		Product2 newProd = new Product2 (Name = 'Product ABC', ProductLine__c ='ABC Products');
		insert newProd;

		PriceBookEntry pbEntry = new PriceBookEntry(
    	UnitPrice = 1,
        Product2Id = newProd.Id,
   		IsActive = true);
        ID lstpbe = [SELECT Product2.Id, Product2.Name FROM PriceBookEntry WHERE Pricebook2Id IN (SELECT Id FROM PriceBook2 WHERE Name = 'ABC Products') LIMIT 1].Id;        
            
		insert pbEntry ;

		OpportunityLineItem oppLine = new OpportunityLineItem(pricebookentryid=pbEntry.Id,TotalPrice=2000,Quantity = 1,OpportunityID = oppObj.Id);
		insert oppLine;
        
        Test.startTest();
            Account acct4 = [Select cb6 from Account limit 1];
        Test.stopTest();
         }
}