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
CarlaMartinez.ax1388CarlaMartinez.ax1388 

Trigger - Auto-Update of lookup fields based on picklist value selection on custom object

I am pretty new to APEX and with help wrote a trigger to auto-update a lookup field based on selecting a value on a picklist field on the account object. It works fine on the Account object.  Now I am trying to replictae it on a custom object and the code below is giving me errors.

 

Error: Compile Error: unexpected token: 'Map" at line 1 column 14



public static Map<String, List<Primary_Sales_Rep__c>> filterPrimarySalesRepsWithChangedVerticals(List<Primary_Sales_Rep__c> incomingPrimarySalesReps,
Map<Id, PrimarySalesRep__c>oldPrimarySalesRepIdToOldPrimarySalesRep)
{

Map<String, List<PrimarySalesRep__c>> verticalNametoPrimarySalesRepList = new Map<String, List<PrimarySalesRep__c>>();

//For all PrimarySalesRep that were sent to out Trigger
for(PrimarySalesRep__c incomingPrimarySalesRep : incomingPrimarySalesReps)
{
//Change conditions
//1.Vertical is now being set
//2. Vertical is being changed

Boolean isChanged
Boolean isInsert - oldPrimarySalesRepIdToOldPrimarySalesRep == null;
PrimarySalesRep__c oldPrimarySalesRep = isInsert ? null : oldPrimarySalesRepIdToOldPrimarySalesRep.get(incomingPrimarySalesRep.Id);

isChanged = (( isInsert && incomingPrimarySalesRep__c.Vertical_Major__c != null)
||(!isInsert && oldPrimarySalesRep__c.Vertical_Major__c != incomingPrimarySalesRep__c.Vertical_Major__c));

if(isChanged)
{
if(!verticalNameToPrimarySalesRepList.containsKey( incomingPrimarySalesRep__c.Vertical_Major__c))
verticalNameToPrimarySalesrepList.put(incomingPrimarySalesRep__c.Vertical_Major__c, new List<PrimarySalesRep__c>));

verticalNameToPrimarySalesrepList.get( incomingPrimarySalesrep__c.Vertical_Major__c).add(incomingPrimarySalesrep__c);
}

}
return verticalNametoPrimarySalesRepList;
}

 

Andy BoettcherAndy Boettcher

Can you post your entire trigger (helps for context)

 

-Andy

CarlaMartinez.ax1388CarlaMartinez.ax1388
trigger UpdateMajorVerticals on PrimarySalesRep (after insert, after update) {
    
    PrimarySalesRepServices.setMajor_Verticals__c( PrimarySalesRepServices.filterPrimarySalesRepsWithChangedVerticals( Trigger.New, Trigger.oldMap ) );
    
}

 

 

craigmhcraigmh

This line:

 

public static Map<String, List<Primary_Sales_Rep__c>> filterPrimarySalesRepsWithChangedVerticals(List<Pr​imary_Sales_Rep__c> incomingPrimarySalesReps,
Map<Id, PrimarySalesRep__c>oldPrimarySalesRepIdToOldPrimar​ySalesRep)

 

Is that copied correctly?

CarlaMartinez.ax1388CarlaMartinez.ax1388

Yes, I think so

Andy BoettcherAndy Boettcher

Judging on how you're calling your method from the trigger, your method declaration is off.  You have it asking for a List and Map, but you're passing it two Maps in the trigger.

 

public static Map<String, List<Primary_Sales_Rep__c>> filterPrimarySalesRepsWithChangedVerticals(List<Primary_Sales_Rep__c> incomingPrimarySalesReps,
Map<Id, PrimarySalesRep__c>oldPrimarySalesRepIdToOldPrimarySalesRep)

To modify your class to take the parameters from the trigger, you would change your first variable to a MAP instead of a LIST.

 

public static Map<String, List<Primary_Sales_Rep__c>> filterPrimarySalesRepsWithChangedVerticals(Map<Id, Primary_Sales_Rep__c> incomingPrimarySalesReps,
Map<Id, PrimarySalesRep__c>oldPrimarySalesRepIdToOldPrimarySalesRep)

-Andy