Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Can anyone substantiate why this is?

    Opportunity o2 = new Opportunity();
    o2.Name = 'DSGTestClassRecord Opportunity 2';
    o2.StageName = 'New';
    o2.CLoseDate =;
    insert o2;

    Opportunity OppRec1 = [select Id From Opportunity Where Name = 'DSGTestClassRecord Opportunity 2'];
    OppRec1.StageName = 'Closed Won';
    OppRec1.Sub_Stage__c = 'Paid';
    OppRec1.Opportunity_Discount_Approved__c = true;

    update OppRec1;

In this class code, the OppRec1 SELECT statement is returning that no rows are found. What?

share|improve this question
up vote 2 down vote accepted

1) instead of using name= 'DSGTestClassRecord Opportunity 2' do Id = : o2.Id;

Opportunity OppRec1 = [select Id From Opportunity Where id=: o2.Id];

2) If you want to verify the name is the one you inserted

System.assertEquals('DSGTestClassRecord Opportunity 2',;
share|improve this answer
Interesting. I had misunderstood that the search (by name) was important to the particular test being done, but it is not. Thanks! – AMM Dec 12 '12 at 19:23
I am sure you might have tried looking up, but just incase the reference slipped out,check this doc : – Rao Dec 12 '12 at 19:36
Thank you. . . . – AMM Dec 12 '12 at 19:42

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.