1

I have taken a reference from : https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_custom.htm and developed below code.

But it gives me the following error.

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

MyController

public class MyController {
    private final Account account;

    public MyController(){
        Id newId = ApexPages.currentPage().getParameters().get('id');
        System.debug('New ID : '+newId);
        account = [SELECT Id, Name, Site FROM Account WHERE Id = :newId];
    }

    public Account getAccount(){
        return account;
    }

    public PageReference save(){
        update account;
        return null;
    }
}

MyControllerVF

<apex:page controller="MyController" tabStyle="Account">
    <apex:form>
        <apex:pageBlock title="Congratulations {!$User.FirstName}">
            You belong to Account Name: <apex:inputField value="{!account.name}"/>
            <apex:commandButton action="{!save}" value="save"/>
        </apex:pageBlock>
    </apex:form>
</apex:page>

enter image description here

My input screen.

enter image description here

  • Its a good practice to add limit 1 to the query when getting one record, also do you actually have any record in the system that matches that Id? try pasting the record id in url and see where it takes you – d_k May 16 '17 at 5:37
  • 2
    make sure to put the ?id=<an account id> in the url when requesting the page. – martin May 16 '17 at 5:40
  • @martin, put your comment as answer – Santanu Boral May 16 '17 at 5:44
1

EDIT added upsert + exception handling

This is not going to fix the problem - while the error checking is good, it won't prevent the error mentioned here from occurring.

You shouldn't query straight to a single sObject - you need to query to a list. Eg:

Account[] accounts = [SELECT Id, Name, Site FROM Account WHERE Id = :newId];
if (!accounts.isEmpty()){
  account = accounts[0];
}
else {
  account = new Account();
}

Then your save method can upsert the Account.

public PageReference save(){
  try {
    upsert account;
    return null;
  }
  catch (Exception e){
    ApexPages.addMessage(new ApexPages.Message(
      ApexPages.Severity.Error, 
      'Line ' + e.getLineNumber() + ' ' + e.getMessage()
    )); 
  }
  return null;
}
0

You should always query in a List like:

Id newId = ApexPages.currentPage().getParameters().get('id');
System.debug('New ID : '+newId);
if (newId == NULL) { 
    ApexPages.addMessage(new ApexPages.Message(
        ApexPages.Severity.Error, 
        System.Label.UnabletoFindIdParameter // make sure to create the custom label
    )); 
} else {
    List<account> accounts = [SELECT Id, Name, Site FROM Account WHERE Id = :newId];
    if(accounts.size() > 0){
        /* We got record from database process it using accounts[0] */
    }else{
        /* No record found with the Id provided in the URL. */
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.