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
PlainviewPlainview 

Clarity regarding a received Error Message

Hello,

Have received this error message:

Apex script unhandled exception by user/organization: 00500000006r7Mb/00D00000000hgdL

Visualforce Page: /apex/opportunityButtons

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

Class.opportunityButtons.<init>: line 73, column 1


The relevant line from the code; line 73 starts with "theOpp":

public opportunityButtons(ApexPages.StandardController controller) {   
   
    refreshOpp = '';
   
    theOpp = [SELECT
          Id,
          OwnerId, 
          Contract__c,
          Type,
          Name,
          Pay_Option__c,
          StageName,
          Amount,
          Renewal_Opportunity__r.Id,
          Renewal_Opportunity__r.Amount,
          Renewal_Opportunity__r.StageName,
          RenewalWithUpgrade__c,
          RecordTypeId,
          triggerTimeStamp__c,
          Original_Agreement_Date_New__c,
          approvalStatus__c,
          Existing_Service__c,
          Existing_Downloads__c,
          Existing_Slots_per_User__c,
          Existing_Users__c,
          Existing_Max_Users__c,
          Existing_Contract_Term_Months__c,
          Existing_Subscription_Total__c,
          Pricing_Type__c,
          Downloads_Enabled__c,
          Slots_per_User__c,
          Number_of_Users__c,
          Max_Users__c,
          Contract_Term_Months__c,
          Subscription_Total__c,
          Product__c
          FROM Opportunity
          WHERE Id=:((Opportunity)controller.getRecord()).Id];   
     
  
    SAurl = '';
    SSPurl = '';

I haven't made changes, so am confused why this is popping up now? Anybody have an idea?

Thanks,
Julien

Avidev9Avidev9
Your code expects an Opp Id to be passed while opening the page and a user probably opnened it without one. You can handle it by checking

controller.getId() != NULL

and show appropriate error message in the page ?
Virendra ChouhanVirendra Chouhan
Hi Julien,

Avidev is right. You forget to pass id in url like 
/apex/opportunityButtons?id='xxxxxxxxxxxxxxx'
And if you assign this vf page in sObjects(StandardController) button or link then it'll automattically pass this id.

And you can also use ApexPages.currentPage().getParameters().get('id') for getting the id of the record.

so try this --

    theOpp = [SELECT Id, OwnerId, Contract__c, Type, Name,Pay_Option__c,StageName,Amount,Renewal_Opportunity__r.Id, Renewal_Opportunity__r.Amount,Renewal_Opportunity__r.StageName, RenewalWithUpgrade__c, RecordTypeId,triggerTimeStamp__c,Original_Agreement_Date_New__c, approvalStatus__c, Existing_Service__c,Existing_Downloads__c, Existing_Slots_per_User__c,Existing_Users__c, Existing_Max_Users__c,Existing_Contract_Term_Months__c,Existing_Subscription_Total__c,Pricing_Type__c,Downloads_Enabled__c,Slots_per_User__c, Number_of_Users__c,Max_Users__c,Contract_Term_Months__c,Subscription_Total__c,Product__c FROM Opportunity WHERE Id=:ApexPages.currentPage().getParameters().get('id')];


Regards
Virendra
AshwaniAshwani
Plainview,

"System.QueryException: List has no rows for assignment to SObject" says the query from which you are expecting atleast a record is unable to fetch any record based on criteria specified so it is unable to assign instance of queried sobject records to the List. It must have atleast one record to work with. Make sure your query returns atleast one record.
bob_buzzardbob_buzzard

Your code is assigning the results of the query to an instance of an sobject - for this to be successful the query must return exactly one result. In your case it most likely means that the page is being used to create an opportunity so the id is null.

Its best practice to assign the results of the query to a list, and then check the size of the list to determine whether there were any matches - that will handle 0, 1 or multiple matches so should work in any scenario.

List<Opportunity> opps= [SELECT
          Id,
          OwnerId, 
          Contract__c,
          Type,
          Name,
          Pay_Option__c,
          StageName,
          Amount,
          Renewal_Opportunity__r.Id,
          Renewal_Opportunity__r.Amount,
          Renewal_Opportunity__r.StageName,
          RenewalWithUpgrade__c,
          RecordTypeId,
          triggerTimeStamp__c,
          Original_Agreement_Date_New__c,
          approvalStatus__c,
          Existing_Service__c,
          Existing_Downloads__c,
          Existing_Slots_per_User__c,
          Existing_Users__c,
          Existing_Max_Users__c,
          Existing_Contract_Term_Months__c,
          Existing_Subscription_Total__c,
          Pricing_Type__c,
          Downloads_Enabled__c,
          Slots_per_User__c,
          Number_of_Users__c,
          Max_Users__c,
          Contract_Term_Months__c,
          Subscription_Total__c,
          Product__c
          FROM Opportunity
          WHERE Id=:((Opportunity)controller.getRecord()).Id];   
     
if (1==opps.size())
{
   theOpp=opps[0];
}
else
{
    // do whatever is required to happen when there is no existing opportunity
}