I created a method to run a query based on a string parameter for the Opportunity.Name field. I've tried using the parameter for other fields and it works fine, but for the 'Name' field I'm getting no results.
This seems to only be in unit testing, I can execute the code on it's own and don't notice the same issues. This makes me believe there is something wrong with the test class, but can't think of what I could be overlooking.
Here's the method I'm trying to test:
public void oppOCRcreate(string query) { List<Opportunity> oppQuery = [SELECT Id, accountId FROM Opportunity WHERE Name like :query]; system.debug('oppQuery: '+oppQuery); Test code:
@istest static void validateOCRQueryOpps(){ //Create test account Account a = new Account(name = 'testAccount', BillingCity = 'Cambridge', BillingState = 'MA', Family_Last_Name__c = 'reassignMitchener', BillingPostalCode='02135'); //Insert test account insert a; //test opp Opportunity[] oList = new Opportunity[0]; for(integer i = 0; i < 5; i++){ Opportunity o = new Opportunity(accountid = a.id, CloseDate = date.today(), name = 'testOpp'+i, StageName = 'Qualified Inquiry', Date_Looking_to_Host__c = date.today(), RecordTypeId='012i0000000DXaOAAW'); oList.add(o); } //Insert test cases insert oList; for(Opportunity db : oList){ system.debug('Inserted Opportunities: '+db); } //Create testOCR object CreateOCRQuery testOCR = new CreateOCRQuery(); //Call testOCR method testOCR.oppOCRcreate('testOpp%'); Here is what is output from debug log:
No errors, but I'm puzzled why no results are being captured in the oppQuery List. The WHERE clause in my query should catch all opportunities with LIKE 'testOpp%'. The 5 opportunities all have the name "testOpp".
Is there something I'm overlooking?
Thanks for the help!