4

My Apex class is

Class1:

Public class myclass{
    //some code
    public void save(){
        invoice_policy__c inp=[select net__c,Internal_alerts__c,Send_Email__c,Email_Alerts__c,Send_To__c from invoice_policy__c where id=:selectin]; //getting error here
        if(inp.Internal_alerts__c == true){
             //some code
        }
    }
    string Selectin;
    Public string getSelectin(){return Selectin;}
    public void setSelectin(string selectin){this.selectin = Selectin;}

    public list<selectoption> getselectname(){
        list<selectoption> option = new list<selectoption>();  

        for(Invoice_Policy__c invoice : [select id,name,Policy_Status__c from Invoice_Policy__c where Policy_Status__c='Active'] ){       

            option.add(new selectoption(invoice.Id,invoice.name));
            system.debug('invocenameee'+invoice.Id);  
        }
        return option;     
    } 
}

My test class is

invoice_policy__c ip = new invoice_policy__c(name='TestPolicy',net__c='3',Policy_Status__c='Active',send_to__c='[email protected]',Email_Alerts__c='Before',Internal_Alerts__c =true);
ip.send_email__c='2';
insert ip;
system.debug('testttt'+ip);

myclass ic = new myclass();
ic.getselectin();
ic.setselectin('a0Wi0000000NMqbEAG');
ic.save();

Getting error as System.QueryException List has no rows for assignment to SObject Help on this please.

6
  • 1
    you dont want to rely on setAllData, better setup your own data. but besides that, what does the following print out to the console? public class myclass2{ public void myMethod1(string strid,string invoiceid){ invoice__c invoice=[select Name,guid__c,invoice_contact__c,status__C from invoice__c where id=:invoiceid]; system.debug('###invoice=' + invoice); ... } }
    – Seb Wagner
    Commented May 15, 2013 at 13:00
  • If I don'y use seealldata=true,getting QueryException somewhere else in my code.Whatever Id I'm passing in my first class myclass those related fields are displayed in Invoice
    – Eagerin Sf
    Commented May 15, 2013 at 13:06
  • Unit tests should exercise your methods as users would; why not just mimic the normal program flow?
    – Mike Chale
    Commented May 15, 2013 at 13:07
  • 1
    On why seealldata=true is a bad idea: salesforce.stackexchange.com/a/8562/142
    – Mike Chale
    Commented May 15, 2013 at 13:08
  • 1
    Why are you setting selectin to a hardcoded ID field? ic.setselectin('a0Wi0000000NMqbEAG'); in a testmethod you should never refer to hard coded IDs. Seems like you want ic.setselectin(ip.id)
    – cropredy
    Commented Jun 12, 2013 at 1:51

1 Answer 1

4

As crop1645 in the comments pointed out, you're hard-coding an Id into your test that does not represent the object you just inserted.

In general though,

System.QueryException List has no rows for assignment to SObject

can be avoided by selecting to a list and then doing a check as to whether the list contains any objects. You can use that to either gracefully handle this condition or to surface a custom, and perhaps more descriptive exception to be handled elsewhere.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .