• Mitch Morrison
  • NEWBIE
  • 25 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 12
    Replies
Hello, I have a trigger that works for my test class, but in implementation, it doesn't work. When the field "YTD_sales" changes on a contact, the trigger should increment the field "Sales within 1 Year" for every event tied to the contact.
Right now, only some event values get updated, and it is especially problematic for multi-person events (which I tried addressing through event relations). The batch size of the import could be an issue (its 10,000)
Let me know if you have any thoughts! Thanks!
(I'll post the code below)
 
I am trying to update a custom event field, sales_within_1_year__c, when a custom contact field, sales__c, changes. For example:

Event 1: tied to Contact A & Contact B; sales_within_1_year = $0
Event 2: tied to Contact A & Contact C; sales_within_1_year = $10

Contact A sales changes from $100 to $200.
Event 1: new sales_within_1_year = $100
Event 2: new sales_within_1_year = $110

If the sales for Contact B or C changed, it would only affect the event they are tied to.

I thought my trigger would work, and it does when I change a contact's sales number individually. But when I bulk update contacts, none of the sales_within_1_year numbers change. I was wondering if someone could help me through this. Thanks!

Here is my trigger:

trigger EventSales on Contact (before update) {
                //Create a list of contact IDs that have updating sales
                List<Id> contactIds = new List<Id>();
    for(Contact con : trigger.new) {
        if(Trigger.newMap.get(con.Id).Sales__c == NULL) return;
        else if(trigger.newMap.get(con.Id).Sales__c == 0) return;
        else if(trigger.oldMap.get(con.Id).Sales__c == NULL) {
            contactIds.add(con.Id);
        }
        else if(trigger.newMap.get(con.Id).Sales__c !=
                trigger.oldMap.get(con.Id).Sales__c) {
            contactIds.add(con.Id);
        }
    }
   
    //Create a list of event relations (contact-event pairs) for updating contacts
    List<EventRelation> evRel = [SELECT EventId, RelationId
                                FROM EventRelation
                                WHERE RelationId IN :contactIds];
   
    //Create a map of the updating contacts and their related events
    Map<Id, List<Id>> contactEvents          = new Map<Id, List<Id>>();
   
    //Create a list of event Ids that are going to be updated
    List<Id> eventIds = new List<Id>();
   
    //Put the contact Id and event Id in the map for events that have updating contacts
    for(EventRelation rel :evRel) {
        if(contactEvents.containsKey(rel.RelationId)) {//If the contact is already in the map
            List<Id> relatedEv = contactEvents.get(rel.RelationId);//Grab the list of events tied to the contact
            relatedEv.add(rel.EventId);//Add the new event to the list
            contactEvents.put(rel.RelationId, relatedEv);//Put the updated list into the map
        }
        else {//If the contact isn't in the map
            contactEvents.put(rel.RelationId, new List<Id>{rel.EventId});//Put the contact and event into the map
        }
        //Add the updating event to the eventIds list
        if(eventIds.contains(rel.EventId)) return;
        else eventIds.add(rel.EventId);
    }
   
    //Create a list of events that are going to be updated
    List<Event> listEventsMaker = [SELECT Id, Sales_within_1_year__c
                                   FROM Event
                                   WHERE Id IN :eventIds
                                   AND EndDateTime = LAST_N_DAYS:365];
   
    List<Event> listEvents = new List<Event>();
    for(Event ev :listEventsMaker) {
        if(listEvents.contains(ev)) return;
        else listEvents.add(ev);
    }
   
    //Keep a list of events to update
    List<Event> changedEvents = new List<Event>();
   
    //Update the sales amounts for the events
    for(Id con :contactEvents.keySet()) {//For each contact in the map
        List<Id> thisContact = new List<Id>();//Create a list of events tied to this specific contact
        Contact oldCon = trigger.oldMap.get(con); //create a record for the contact pre-update
        Contact newCon = trigger.newMap.get(con); //create a record for the contact post-update
        if(newCon.Sales__c == NULL) return;
        else if(newCon.Sales__c == 0) return;
        else if(oldCon.Sales__c == NULL) {
            for(Id event :contactEvents.get(con)) {
                thisContact.add(event);
            }
            for(Event ev :listEventsMaker) {
                if(thisContact.contains(ev.Id)) {
                    changedEvents.add(ev);
                    if(ev.Sales_within_1_Year__c == NULL) {
                        ev.Sales_within_1_Year__c = newCon.Sales__c;
                    }
                    else ev.Sales_within_1_Year__c += newCon.Sales__c;
                }
            }
        }
        else if(oldCon.Sales__c
                != newCon.Sales__c) {
            for(Id event :contactEvents.get(con)) {
                thisContact.add(event);
            }
            for(Event ev :listEventsMaker) {
                if(thisContact.contains(ev.Id)) {
                    changedEvents.add(ev);
                    if(ev.Sales_within_1_Year__c == NULL) {
                        ev.Sales_within_1_year__c = (newCon.Sales__c
                                                     - oldCon.Sales__c);
                    }
                    else ev.Sales_within_1_Year__c += (newCon.Sales__c
                                                       - oldCon.Sales__c);
                }
            }
        }
    }
    update changedEvents;
}
 
Trying to compare an Events EndDateTime to today's date. Obviously, I need to convert EndDateTime to something that can be compared. I tried:

DateTime dt = Ev.EndDateTime();
Date EndDate = date.newInstance(dt.year(), dt.month(), dt.day());

And got this error: Method does not exist or incorrect signature: void EndDateTime() from the type Event

I want to get the difference between the Event date and today's date (so subtraction is needed). Thanks for any help!
Hi all, I have had some trouble trying to create a trigger that updates a field on related event records whenever a specific contact field is updated. I tried using a map of lists, but have found that a can't add an Event type as the value of the list. I wanted to make this map to link all updating contacts to their related events. Then, I would have to find a way to loop through the map, updating the event field. Here is my code:

Trigger IncrementValue on Contact (before update) {

    //Create a list of all updating contact IDs
    List<Id> contactIds = new List<Id>();
    for(Contact con : Trigger.new) {
        contactIds.add(con.Id);
    }
    
    //Create a map of all updating contact IDs and their related event IDs
    Map<Id, List<Event>> contactEvents = new Map<Id, List<Event>>();
    
    for(Event ev : [SELECT WhoId FROM Event WHERE WhoId IN :contactIds]) {
        if(contactEvents.containsKey(ev.WhoId)) {
            List<Event> eventId = contactEvents.get(ev.WhoId);
            eventId.add(ev.Activity_ID__c);
            contactEvents.put(ev.WhoId, eventId);
        }
        else {
            contactEvents.put(ev.WhoId, new List<Event> {ev.Activity_ID__c});
        }
    }
    
    List<Id> eventUpdate = new List<Id>();
    
    for(Contact con : Trigger.new) {
        if(contactEvents.containsKey(con.Id)) {
            return; //increment sales (Need help looping through map)
        }
    }
}

Here are my errors:

eventId.add(ev.Activity_ID__c); - Method does not exist or incorrect signature: void add(String) from the type List<Event>

contactEvents.put(ev.WhoId, new List<Event> {ev.Activity_ID__c}); - 
Initial expression is of incorrect type, expected: Event but was: String

Thank you for any help and will obviously answer any questions if my explanation was confusing!
Hello, I have a trigger that works for my test class, but in implementation, it doesn't work. When the field "YTD_sales" changes on a contact, the trigger should increment the field "Sales within 1 Year" for every event tied to the contact.
Right now, only some event values get updated, and it is especially problematic for multi-person events (which I tried addressing through event relations). The batch size of the import could be an issue (its 10,000)
Let me know if you have any thoughts! Thanks!
(I'll post the code below)
 
I am trying to update a custom event field, sales_within_1_year__c, when a custom contact field, sales__c, changes. For example:

Event 1: tied to Contact A & Contact B; sales_within_1_year = $0
Event 2: tied to Contact A & Contact C; sales_within_1_year = $10

Contact A sales changes from $100 to $200.
Event 1: new sales_within_1_year = $100
Event 2: new sales_within_1_year = $110

If the sales for Contact B or C changed, it would only affect the event they are tied to.

I thought my trigger would work, and it does when I change a contact's sales number individually. But when I bulk update contacts, none of the sales_within_1_year numbers change. I was wondering if someone could help me through this. Thanks!

Here is my trigger:

trigger EventSales on Contact (before update) {
                //Create a list of contact IDs that have updating sales
                List<Id> contactIds = new List<Id>();
    for(Contact con : trigger.new) {
        if(Trigger.newMap.get(con.Id).Sales__c == NULL) return;
        else if(trigger.newMap.get(con.Id).Sales__c == 0) return;
        else if(trigger.oldMap.get(con.Id).Sales__c == NULL) {
            contactIds.add(con.Id);
        }
        else if(trigger.newMap.get(con.Id).Sales__c !=
                trigger.oldMap.get(con.Id).Sales__c) {
            contactIds.add(con.Id);
        }
    }
   
    //Create a list of event relations (contact-event pairs) for updating contacts
    List<EventRelation> evRel = [SELECT EventId, RelationId
                                FROM EventRelation
                                WHERE RelationId IN :contactIds];
   
    //Create a map of the updating contacts and their related events
    Map<Id, List<Id>> contactEvents          = new Map<Id, List<Id>>();
   
    //Create a list of event Ids that are going to be updated
    List<Id> eventIds = new List<Id>();
   
    //Put the contact Id and event Id in the map for events that have updating contacts
    for(EventRelation rel :evRel) {
        if(contactEvents.containsKey(rel.RelationId)) {//If the contact is already in the map
            List<Id> relatedEv = contactEvents.get(rel.RelationId);//Grab the list of events tied to the contact
            relatedEv.add(rel.EventId);//Add the new event to the list
            contactEvents.put(rel.RelationId, relatedEv);//Put the updated list into the map
        }
        else {//If the contact isn't in the map
            contactEvents.put(rel.RelationId, new List<Id>{rel.EventId});//Put the contact and event into the map
        }
        //Add the updating event to the eventIds list
        if(eventIds.contains(rel.EventId)) return;
        else eventIds.add(rel.EventId);
    }
   
    //Create a list of events that are going to be updated
    List<Event> listEventsMaker = [SELECT Id, Sales_within_1_year__c
                                   FROM Event
                                   WHERE Id IN :eventIds
                                   AND EndDateTime = LAST_N_DAYS:365];
   
    List<Event> listEvents = new List<Event>();
    for(Event ev :listEventsMaker) {
        if(listEvents.contains(ev)) return;
        else listEvents.add(ev);
    }
   
    //Keep a list of events to update
    List<Event> changedEvents = new List<Event>();
   
    //Update the sales amounts for the events
    for(Id con :contactEvents.keySet()) {//For each contact in the map
        List<Id> thisContact = new List<Id>();//Create a list of events tied to this specific contact
        Contact oldCon = trigger.oldMap.get(con); //create a record for the contact pre-update
        Contact newCon = trigger.newMap.get(con); //create a record for the contact post-update
        if(newCon.Sales__c == NULL) return;
        else if(newCon.Sales__c == 0) return;
        else if(oldCon.Sales__c == NULL) {
            for(Id event :contactEvents.get(con)) {
                thisContact.add(event);
            }
            for(Event ev :listEventsMaker) {
                if(thisContact.contains(ev.Id)) {
                    changedEvents.add(ev);
                    if(ev.Sales_within_1_Year__c == NULL) {
                        ev.Sales_within_1_Year__c = newCon.Sales__c;
                    }
                    else ev.Sales_within_1_Year__c += newCon.Sales__c;
                }
            }
        }
        else if(oldCon.Sales__c
                != newCon.Sales__c) {
            for(Id event :contactEvents.get(con)) {
                thisContact.add(event);
            }
            for(Event ev :listEventsMaker) {
                if(thisContact.contains(ev.Id)) {
                    changedEvents.add(ev);
                    if(ev.Sales_within_1_Year__c == NULL) {
                        ev.Sales_within_1_year__c = (newCon.Sales__c
                                                     - oldCon.Sales__c);
                    }
                    else ev.Sales_within_1_Year__c += (newCon.Sales__c
                                                       - oldCon.Sales__c);
                }
            }
        }
    }
    update changedEvents;
}
 
Hi all, I have had some trouble trying to create a trigger that updates a field on related event records whenever a specific contact field is updated. I tried using a map of lists, but have found that a can't add an Event type as the value of the list. I wanted to make this map to link all updating contacts to their related events. Then, I would have to find a way to loop through the map, updating the event field. Here is my code:

Trigger IncrementValue on Contact (before update) {

    //Create a list of all updating contact IDs
    List<Id> contactIds = new List<Id>();
    for(Contact con : Trigger.new) {
        contactIds.add(con.Id);
    }
    
    //Create a map of all updating contact IDs and their related event IDs
    Map<Id, List<Event>> contactEvents = new Map<Id, List<Event>>();
    
    for(Event ev : [SELECT WhoId FROM Event WHERE WhoId IN :contactIds]) {
        if(contactEvents.containsKey(ev.WhoId)) {
            List<Event> eventId = contactEvents.get(ev.WhoId);
            eventId.add(ev.Activity_ID__c);
            contactEvents.put(ev.WhoId, eventId);
        }
        else {
            contactEvents.put(ev.WhoId, new List<Event> {ev.Activity_ID__c});
        }
    }
    
    List<Id> eventUpdate = new List<Id>();
    
    for(Contact con : Trigger.new) {
        if(contactEvents.containsKey(con.Id)) {
            return; //increment sales (Need help looping through map)
        }
    }
}

Here are my errors:

eventId.add(ev.Activity_ID__c); - Method does not exist or incorrect signature: void add(String) from the type List<Event>

contactEvents.put(ev.WhoId, new List<Event> {ev.Activity_ID__c}); - 
Initial expression is of incorrect type, expected: Event but was: String

Thank you for any help and will obviously answer any questions if my explanation was confusing!