0

This is the code I am using:

trigger ParentOppToChild on Opportunity (after update)
{

    List <Opportunity> oppQueryResults = new List <Opportunity>([Select ID, (Select ID, Dealership__c From OpportunityLineItems) From Opportunity]);

    Map<ID,OpportunityLineItem> accountTolineItems = New Map<ID,OpportunityLineItem>();
    for(Opportunity o : oppQueryResults)
    {
        if(o.StageName = 'Closed Won')
        {
            for(OpportunityLineItem oli : o.OpportunityLineItems)
            {

                accountToLineItems.put(oli.Dealership__c, new OpportunityLineItem{oli});

                if(accountToLineItems.containsKey(oli.Dealership__c))
                {
                    accountToLineItems.containsKey(oli.Dealership__c).add(oli);
                }
                else
                {
                    accountToLineItems.put(oli.Dealership__c,New OpportunityLineItem[]{oli});
                }
            }
        }

    }   
}

And this is the error that I am getting: unexpected token: '{' at line 21

Sorry if this is really obvious but I have changed the code around so many times because of this and I can't seem to find an answer elsewhere.

3
  • Which line is line 21 ?
    – BarCotter
    Aug 24, 2015 at 12:51
  • @BarCotter accountToLineItems.put(oli.Dealership__c, new OpportunityLineItem{oli});
    – Nik
    Aug 24, 2015 at 13:47
  • If you want dealership to be the key and the oli variable to be the value then you should be doing the following: accountToLineItems.put(oli.Dealership__c, oli);
    – BarCotter
    Aug 24, 2015 at 14:25

2 Answers 2

0

Instead use this on line 21 :

accountToLineItems.put(oli.Dealership__c,New list<OpportunityLineItem>{oli});

Also look this link : https://developer.salesforce.com/forums/ForumsMain?id=906F0000000913SIAQ

3
  • This gives an error: Incompatible value type List<OpportunityLineItem> for Map<Id,OpportunityLineItem>
    – Nik
    Aug 24, 2015 at 13:46
  • Have you included {oli} or just list<OpportunityLineItem>() because accountToLineItems is map of ID and Opp Line Item object (not list of object). Please verify. :) Aug 24, 2015 at 14:20
  • I just pasted that line of code you suggested in place of the line I had and I got that error. Now I feel like I might be approaching this the wrong way :/
    – Nik
    Aug 24, 2015 at 14:31
0

Not sure about your complete use case.. you shouldn't be querying all the opportunities and its line items in your trigger.. you should just limit the opportunity records that got updated.

but to the error, you should change

accountToLineItems.put(oli.Dealership__c, new OpportunityLineItem{oli});

to

accountToLineItems.put(oli.Dealership__c, new List<OpportunityLineItem>());

but you are doing this initialization inside the for loop which is not required since the next if.. else.. part takes care of the initialization as well..

and also you should change this line as well

accountToLineItems.containsKey(oli.Dealership__c).add(oli);

to

accountToLineItems.get(oli.Dealership__c).add(oli);

so, your overall code will look like this

trigger ParentOppToChild on Opportunity (after update)
{

    List <Opportunity> oppQueryResults = new List <Opportunity>([Select ID, (Select ID, Dealership__c From OpportunityLineItems) From Opportunity]);

    Map<ID,OpportunityLineItem> accountTolineItems = New Map<ID,OpportunityLineItem>();
    for(Opportunity o : oppQueryResults)
    {
        if(o.StageName = 'Closed Won')
        {
            for(OpportunityLineItem oli : o.OpportunityLineItems)
            {

                if(accountToLineItems.containsKey(oli.Dealership__c))
                {
                    accountToLineItems.get(oli.Dealership__c).add(oli);
                }
                else
                {
                    accountToLineItems.put(oli.Dealership__c, new List<OpportunityLineItem>());
                }
            }
        }

    }   
}
3
  • I need to get a related list of line items and each line items dealership(dealership__c) is this not the right way to do it?
    – Nik
    Aug 24, 2015 at 12:52
  • I hv updated my answer.. thou this will get thru the compile error, I am not sure about your complete business logic & what exactly you are trying to achieve in this trigger. Aug 24, 2015 at 13:25
  • Can you check my last question to see what it is I need to do? No one has provided any clarification on that. That will explain what I am trying to do. Thanks for your help on this but it says there is an error now on line 23: Method does not exist or incorrect signature: [OpportunityLineItem].add(OpportunityLineItem)
    – Nik
    Aug 24, 2015 at 13:38

You must log in to answer this question.

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