This is my first attempt at Apex. I'm calling Apex code from a Custom Button. I've successfully run the code (largely inspired by Andrew Fawcett and the Salesforce Apex Approval Process Example)

but now I'm writing the unit test. When I run it, it's failing with error:

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

Here's the class:

public with sharing class DetailButtonController
    private ApexPages.StandardController standardController;

    public DetailButtonController(ApexPages.StandardController standardController)
        this.standardController = standardController;

    public PageReference doSomething()
        // Apex code for handling record from a Detail page goes here
        Id recordId = standardController.getId();
        Opportunity record = (Opportunity) standardController.getRecord();
        // My code
        User user1 = [SELECT Id FROM User WHERE Alias='mhall']; 
        // Create an approval request for the opportunity

        Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest(); 
        req1.setComments('Submitting request for approval.'); 

        // Submit on behalf of a specific submitter 

        // Submit the record to specific process and skip the criteria evaluation 
        req1.setNextApproverIds(new Id[] {user1.id}); 
        // Submit the approval request for the opportunity 
        Approval.ProcessResult result = Approval.process(req1); 

        // End my code
        return null;

And Here is the Unit Test

public class DetailButtonControllerTest
    public static void TestDetailButtonController(){
        Opportunity MyTestOppty = [SELECT id From Opportunity LIMIT 1];

        Test.StartTest(); //indicate that the test is about to start

        PageReference myVfPage = Page.opptopartnerbuttonpage;

        ApexPAges.StandardController sc = new ApexPages.StandardController(MyTestOppty);
        DetailButtonController bcPartnerRel = new DetailButtonController(sc); //instance of the class we’re testing       

        bcPartnerRel.doSomething(); //call actual function to perform the test

        Test.StopTest(); // end the test


Is there anything obvious? I'm trying to set trace flags or view a debug log, but haven't figured those out yet. Any help is appreciated.

  • 1
    what is the error your are getting. please update this question with your test class. – Himanshu Dec 15 '15 at 19:53
  • Error message is: System.QueryException: List has no rows for assignment to SObject – MHalliday Dec 15 '15 at 20:20
  • go to setup-> apex test execution and select the test class. in bottom section you will see test method which is failing click on that. you will see detail information about the error message. kindly post that one here. though you can fix one issue mentioned by Eric in his comment. – Himanshu Dec 15 '15 at 20:23
  • Himanshu thank you for your time and help. Once I made the changes Eric suggested, the test ran successfully. – MHalliday Dec 15 '15 at 20:50
  • great :) that is what I suggested. – Himanshu Dec 15 '15 at 20:54

This line:

Opportunity MyTestOppty = [SELECT id From Opportunity LIMIT 1];

Queries for an opportunity. Since you did not create an opportunity you will get List has no rows for assignment to sObject

you need to created the Account, Opportunity records in your test class.

The reason that you need to create the records is that by default and best practice, test data is isolated from production data. Thus when you start the test you start with a completely blank database sans some of the setup objects.


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.