Task a=new Task();
a.Status='Communication Sent';
a.Action__c='Auto Execute';
a.Email_Address__c='[email protected]';
insert a;

I am trying to insert a Task in my test class. The insert trigger is firing, which contains:

Contact CT = [Select Id,Email from contact where name = 'Do Not Delete'];
    CT.Email = strEmailId;
    update CT;

I was getting the error

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

1 Answer 1


Unlike with User data and a few other objects, you don't get to see Contact records in a Test without setting SeeAllData=true. Much better than using that flag, however, is to just create this data in a @TestSetup method. You might also wish to move the string 'Do Not Delete' to a constant somewhere.

static final String DO_NOT_DELETE = 'Do Not Delete';
static void setup()
    Contact requiredContact = new Contact(LastName=DO_NOT_DELETE);
    // set required fields;
    insert requiredContact;

See also: Isolation of Test Data from Organization Data in Unit Tests

  • 1
    Field is not writeable: Contact.Name Sep 12, 2016 at 12:22
  • @keerthanachowdhary You just need to set LastName instead.
    – Adrian Larson
    Sep 23, 2016 at 23:24

You must log in to answer this question.

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