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
jaw999jaw999 

Relating Contacts from Invitees via Trigger

I want to dumb down the multi person calendar events in Salesforce so a Salesperson can go to the invitees on the bottom and just enter all the people - users and contacts in one place.

If one adds contacts there, the event does not show in the Contact's related list. The EventRelation field IsParent defaults to FALSE, but updating it to TRUE makes this appear in the Contact's related lists.

Are there other considerations I am missing?

My trigger is below. One thing I can't do yet is limit my query to related contacts. Can't use LIKE '003%' in the id query.

Also, it doesn't seem to be updating. Am I missing the update...


 
trigger EventAttendeeCollector2 on Event (after insert, after update) {

    For (Event EVT:trigger.new){

        List <eventRelation> Erelates =[select RelationId from eventRelation where eventID=: EVT.id and IsInvitee = TRUE and IsParent=False]; 

        system.debug('ERELATES______________________________________'+ERELATES);

       for (eventRelation Link:Erelates){
           link.isparent=true;
           }

    }

}

 
Abhishek BansalAbhishek Bansal
Hi,

As salesforce best practices does not allow us to use SOQL inside the for loop so i have modified your trigger code and you are also missing an Update statement.
Please replace your trigger code from below code :
 
trigger EventAttendeeCollector2 on Event (after insert, after update) {
	List<EventRelation> eventRelList = new List<EventRelation>();
	eventRelList = [Select RelationId from EventRelation where eventID IN :trigger.new and IsInvitee = TRUE and IsParent=False];
	
	for(EventRelation rel : eventRelList){
		rel.IsParent = true;
	}
	update eventRelList;
}

Let me know if you have any issues with this.

Thanks,
Abhishek
jaw999jaw999
Thanks! Cleaner.
Do you know how I'd filter out to only query Contact EventRelation and not Users?
jaw999jaw999
It did not update the IsParent value. It finds them, it lists to DML rows, but the value doesn't change. hmm
Abhishek BansalAbhishek Bansal
I did not get your point.
Can you please explain it more clearly ?
jaw999jaw999
It's not doing any change to the EventRelation. They retain the value of FALSE for IsParent.
jaw999jaw999
To investigate, I added another debug after the update eventRelList; line. It shows:
 
08:38:17.272 (272504803)|USER_DEBUG|[14]|DEBUG|ERELATES__________updated?____________________________(EventRelation:{RelationId=003J000001FHLgFIAX, Id=0REJ00000026BZUOA2, IsParent=true}, EventRelation:{RelationId=003J000001FHLgUIAX, Id=0REJ00000026BZTOA2, IsParent=true})

So isParent = True. But when I do an export of the EventRelations using the Data Loader IsParent=false. Is something not being committed?
Abhishek BansalAbhishek Bansal
There may be some triggers on Event Relation which are running after this debug statement.
So please go through the debug log from top to bottom and check which triggers are running after this debug statement.
Your Event Relation is being updated once againg during the same context tht is why it is showing still false.

Please go through your debug log once again.

Abhishek.
jaw999jaw999
There is a trigger that updates the EVent after, which then re-runs this trigger. In the debug for this second run, the first query finds no records. But no updates are made to eventRelations, only to the Event and certain parents. Also, this only runs on update, not on insert.
jaw999jaw999
And to be extra clear, I deactivated all EVent triggers so only this one runs and I get the same non update result every update. Debug logs show 2 rows returned, then show the IsParent is True.
10:00:53.238 (238584549)|USER_DEBUG|[6]|DEBUG|ERELATES______________________________________(EventRelation:{RelationId=003J000001FHLgFIAX, Id=0REJ00000026BZUOA2}, EventRelation:{RelationId=003J000001FHLgUIAX, Id=0REJ00000026BZTOA2})
10:00:53.238 (238592027)|SYSTEM_METHOD_EXIT|[6]|System.debug(ANY)
(debug truncated)
10:00:53.264 (264983049)|SYSTEM_METHOD_ENTRY|[14]|System.debug(ANY)
10:00:53.264 (264995148)|USER_DEBUG|[14]|DEBUG|ERELATES__________updated?____________________________(EventRelation:{RelationId=003J000001FHLgFIAX, Id=0REJ00000026BZUOA2, IsParent=true}, EventRelation:{RelationId=003J000001FHLgUIAX, Id=0REJ00000026BZTOA2, IsParent=true})
10:00:53.265 (265008250)|SYSTEM_METHOD_EXIT|[14]|System.debug(ANY)
10:00:53.265 (265036810)|CUMULATIVE_LIMIT_USAGE
10:00:53.265 (265036810)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 1 out of 100
  Number of query rows: 2 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 1 out of 150
  Number of DML rows: 2 out of 10000
Abhishek BansalAbhishek Bansal
It would be good if you can contact me on gmail or skype :
Gmail : abhishek.bansal@metacube.com
SkypeId : abhishek.bansal2790

The solution can not be figure out from conversation over here,
So if it is possible for you than please contact me personally.

Thanks,
Abhishek
jaw999jaw999
Thanks for your help. It appears to be possible per this page: https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_eventattendee.htm (https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_eventattendee.htm" target="_blank)

But we're seeing something stop it.
Trying it with their LIMIT of 1 doesn't make a difference.

 
jaw999jaw999
How about pushing the action into an @future method? 
Trigger:
 
trigger EventAttendeeContactConnectorPusher on Event (before insert) {
    
     List<EventRelation> eventRelList = [SELECT Id, isParent, isInvitee FROM EventRelation WHERE EventId IN :trigger.new 
                                         AND isInvitee = true and isParent = false];
    
  	Set<Id> eventRelSet = (new Map<Id,EventRelation>(eventRelList)).keySet();
    EventAttendeeContactConnectorHandler.EventAttendeeContactConnector(eventRelSet);
    
}

One error on code - loop variable must be type Id ?
 
public class EventAttendeeContactConnectorHandler {
    
    
    @future
    public static void EventAttendeeContactConnector(set <id> relations) {
        
        for (EVentRelation rel: relations){
            rel.isParent=true;
        }
        update relations; 
    }

}

 
jaw999jaw999

public class EventAttendeeContactConnectorHandler {
    
    
    @future
    public static void EventAttendeeContactConnector(set <id> eventRelSet) {
    
        list <EventRelation> relations =[select id from EventRelation where id IN: eventRelSet];
    
        for (EVentRelation rel: relations){
            rel.isParent=true;
        }
        //update relations; 
        Database.update(relations,false);
    
    }

}

jaw999jaw999
The @fututre method is working.