1

I'm trying to insert a new record into an object where I pull information from OpportunityLineItem, Product, Opportunity, and Account. I did a query and get the results I want. Then I go to create a new record with the results of the query and I get the error:

Variable does not exist: oli.Opportunity.AccountId

oli is the name I used in the for loop to search for OpportunityLineItem records, what am I doing wrong? In other variable does not exist posts it references when you create a variable and then in a different method try and reference that variable again. This is all in the same method so I am confused.

Example:

public with sharing class RevenuePipelineHandler {
    public static void createRevPipeline(Set<Id> oppyIds) {
        List<Revenue_Pipeline__c> revPipeline = new List<Revenue_Pipeline__c>();

        for(OpportunityLineItem oli:[SELECT Start_Date__c, 
                                            End_Date__c,
                                            TotalPrice,
                                            Is_Fund__c,
                                            OpportunityId,
                                            Opportunity.AccountId,
                                            Opportunity.Account.Account_BU_2__c,
                                            Opportunity.CurrencyIsoCode,
                                            Opportunity.Practice__c,
                                            Opportunity.Cash_or_Barter__c,
                                            Opportunity.StageName,
                                            Opportunity.Sales_Order_Approved_Date__c,
                                            Product2Id,
                                            Product2.ALR_Retailer__c,
                                            Product2.Product_LOB__c,
                                            Product2.Product_Type__c,
                                            Product2.Family,
                                            Product2.Product_Group__c,
                                            Product2.Product_Offering__c
                                            FROM OpportunityLineItem WHERE OpportunityId IN :oppyIds])

        {
            revPipeline.add(new Revenue_Pipeline__c(
                Account__c = oli.Opportunity.AccountId,
                ALR_Retailer__c = oli.Product2.ALR_Retailer__c,
                Cash_Barter__c = oli.Opportunity.Cash_or_Barter__c,
                Cross_Practice__c = oli.Opportunity.Account.Account_BU_2__c,
                CurrencyIsoCode = oli.Opportunity.CurrencyIsoCode,
                External_Id__c = oli.Product2.Family + '/' + oli.Product2.Product_Group__c + '/' + oli.Product2.Product_Offering__c,
                Is_Fund__c = oli.Is_Fund__c,
                Name = oli.OpportunityId + '-' + oli.Product2Id,
                Opportunity__c = oli.OpportunityId,
                Practice__c = oli.Opportunity.Practice__c,
                Product_AU__c = oli.Product2.Product_LOB__c,
                Product_End_Date__c = oli.End_Date__c,
                Product_Price__c = oli.TotalPrice,
                Product_Start_Date__c = oli.Start_Date__c,
                Product__c = oli.Product2Id,
                Revenue_Type__c = 'DANDAN',
                Subscription_Type__c = oli.Product2.Product_Type__c));
        }
        insert revPipeline;     
    }
}

1 Answer 1

1

Figured it out with help from this post: Compile Error: Variable does not exist: o.CloseDate

I had a newline/line break between the line between FROM and {:

FROM OpportunityLineItem WHERE OpportunityId IN :oppyIds])

    {
        revPipeline.add(new Revenue_Pipeline__c(

The correct code is:

    FROM OpportunityLineItem WHERE OpportunityId IN :oppyIds])
    {
        revPipeline.add(new Revenue_Pipeline__c(

So if only the first line is considered in the loop and there is a blank space then the rest is out of the loop and thus error. Removing the space allows the code to be compiled.

3
  • I edited your answer to change space to newline/line break because there is a difference (and I was looking at the wrong place in your examples here when I thought you meant a plain-old space)
    – Derek F
    Commented Nov 1, 2016 at 20:39
  • 1
    The compiler is whitespace agnostic...
    – Adrian Larson
    Commented Nov 1, 2016 at 20:55
  • I use sublime text 3. Maybe that was the issue? Once I removed the line break it compiled and worked. Commented Nov 1, 2016 at 22:30

You must log in to answer this question.

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