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
SticklebackStickleback 

Restricting certain Event Types based on custom Contact field

Hi.  I have requirement to only allow certain Event Types to be used for certain type of contacts e.g. we have a custom field of is_prospect__c on Contact & if this is true we only want our users to be able to assign an Event Type of introductory Meeting whereas if it is false they can choose any of the other Event Types.

 

I thought at first I could use a trigger on the event but I've not been able to get that to work correctly - at the time the trigger fires SF only seems to have created the EventRelation row for the primary contact so my validation for the prospect contact is ignored.  

 

Any thoughts gratefully received.

 

Stickleback

digamber.prasaddigamber.prasad

Hi,

 

Above said requirement can be achived even using validation rule. However, if you have written trigger, could you please paster your code here, so that I can take a look and help you in fixing it.

 

Happy to help you!

 

Regards,

Digamber Prasad

SticklebackStickleback

Digamber,

 

Thanks for the reply.

 

Here's my trigger (apologies for the noob standard of it - still learning)

 

trigger Event_AfterInsertBeforeUpdate on Event (after insert, before update) {
    string errorMessage = '';
    string errorContactList = '';
    for ( Event event: trigger.new )
    {
        errorMessage = '';
        errorContactList = '';
        for (EventRelation whoRelations : [SELECT RelationId FROM EventRelation WHERE isWhat = false AND EventId = :event.id]){
            for (Contact contact : [Select id, is_prospect__c, name From contact where id=:whoRelations.RelationId]){
                if (contact.is_prospect__c == true) {
                    if (event.type__c != 'Introductory Call' && event.type__c != 'Introductory Email'){
                        if (errorContactList == '' ){
                            errorMessage = 'The activity type you have selected is not valid for the Prospect(s) you have selected.  Only ‘Introductory Call’ or ‘Introductory Email’ activity types can be used';
                            errorContactList = contact.name;
                        } else {
                            errorContactList = errorContactList + ', ' + errorContactList ;
                        } 
                    }
        
                } else {
                    if (event.type__c == 'Introductory Call' || event.type__c == 'Introductory Email'){
                        if (errorContactList == '' ){
                            errorMessage = 'The activity type you have selected is only valid for Propects.  Please choose another activity type.';
                            errorContactList = contact.name;
                        } else {
                            errorContactList = errorContactList + ', ' + errorContactList ;
                        }
                    }
                }
            }
            if (errorMessage != '') {
                event.type__c.addError(errorMessage + ' (' + errorContactList + ')');
            }
        }     
    }

 

The problem seems to be that when the trigger fires, the EventRelation only has a row for the primary contact so any other contacts that have been assigned to the Event are not found.

 

I've had a look at Validation Rules and that seems a far better solution but I cannot work out how to get to the Contact rows from the event.  I've had a hunt around the web & the only examples I can find use the WhoId to determine if the event is aimed at a contact e.g. left(whoid,3)='003'.  Presumably the whoId would only be the primary contact anyway & so won't help in the scenario I have.  I'm not sure I can get to the information I want.

 

Again any help appreciated as this is driving me crazy.

 

Stickleback

 

digamber.prasaddigamber.prasad

Hi,

 

I have had a look at your trigger. You don't need to query EventRelation object. As you said you can just use WhoId field to make it work. I have modified your trigger, not it will allow 'Introductory Call' & 'Introductory Email' event type only for 'Is_Prospect__c' checked contact. If this check box is unchecked on contact it will only only all other event type.

trigger EventTrigger on Event (before insert, before update) {
    Set<Id> setWhoId = new Set<Id>();
    Map<Id, Contact> mapContact = new Map<Id, Contact>();
    for(Event ev : trigger.new){
        if(String.valueOf(ev.WhoId).startsWith('003')){
            setWhoId.add(ev.WhoId);
        }
    }
    
    if(setWhoId.size() > 0){
        List<Contact> lstContact = [Select Id, Is_Prospect__c from Contact where Id in :setWhoId];
        for(Contact c : lstContact){
            mapContact.put(c.Id, c);
        }
    }
    
    for(Event ev : trigger.new){
        if(String.valueOf(ev.WhoId).startsWith('003')){
            if(mapContact.containsKey(ev.WhoId)){
                if(!mapContact.get(ev.WhoId).Is_Prospect__c && (ev.Type__c == 'Introductory Call' || ev.Type__c == 'Introductory Email')){
                    ev.addError('Invalid event Type');
                }else if(mapContact.get(ev.WhoId).Is_Prospect__c && (ev.Type__c != 'Introductory Call' && ev.Type__c != 'Introductory Email')){
                    ev.addError('Invalid event Type');
                }
            }
        }
    }

}

 

Hope it will help you and you can make modification in trigger to achieve your business objective.

 

Happy to help you!

 

Regards,

Digamber Prasad

 

 

 

 

SticklebackStickleback

Hi again Digamber,

 

I really, really appreciate your help on this but unfortunately your code doesn't fix the problem.  When I assign 2 contacts to a new event, the primary is a normal contact (is_prospect__c = false) & the second is a prospect (is_prospect__c = true), it allows a non-prospect event type to be chosen.  Putting a debug statement in I can see that when it is building the setWhoId set it only goes through this loop once.  SetWhoId ends up with just the primary's contact Id in it.  Also if I run a SOQL query over the event :-

 

select whoid from event where id = '00Ue0000001jyTy'

 it returns an Id of 

003D000000wtiTlIAI

 

I think whoid just holds the id of the primary contact & to get the full list of invitees I need to go to EventRelation but that isn't populated fully by the time the event trigger has fired.

 

Rather than the trigger solution you also mentioned that this could be achieved via a validation rule.  Do you still believe that to be true & if so can you suggest how to get to the event's contacts?

 

Thanks very much  once again,

 

Stickleback

digamber.prasaddigamber.prasad

Are you talking about adding contacts to event using "Add to Invitees" link on edit page of events?

 

If not how are you using multiple contacts to same event? Please let me know, so that I can further look into it.

 

Happy to help you!

 

Regards,

Digamber Prasad

SticklebackStickleback

Yes, Add to Invitees on edit page of events.

 

digamber.prasaddigamber.prasad

Cool. Looks like I got answer of your question, there are couple of tables related to events 

 

  1. EventRelation
  2. UndecidedEventRelation

Now, you can modify the trigger which I have written above or your original trigger to include contact records from above 2 objects and you should be good to go.

 

Let me know if you still have any problem.

 

Happy to help you!

 

Regards,

Digamber Prasad