+ Start a Discussion
Christine KleinChristine Klein 

EventTriggerHandler

When a user clicks on "New Event" from a lead, I would like to populate the campaign field on Events with the campaign that is listed on the Lead Record.  I'm running into an error where it says "Variable does not exist: whoid".  I can't seem to figure out why it would be complaining about whoid when it shows up under Workbench for events.

public with sharing class EventTriggerHandler 
{
	private boolean triggerIsExecuting = false;
    private integer batchSize = 0; 

 	public boolean isTriggerContext{
        get { return triggerIsExecuting;}
    }
    
    public boolean isVisualforcePageContext{
        get { return !isTriggerContext;}
    }
     
    public boolean isWebServiceContext{
        get { return !isTriggerContext;}
    }
    
    public boolean isExecuteAnonymousContext{
        get { return !isTriggerContext;}
    }

  	public EventTriggerHandler(boolean isExecuting, integer size){
    	triggerIsExecuting = isExecuting;
    	BatchSize = size;
  	}
    	
   	public void UpdateLastCampaignActivityOnEvents(Event[] newEvent)
  	{
  		Set<Id> whoIDs = new Set<ID>();
  		for(Event CurrentEvent : newEvent) 
    	{
    		WhoIds.add(CurrentEvent.whoid);
    	}
    	
    	if (whoIDs.size() > 0 )
    	{
    		list<Lead> CurrentLeads = [Select ID, Eloqua_Campaign_Name__c From Lead where ID IN :WhoIDs];
    		
    		map<id,Lead> Leads = new map<id,Lead>();
    		
    		if (currentLeads.size() > 0 )
    		{
    			for (Lead currentlead : currentleads)
    			{
    				Leads.put(currentLead.ID, currentLead);
    			}
    			
    			for(Event CurrentEvent : newEvent) 
		    	{
		    		Lead LeadLookup = Leads.get(CurrentEvent.WhoID); 
		    		if (LeadLookup <> null)
		    		{
		    			CurrentEvent.Last_Campaign_Activity__c = LeadLookup.Eloqua_Campaign_Name__C; 
		    		}
		    	}
    		}
    	}   	

  	}
 	
  	public void onBeforeInsert(Event[] newEvent)
  	{
  		UpdateLastCampaignActivityOnEvents(newEvent);
   	}
   
  
    /*
    @future public static void onAfterInsertAsync(Set<ID> newEventIDs){
    	
    }
    */
 	/*
 	public void OnAfterInsert(Event[] newEvents) {
 		
 		
 	}
 	*/
 	/*
 	public void onBeforeUpdate(Event[] oldEvent, Event[] updatedEvent, Map<ID, Event> oldEventMap) {

    }
    */
 	/*
    public void onAfterUpdate(Event[] oldEvent, Event[] updatedEvent, Map<ID, Event> oldEventMap) {
    		
    }
    */
  	/*  
    @future public static void onAfterUpdateAsync(Set<ID> updatedEvent){
    	
    }
    */
    /*
    public void onBeforeDelete(Event[] EventToDelete, Map<ID, Event> EventMap){
        
    }
    */
    /*
    public void onAfterDelete(Event[] deletedEvent, Map<ID, Event> EventMap){
    			
    }
    */
    
    /*
    @future public static void onAfterDeleteAsync(Set<ID> deletedEvent){
        //to make this work like the on after delete you need to do a select statement into 
        //a list where ID in :deletedOpportunitySplits, this would recreate the deletedOpportunitySplits list
        //from there the code would work the same.
    }
    */
    
    /*
    public void onUndelete(Event[] restoredEvent){
        
    }
    */ 
}


Best Answer chosen by Christine Klein
Kiran  KurellaKiran Kurella
Can you check your API version of the trigger and the class. Make sure they are using the latest API version 30.0

I also optimized your trigger method:

public void UpdateLastCampaignActivityOnEvents(Event[] newEvents) {

		map<Id, Lead> mapLeads = new map<Id, Lead>{};
		list<Event> events_To_Process = new list<Event>{};
		String leadKeyPrefix = Lead.sObjectType.getDescribe().getKeyPrefix();
		
		for (Event eve : newEvents) {
			// make sure the WhoId is a Lead record	
			if (eve.WhoId != null && String.valueOf(eve.WhoId).startsWith(leadKeyPrefix)) {
				mapLeads.put(eve.WhoId, null);
				events_To_Process.add(eve);
			}
		}

		if (!mapLeads.isEmpty()) {
			mapLeads.putAll([Select Id, Eloqua_Campaign_Name__c from Lead where Id in : mapLeads.keySet()]);

			for (Event eve : events_To_Process) {
				eve.Last_Campaign_Activity__c = mapLeads.get(eve.WhoId).Eloqua_Campaign_Name__c;
			}
		}
	}

 

All Answers

Kiran  KurellaKiran Kurella
Can you check your API version of the trigger and the class. Make sure they are using the latest API version 30.0

I also optimized your trigger method:

public void UpdateLastCampaignActivityOnEvents(Event[] newEvents) {

		map<Id, Lead> mapLeads = new map<Id, Lead>{};
		list<Event> events_To_Process = new list<Event>{};
		String leadKeyPrefix = Lead.sObjectType.getDescribe().getKeyPrefix();
		
		for (Event eve : newEvents) {
			// make sure the WhoId is a Lead record	
			if (eve.WhoId != null && String.valueOf(eve.WhoId).startsWith(leadKeyPrefix)) {
				mapLeads.put(eve.WhoId, null);
				events_To_Process.add(eve);
			}
		}

		if (!mapLeads.isEmpty()) {
			mapLeads.putAll([Select Id, Eloqua_Campaign_Name__c from Lead where Id in : mapLeads.keySet()]);

			for (Event eve : events_To_Process) {
				eve.Last_Campaign_Activity__c = mapLeads.get(eve.WhoId).Eloqua_Campaign_Name__c;
			}
		}
	}

 
This was selected as the best answer
Christine KleinChristine Klein
Thanks for your help :)

I checked the version of my trigger and class and they're both on version 31. 

Even with your optimized trigger method I still get the WhoId does not exist error on this line:  

mapLeads.put(eve.WhoId, null);

Kiran  KurellaKiran Kurella
I tried this in eclispe with api version 30 and it worked fine. Can you change your API version to 30.0 and try again?

Are you using any IDE for development or using the browser (developer console) for development? Try editing the code in eclipse or developer console and see if it makes a difference. Hope it will resolve your issue.
Christine KleinChristine Klein
I'm using eclipse.  The only version I have available when creating a new class/trigger is version 31.  I've updated the meta.xml files for both the trigger and trigger handler class so the API version is 30.  I even tried doing this with the developer console for version 31,30 and 29 and I keep getting the same error :-/
Kiran  KurellaKiran Kurella
hmm.. I am not sure what's going wrong. Make sure there is no space or any special characters in the following line. Remove the line and type it manually. If the problem still exists then post a screen shot of your eclipse code possibly with error.

mapLeads.put(eve.WhoId, null);
Christine KleinChristine Klein
It doesn't seem to like Event eve.  I commented out the following
 
if (eve.WhoId != null && String.valueOf(eve.WhoId).startsWith(leadKeyPrefix)) {
mapLeads.put(eve.WhoId, null);
 events_To_Process.add(eve);
}

and then I got another error on

eve.Last_Campaign_Activity__c = mapLeads.get(eve.WhoId).Eloqua_Campaign_Name__c;

saying Variable Last_Campaign_Activity__c does not exist.

User-added image
Christine KleinChristine Klein
public with sharing class EventsTriggerHandler {
 private boolean triggerIsExecuting = false;
    private integer batchSize = 0;

  public boolean isTriggerContext{
        get { return triggerIsExecuting;}
    }

    public boolean isVisualforcePageContext{
        get { return !isTriggerContext;}
    }

    public boolean isWebServiceContext{
        get { return !isTriggerContext;}
    }

    public boolean isExecuteAnonymousContext{
        get { return !isTriggerContext;}
    }

   public EventsTriggerHandler(boolean isExecuting, integer size){
     triggerIsExecuting = isExecuting;
     BatchSize = size;
   }
   
  public void UpdateLastCampaignActivityOnEvents(Event[] newEvents) 
  {
   map<Id, Lead> mapLeads = new map<Id, Lead>{};
   list<Event> events_To_Process = new list<Event>{};
   String leadKeyPrefix = Lead.sObjectType.getDescribe().getKeyPrefix();
   
   for (Event eve : newEvents) {
    // make sure the WhoId is a Lead record 
    if (eve.WhoId != null && String.valueOf(eve.WhoId).startsWith(leadKeyPrefix)) {
     mapLeads.put(eve.WhoId, null);
     events_To_Process.add(eve);
    }
   }
 
   if (!mapLeads.isEmpty()) {
    mapLeads.putAll([Select Id, Eloqua_Campaign_Name__c from Lead where Id in : mapLeads.keySet()]);
 
    for (Event eve : events_To_Process) {
     eve.Last_Campaign_Activity__c = mapLeads.get(eve.WhoId).Eloqua_Campaign_Name__c;
    }
   }
  }
    
   public void onBeforeInsert(Event[] newEvent){
  UpdateLastCampaignActivityOnEvents(newEvent);
    }
    
    /*
    @future public static void onAfterInsertAsync(Set<ID> newEventIDs){

    }
    */
  /*
  public void OnAfterInsert(List<Event> Events) {


  }
  */
  /*
  public void onBeforeUpdate(Event[] oldEvent, Event[] updatedEvent, Map<ID, Event> oldEventMap) {

    }
    */
  /*
    public void onAfterUpdate(Event[] oldEvent, Event[] updatedEvent, Map<ID, Event> oldEventMap) {

    }
    */
   /*  
    @future public static void onAfterUpdateAsync(Set<ID> updatedEvent){

    }
    */
    /*
    public void onBeforeDelete(Event[] EventToDelete, Map<ID, Event> EventMap){

    }
    */
    /*
    public void onAfterDelete(Event[] deletedEvent, Map<ID, Event> EventMap){

    }
    */

    /*
    @future public static void onAfterDeleteAsync(Set<ID> deletedEvent){
        //to make this work like the on after delete you need to do a select statement into 
        //a list where ID in :deletedOpportunitySplits, this would recreate the deletedOpportunitySplits list
        //from there the code would work the same.
    }
    */

    /*
    public void onUndelete(Event[] restoredEvent){

    }
    */
}

Christine KleinChristine Klein
trigger EventsTrigger on Event (after delete, after insert, after undelete, after update, before delete, before insert, before update) 
{
    EventsTriggerHandler handler = new EventsTriggerHandler(Trigger.isExecuting, Trigger.size);
    // calls that are commented out are not currently needed
    if(Trigger.isInsert && Trigger.isBefore){ 
     //   handler.onBeforeInsert(Trigger.new);
    }
    else if(Trigger.isInsert && Trigger.isAfter){
        //handler.onAfterInsert(Trigger.new);
    }
    else if(Trigger.isUpdate && Trigger.isBefore){
       // handler.onBeforeUpdate(Trigger.old, Trigger.new, Trigger.oldMap);
    }
    else if(Trigger.isUpdate && Trigger.isAfter){
       //handler.onAfterUpdate(Trigger.old, Trigger.new, Trigger.oldMap);
    }
    else if(Trigger.isDelete && Trigger.isBefore){
        //handler.onBeforeDelete(Trigger.old, Trigger.oldMap);
    }
    else if(Trigger.isDelete && Trigger.isAfter){ 
        //handler.onAfterDelete(Trigger.old, Trigger.oldMap); 
    }
    else if(Trigger.isUnDelete){
        //handler.onUndelete(Trigger.new);    
    }
}

Christine KleinChristine Klein
Just to show that the fields do exist under Event :)

User-added image
Christine KleinChristine Klein
So it turns out the issue was with another class that we had.  We had a class called Event so when I tried to instantiate the Event object, my trigger handler was getting confused thinking I was calling the Event class.

I didn't even think to check that as I didn't think Salesforce would let you create a class with the same name as an object.   Sigh.

Thanks for all your help!!