0

I tried this code i m getting error like this

"List has no rows for assignment to SObject"


public class delrecords {
  public string cid{get;set;}
  public list<contact> conlist{get;set;}
  public contact con{get;set;}

  public list<contact> getcontacts(){
    conlist = [select id,firstname,lastname,email from contact where (owner.id =: userinfo.getuserid())];
    return conlist; 
  }

  public void deletecon(){
    con = [select id,firstname,lastname,email from contact where id=:cid];
    delete con;
  }
}

7
  • 1
    from the page you are assigning the value to the variable cid , but in the apex controller method you are using the variable conid to get the contact Commented Nov 23, 2015 at 11:45
  • i changed contact id conid to cid still its not working getting same error "list has no rows fos assignment to sobject".....
    – user123
    Commented Nov 23, 2015 at 11:49
  • please any one give me solution for this
    – user123
    Commented Nov 23, 2015 at 11:58
  • system.debug your conid/cid value in your apex and see if its populating. Commented Nov 23, 2015 at 11:58
  • no its not populating returning 0 contact records in debug log
    – user123
    Commented Nov 23, 2015 at 12:02

1 Answer 1

0

This will give you the error if no records match:

con = [select id,firstname,lastname,email from contact where id=:cid];

Instead, do

List<Contact> cons = [select id,firstname,lastname,email from contact where id=:cid]; 
if(cons.size() > 0) { delete cons[0]; }
0

You must log in to answer this question.

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