3

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:

debug

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!

2 Answers 2

3

I tried a cut down version without the RecordTypeIds and custom fields and it seemed to work fine. 5 Opportunities were inserted and brought back via the SOQL query.

Account a = new Account(name = 'testAccount', BillingCity = 'Cambridge', BillingState = 'MA',  BillingPostalCode='02135');
insert a;

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');
    oList.add(o);
}

//Insert test cases
insert oList;

for(Opportunity db : oList){
    system.debug('Inserted Opportunity: '+db.Id);
}

string query = 'testOpp%';
List<Opportunity> oppQuery = [SELECT Id, accountId FROM Opportunity WHERE Name like :query];
system.debug('oppQuery: '+oppQuery);
System.assertEquals(oList.size(), oppQuery.size());

A couple of suggestions:

  1. Try and avoid hard coding the RecordTypeId in test cases. This will make your code less portable between orgs.
  2. Check to see if you have any triggers or workflow rules that may be altering your records when they are inserted. E.g. something might be changing the Opportunity Name.
1
  • Thanks Daniel, you were spot with your 2nd suggestion. Our org renames the opportunity after it's parent account + date created. Before insert the opportunity name was "testOpp..." and after insert it became "testAccount...". Nov 13, 2013 at 16:25
1

You are declaring a list of fixed size zero.

Opportunity[] oList = new Opportunity[0];

Change it to:

List<Opportunity> oList = new List<Opportunity>();
1
  • on second thought im not confident in this answer seeing as how the debug output youve attached shows five items.
    – Phil Rymek
    Nov 13, 2013 at 0:38

You must log in to answer this question.

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