function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Travis WrightTravis Wright 

expecting a semi-colon, found 'oppIdSet'

Hey Guys need another set of eyes on this as I am not sure why this is happening. 

trigger Projectrollup on OpportunityLineItem (after insert, after update, before delete)

    Set oppIdSet = new Set();

    if(Trigger.isAfter)
        for(OpportunityLineItem oli : Trigger.new)
            oppIdSet.add(oli.OpportunityId);
    if(Trigger.isBefore)
        for(OpportunityLineItem oli : Trigger.old)
            oppIdSet.add(oli.OpportunityId);
    
    Map oppOldMap = new Map([Select Id, Implementation_Team__c, Training_Team__c From Opportunity Where Id IN :oppIdSet]);

    Set oppImplementationTeamList = new Set();
    Set oppTrainingTeamList = new Set();
        
    List oliList = [Select UnitPrice, Quantity, PricebookEntry.Product2Id, TotalPrice,PricebookEntry.Product2.Name, Description,
        Converted_to_Asset__c,Asset__c,PricebookEntry.Product2.Create_Asset__c, PricebookEntry.Product2.Sales_Order_Group__c, OpportunityId From OpportunityLineItem where 
        OpportunityId IN : oppIdSet And (PricebookEntry.Product2.Product_Reporting_Category__c = 'Services' or PricebookEntry.Product2.Name LIKE '%Communications%')];

    for(OpportunityLineItem oli : oliList)
    {
        if(oli.PricebookEntry.Product2.Sales_Order_Group__c == 'Hotline Services' || oli.PricebookEntry.Product2.Sales_Order_Group__c == 'Policy Management Services' || oli.PricebookEntry.Product2.Sales_Order_Group__c == 'Incident Management Services')
        {
            oppImplementationTeamList.add(oli.OpportunityId);
        }
        if(oli.PricebookEntry.Product2.Sales_Order_Group__c == 'ReadyTraining Services')
        {
            oppTrainingTeamList.add(oli.OpportunityId);
        }
    }
    
    Map updateMap = new Map();
    
    for(Id oppId : oppIdSet)
    {
        Opportunity opp = new Opportunity(Id = oppId, Implementation_Team__c = false, Training_Team__c = false);
        updateMap.put(oppId, opp);
    }
    
    for(Id oppId : oppImplementationTeamList)
    {
        Opportunity opp = updateMap.get(oppId);
        opp.Implementation_Team__c = true;
    }
    
    for(Id oppId : oppTrainingTeamList)
    {
        Opportunity opp = updateMap.get(oppId);
        opp.Training_Team__c = true;
    }
    
    for(Opportunity oppOld : oppOldMap.values())
    {
        Opportunity oppNew = updateMap.get(oppOld.Id);
        if(oppNew.Implementation_Team__c == oppOld.Implementation_Team__c && oppNew.Training_Team__c == oppOld.Training_Team__c)
            updateMap.remove(oppOld.Id);
    }
    
    List updateList = updateMap.values();
    
    if(updateList.size()>0)
        update updateList;
}
Best Answer chosen by Travis Wright
Amit Chaudhary 8Amit Chaudhary 8
Hi Travis,

In List Map and set you are not using <Type> That is why you are getting error.

Please try below code. I hope that will resolve your issue.
trigger Projectrollup on OpportunityLineItem (after insert, after update, before delete)
{ 
    Set<String> oppIdSet = new Set<String>();
	
    if(Trigger.isAfter)
	{
        for(OpportunityLineItem oli : Trigger.new)
            oppIdSet.add(oli.OpportunityId);
	}		
    if(Trigger.isBefore)
	{
        for(OpportunityLineItem oli : Trigger.old)
            oppIdSet.add(oli.OpportunityId);
	}
	
    Map<Id,Opportunity> oppOldMap = new Map<Id,Opportunity>([Select Id, Implementation_Team__c, Training_Team__c From Opportunity Where Id IN :oppIdSet]);

    Set<String> oppImplementationTeamList = new Set<String>();
    Set<String> oppTrainingTeamList = new Set<String>();
        
    List<OpportunityLineItem> oliList = [Select UnitPrice, Quantity, PricebookEntry.Product2Id, TotalPrice,PricebookEntry.Product2.Name, Description,
        Converted_to_Asset__c,Asset__c,PricebookEntry.Product2.Create_Asset__c, PricebookEntry.Product2.Sales_Order_Group__c, OpportunityId From OpportunityLineItem where OpportunityId IN : oppIdSet And (PricebookEntry.Product2.Product_Reporting_Category__c = 'Services' or PricebookEntry.Product2.Name LIKE '%Communications%')];

    for(OpportunityLineItem oli : oliList)
    {
        if(oli.PricebookEntry.Product2.Sales_Order_Group__c == 'Hotline Services' || oli.PricebookEntry.Product2.Sales_Order_Group__c == 'Policy Management Services' || oli.PricebookEntry.Product2.Sales_Order_Group__c == 'Incident Management Services')
        {
            oppImplementationTeamList.add(oli.OpportunityId);
        }
        if(oli.PricebookEntry.Product2.Sales_Order_Group__c == 'ReadyTraining Services')
        {
            oppTrainingTeamList.add(oli.OpportunityId);
        }
    }
    
    Map<Id,Opportunity> updateMap = new Map<Id,Opportunity>();
    
    for(Id oppId : oppIdSet)
    {
        Opportunity opp = new Opportunity(Id = oppId, Implementation_Team__c = false, Training_Team__c = false);
        updateMap.put(oppId, opp);
    }
    
    for(Id oppId : oppImplementationTeamList)
    {
        Opportunity opp = updateMap.get(oppId);
        opp.Implementation_Team__c = true;
    }
    
    for(Id oppId : oppTrainingTeamList)
    {
        Opportunity opp = updateMap.get(oppId);
        opp.Training_Team__c = true;
    }
    
    for(Opportunity oppOld : oppOldMap.values())
    {
        Opportunity oppNew = updateMap.get(oppOld.Id);
        if(oppNew.Implementation_Team__c == oppOld.Implementation_Team__c && oppNew.Training_Team__c == oppOld.Training_Team__c)
            updateMap.remove(oppOld.Id);
    }
    
    List<Opportunity> updateList = updateMap.values();
    
    if(updateList.size()>0)
        update updateList;
}

Please let us know if this will help you

All Answers

Himanshu ParasharHimanshu Parashar
Line no?
Himanshu ParasharHimanshu Parashar
Hi Travis,

issue is with init of different collection items. For example check the set initialisation 

it should be

set<type> variable name = new set<type>();


Same with the Map

Here is the the detailed document 

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_set.htm#apex_methods_system_set
Amit Chaudhary 8Amit Chaudhary 8
Hi Travis,

In List Map and set you are not using <Type> That is why you are getting error.

Please try below code. I hope that will resolve your issue.
trigger Projectrollup on OpportunityLineItem (after insert, after update, before delete)
{ 
    Set<String> oppIdSet = new Set<String>();
	
    if(Trigger.isAfter)
	{
        for(OpportunityLineItem oli : Trigger.new)
            oppIdSet.add(oli.OpportunityId);
	}		
    if(Trigger.isBefore)
	{
        for(OpportunityLineItem oli : Trigger.old)
            oppIdSet.add(oli.OpportunityId);
	}
	
    Map<Id,Opportunity> oppOldMap = new Map<Id,Opportunity>([Select Id, Implementation_Team__c, Training_Team__c From Opportunity Where Id IN :oppIdSet]);

    Set<String> oppImplementationTeamList = new Set<String>();
    Set<String> oppTrainingTeamList = new Set<String>();
        
    List<OpportunityLineItem> oliList = [Select UnitPrice, Quantity, PricebookEntry.Product2Id, TotalPrice,PricebookEntry.Product2.Name, Description,
        Converted_to_Asset__c,Asset__c,PricebookEntry.Product2.Create_Asset__c, PricebookEntry.Product2.Sales_Order_Group__c, OpportunityId From OpportunityLineItem where OpportunityId IN : oppIdSet And (PricebookEntry.Product2.Product_Reporting_Category__c = 'Services' or PricebookEntry.Product2.Name LIKE '%Communications%')];

    for(OpportunityLineItem oli : oliList)
    {
        if(oli.PricebookEntry.Product2.Sales_Order_Group__c == 'Hotline Services' || oli.PricebookEntry.Product2.Sales_Order_Group__c == 'Policy Management Services' || oli.PricebookEntry.Product2.Sales_Order_Group__c == 'Incident Management Services')
        {
            oppImplementationTeamList.add(oli.OpportunityId);
        }
        if(oli.PricebookEntry.Product2.Sales_Order_Group__c == 'ReadyTraining Services')
        {
            oppTrainingTeamList.add(oli.OpportunityId);
        }
    }
    
    Map<Id,Opportunity> updateMap = new Map<Id,Opportunity>();
    
    for(Id oppId : oppIdSet)
    {
        Opportunity opp = new Opportunity(Id = oppId, Implementation_Team__c = false, Training_Team__c = false);
        updateMap.put(oppId, opp);
    }
    
    for(Id oppId : oppImplementationTeamList)
    {
        Opportunity opp = updateMap.get(oppId);
        opp.Implementation_Team__c = true;
    }
    
    for(Id oppId : oppTrainingTeamList)
    {
        Opportunity opp = updateMap.get(oppId);
        opp.Training_Team__c = true;
    }
    
    for(Opportunity oppOld : oppOldMap.values())
    {
        Opportunity oppNew = updateMap.get(oppOld.Id);
        if(oppNew.Implementation_Team__c == oppOld.Implementation_Team__c && oppNew.Training_Team__c == oppOld.Training_Team__c)
            updateMap.remove(oppOld.Id);
    }
    
    List<Opportunity> updateList = updateMap.values();
    
    if(updateList.size()>0)
        update updateList;
}

Please let us know if this will help you
This was selected as the best answer
Travis WrightTravis Wright
Amit,

Is there any way that I can call this trigger to run when ever the opportunity is edited? It is working but the functionality is not exactly what I was looking for. As when the Sales Reps delete the OLI it will fire but if they delete the product that is driving this trigger last the opportunity won't update. 

I first tried to write the trigger on the opportunity to call and @future Class but couldn't get it to uncheck the box when the OLI was removed. 

Thanks again for correcting my mistake but additional help would be great. 
Amit Chaudhary 8Amit Chaudhary 8
Can you please explain your requirement with some example.
Travis WrightTravis Wright
Sure I have 3 fields on the opportunity which allows me to write logic in an integration to other system, OpenAir. I need these fields to be updated when a product matching the departments requirements are met, 2 of them are writen in the code above. When a Sales Rep selects these products it updates the opportunity to tell me which implementation department to create a project for. During the sales Process they might delete the products meaning that I have to update the opportunity to reflect the change. Here is an example,

The following products are selected when the opportunity is created, Tier 1 Standard Hotline Implementation, Training Implementation (1 Course), Domestic Hotline, and 1 Training Course (1 Year).

During the discovery process the sales rep finds out that the Prospect is only intrested in the Hotline and not the training product. If the sales rep Deletes the 1 Training Course (1 Year) and then deletes Training Implementation (1 Course) the opportunity doesn't update to show that the training_Team__c = false. If the sales rep reverses the order in which they delete the products it works fine. But being that this is for Sales I can expect them to follow this kind of process. 

Idealy I would be writing a trigger that will always check the products on the opportunity when Stage = Closed Won and update these field according to the products selected. 

I hope that helps let me know if you need additional details.