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
Frank JordanFrank Jordan 

Apex Class with Button error: List has no rows for assignment to SObject

I have a class that is updating the owner of a record when the Submit to Legal button on the "Agreement Request" object is pressed. I am running into the "List has no rows for assignment to SObject" error when pressing the button and i'm not sure why. Below is my code: 

public with sharing class classSubmitToLegal {

    public Agreement_Request__c a{get;set;}
    public classSubmitToLegal() {
     a = new Agreement_Request__c ();
     a = [select Business_Unit__c from Agreement_Request__c Where Id=: ApexPages.currentPage().getParameters().get('Id')];
    }
    
    public void someMethod() {
      if(a.Business_Unit__c == 'EIS') {
         a.Business_Unit__c = 'EIS Queue';
         update a;
          Apexpages.Message errorMessage = new Apexpages.Message(ApexPages.Severity.Info,'You have changed the owner');
          Apexpages.addMessage(errorMessage);
      } else {
          Apexpages.Message errorMessage = new Apexpages.Message(ApexPages.Severity.Info,'You cannot update');
          Apexpages.addMessage(errorMessage);
      }
    }

}
Balaji BondarBalaji Bondar
Frank ,

Below query is not returning any records.Thats the reason of issue:
select Business_Unit__c from Agreement_Request__c Where Id=: ApexPages.currentPage().getParameters().get('Id')
pconpcon
It would be because either there is no Agreement_Request__c with that Id or the Id parameter is not set correctly.  You can work around this error (and give yourself more debugging information) by updating your code to the following:

NOTE: When adding code please use the "Add a code sample" button (icon <>) to increase readability and make it easier to reference.
 
public Agreement_Request__c a{get;set;}
public classSubmitToLegal() {
    System.debug(System.LoggingLevel.ERROR, 'Id: ' + ApexPages.currentPage().getParameters().get('id'));

    for (Agreement_Request__c ar : [
        select Business_Unit__c
        from Agreement_Request__c
        where Id = :ApexPages.currentPage().getParameters().get('id')
        limit 1
    ]) {
        this.a = ar;
    }  
}
    
public void someMethod() {
    if (this.a == null) {
        Apexpages.Message errorMessage = new Apexpages.Message(ApexPages.Severity.Info, 'Could not find request');
        Apexpages.addMessage(errorMessage);
    }

    if (a.Business_Unit__c == 'EIS') {
        a.Business_Unit__c = 'EIS Queue';
        update a;
        Apexpages.Message errorMessage = new Apexpages.Message(ApexPages.Severity.Info,'You have changed the owner');
        Apexpages.addMessage(errorMessage);
        return;
    }

    Apexpages.Message errorMessage = new Apexpages.Message(ApexPages.Severity.Info,'You cannot update');
    Apexpages.addMessage(errorMessage);
}
NOTE: When adding code please use the "Add a code sample" button (icon <>) to increase readability and make it easier to reference.

I did update the .get to use a lowercase id instead of the uppercase Id.  This sometimes makes a difference as well.
Frank JordanFrank Jordan
The ID parameter was not set correctly. THank you both!
pconpcon
Great! If you could please choose a "best answer" so that this question can be removed from the unresolved queue and so others may easily find the answer.