You need to sign in to do that
Don't have an account?
System.QueryException: List has no rows for assignment to SObject
Hi guys,
I need your help. I have this error and i can't find any solution. im stuck for a few days now...thanks (heres the code)
Error Message: System.QueryException: List has no rows for assignment to SObject ---- Class.POController.<init>: line 31, column 1
Code:
public class POController {
public String orderLineItems { get; set; }
public String getPO_Line_Item() {
return null;
}
public class MissingAccountException extends Exception {}
public class NoLineItemsException extends Exception {}
public Account account {get; private set;}
public Opportunity opportunity {get; private set;}
public Purchase_Order__c order {get; private set;}
public OpportunityLineItem oppLineItem {get; private set;}
public PO_Line_Item__c[] poLineItems {get; private set;}
public PO_Line_Item__c poLineItem {get; private set;}
public List<PO_Line_Item__c> lineItems= new List<PO_Line_Item__c>();
private integer itemCount=0;
private PriceBookEntry pbEntry;
public POController() {
Boolean ShowPartner=false;
Id id = ApexPages.currentPage().getParameters().get('id');
//Id accountId=ApexPages.currentPage().getParameters().get('aId');
Id opportunityId = ApexPages.currentPage().getParameters().get('oId');
opportunity = (opportunityId == null) ? new Opportunity() :[SELECT name, accountID, CloseDate, Description, StageName, Pricebook2Id FROM Opportunity WHERE id = pportunityId];
if (opportunity.AccountId != null){
account =new Account();
account= [SELECT name, accountnumber, billingstreet, BillingCity, BillingState, BillingPostalCode, BillingCountry,ShippingStreet, ShippingCity, ShippingState, ShippingPostalCode, ShippingCountry, phone, fax FROM account WHERE id = pportunity.AccountId] ;
}else {
throw new MissingAccountException('Cannot create an order from this opportunity: Please assign an account to the opportunity.');
}
order = (id == null) ? new Purchase_Order__c() :
[SELECT Purchase_Order__c.Name FROM Purchase_Order__c WHERE id = :id];
//Pre-populate Opportunity and account information
order.Status_Code__c='New';
//Create a new PO_Line_Item__c and populate a row for each POLineItem that exists
integer i = 0;
poLineItems = new PO_Line_Item__c[0];
for(OpportunityLineItem opp: [SELECT Id, SortOrder, PricebookEntryId, Description, Quantity, ListPrice, UnitPrice, TotalPrice, ServiceDate FROM OpportunityLineItem WHERE Opportunityid = pportunityId])
{
string pbeID = opp.PricebookEntryId;
pbEntry = (pbeId == null) ? new PriceBookEntry() :
[Select Id, Name, Pricebook2Id, Product2Id, ProductCode from PricebookEntry where Id=beID];
poLineItem = new PO_Line_Item__c();
poLineItem.Purchase_Order__c=order.Id;
poLineItem.Product__c = pbEntry.Product2Id;
poLineItem.Name = pbEntry.Name;
poLineItem.Unit_Price__c = opp.ListPrice;
poLineItems.add(poLineItem);
i++;
}
itemCount=i;
if (itemCount == 0) {
throw new NoLineItemsException('Unable to create PO: Products must be added to Sales Order before creating a PO.');
}
System.Debug('Finished Page Load');
}
//Returns the user back to the Sales Order page this was launched from
public PageReference cancel(){
return (new ApexPages.StandardController(Opportunity)).view();
}
//saves PO and child PO
public PageReference save() {
System.Debug('Running Save Method');
try
{
insert order;
System.Debug(order.id);
for(PO_Line_Item__c li: poLineItems)
{
li.Purchase_Order__c=order.id;
}
insert poLineItems;
//If the Opportunity Stage is not 'Closed Won' then update it
if (Opportunity.StageName != 'Closed Won'){
Opportunity.StageName='Closed Won';
update Opportunity;
}
}catch(System.DMLException e)
{
ApexPages.addMessages(e);
return null;
}
// After Save, navigate to the default view page:
return (new ApexPages.StandardController(order)).view();
}
public PageReference loadHelpPage(){
PageReference pg = new PageReference('/apex/CreatePOHelp');
return pg;
}
}
I can't tell which is line 31 in your code, but it is one of the SELECT lines. This error means that the SELECT is not returning any records. You can use System.debug() to help you narrow down the cause.
Hi Jeff,
I actually forgot to include the line with the error.
this is the code:
//Retrieve the PriceBook name for Prepopulation in order
string pbID = opportunity.Pricebook2Id;
PriceBook2 pb = (opportunity.Pricebook2Id == null) ? new PriceBook2() :
[Select Id, Name from Pricebook2 where Id=:pbID];
order.Pricebook__c=pb.Name;
thanks
Try adding a system.debug() to see what the pricebook2 ID is that's being used, and see if that is an actual pricebook in your org.
[Select Id, Name from Pricebook2 where Id=:pbID];
order.Pricebook__c=pb.Name;"
does not return any value. Check it in console whether it returns any value or not
It would obviously throw that error while assigning query to object when query returns no rows.