function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jason CastleJason Castle 

What's wrong with my SOQL query?

I'm trying to perform a unit test on a query that doesn't debug properly in the console because its not using live data.  In an effort to provide the data in the class itself, I'm still getting nothing.  Do you know why my contact isn't being added to my new list?

 

        Contact newContact1 = new Contact(
    	LastName =  'testvalue',
        AccountId = '001g000000f14cB',
    	//company = 'testervalue',
		Lead__c = leadToConvert.Id
		);
        system.debug(newContact1);
        List<Contact> searchList = [SELECT Id,Lead__c FROM Contact WHERE Lead__c =:leadToConvert.Id];
Best Answer chosen by Jason Castle
William TranWilliam Tran
Try insert the contact before selecting it.
 
Contact newContact1 = new Contact(
  	LastName =  'testvalue',
      AccountId = '001g000000f14cB',
  	//company = 'testervalue',
Lead__c = leadToConvert.Id
);

insert newContact1;

      system.debug(newContact1);

      List<Contact> searchList = [SELECT Id,Lead__c FROM Contact WHERE Lead__c =:leadToConvert.Id];

As a common practice, if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you. 

Thanks

All Answers

William TranWilliam Tran
Try insert the contact before selecting it.
 
Contact newContact1 = new Contact(
  	LastName =  'testvalue',
      AccountId = '001g000000f14cB',
  	//company = 'testervalue',
Lead__c = leadToConvert.Id
);

insert newContact1;

      system.debug(newContact1);

      List<Contact> searchList = [SELECT Id,Lead__c FROM Contact WHERE Lead__c =:leadToConvert.Id];

As a common practice, if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you. 

Thanks
This was selected as the best answer
Shyama B SShyama B S
Are you inserting the newContact1 object?
Before line 8, try adding:
insert newContact1;

Thanks.
Amit Chaudhary 8Amit Chaudhary 8
Account acc = new Account(Name='test');
insert acc

Contact newContact1 = new Contact(
  	LastName =  'testvalue',
      AccountId = acc.id
);

insert newContact1;

system.debug(newContact1);

List<Contact> searchList = [SELECT Id,Lead__c FROM Contact WHERE Lead__c =:leadToConvert.Id];v

Please try above code
hu yanghu yang
please insert the data before query it