I am trying to create my very first test class from some code that was left with me from a long gone dev. The original code was choosing a random opp from the past however due to the historical record being locked, the deployment was error-ing out. I need help. Total newbie here with no dev support determined to get this right.

You will see the note in the code that he left indicating he shortcut the test class....


@istest private class opportunityProductEntryTests {

static testMethod void theTests(){

    // You really should create test data, but I'm going to query instead
    // It's my best shot of avoiding a test failure in most orgs
    // Once you've installed this package though, you might want to write your own tests
    // or at least customize these ones to make them more applicable to your org

     Opportunity testOpportunity = new Opportunity(
        StageName = 'Inquiry',
        AccountID = '0010v000003wijc',
        CloseDate = Date.today(),
        Name = 'Test Opportunity Triggers'

    insert testOpportunity;

    OpportunityLineItem oli = [select Id, PricebookEntryId, PricebookEntry.Pricebook2Id, PricebookEntry.Name, PriceBookEntry.Product2Id, OpportunityId, Opportunity.AccountId from OpportunityLineItem where **Opportunity.Id =:Opportunity.Id**];

The compiler interprets Opportunity.Id as an SObjectField token because Opportunity is a known SObjectType.

Replace that with testOpportunity.Id which is the Id field of the SObject instance you have just created. The lookup field name is also OpportunityId and there might be zero, 1 or many line items so:

OpportunityLineItem[] olis = [select ... where OpportunityId = :testOpportunity.Id];
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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