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
Leonardo Girgenti 5Leonardo Girgenti 5 

Help with a Trigger

Dear all,
thank you in advance for you help.
i have a new custom object called Meetings.
then I have a Join object called Meeting Participants, that links Contacts to Meetings.
I would like to ensure that the meeting also show in the Account of the Contacts selected as Meeting Participants.
I need a trigger that automatically puts the selected contact's account in the Account field that I have in the Join Object.
Workflow dont seem to help here as the target field is a lookup.
I could look into process builder but I think a simple trigger should work.
i am purpously not using activities as we need several rich text fields on the Meeting Record and need to use SDoc or Conga to generate and print documents of the meeting.
 
Would anyone help me with the trigger creation??
thanks,
Leonardo
Best Answer chosen by Leonardo Girgenti 5
James LoghryJames Loghry

No, the trigger in the first post only handles the insert operation, not an update.  To handle the case where the Account changes, you would need to add a bit of extra logic:

trigger MeetingParticipantTrigger on Meeting_Participant__c(before insert,before update){
    Set<Id> contactIds = new Set<Id>();
    List<Meeting_Participant__c> participantsWithUpdatedContacts = new List<Meeting_Participant__c>();

    for(Meeting_Participant__c mp : Trigger.new){
        if(Trigger.oldMap == null || (Trigger.oldMap.get(mp.Id).Contact__c == null && mp.Contact__c != null) || (Trigger.oldMap.get(mp.Id).Contact__c != mp.Contact__c)){
        contactIds.add(mp.Contact__c);
        participantsWithUpdatedContacts.add(mp);
    }
 
    Map<Id,Contact> contacts = new Map<Id,Contact>([Select AccountId From Contact Where Id in :contactIds];
 
    for(Meeting_Participant__c mp : participantsWithUpdatedContacts){
        Contact c = contacts.get(mp.Contact__c);
        mp.Account__c = c.AccountId;
    }
}

To answer your test question, yes, you would need some test class to test your Meeting_Participant__c logic if you went the trigger route.  (You don't need one if you go the process builder route, fyi).  For test code coverage, you need code coverage > 0% on triggers, and at least 75% across all apex classes in your org.  However, you should always shoot for 100% code coverage on all your apex classes and triggers.  

All Answers

James LoghryJames Loghry
Here's a quick stab at your trigger.  You'll want to change the API names for one to match your Meeting Participant object and its contact and account fields.  Yes, you could also accomplish this via process builder, but be careful as it doesn't support bulk loads at the moment.
trigger MeetingParticipantTrigger on Meeting_Participant__c(before insert){
    Set<Id> contactIds = new Set<Id>();
    for(Meeting_Participant__c mp : Trigger.new){
        contactIds.add(mp.Contact__c);
    }

    Map<Id,Contact> contacts = new Map<Id,Contact>([Select AccountId From Contact Where Id in :contactIds];

    for(Meeting_Participant__c mp : Trigger.new){
        Contact c = contacts.get(mp.Contact__c);
        mp.Account__c = c.AccountId;
    }
}

 
Leonardo Girgenti 5Leonardo Girgenti 5
Thanks, I will test it in Sandbox.
Would this handles also if a user click on the join record and changes the Contact to another one in a different Account?
To then deploy this would I need a test class??
Thanks in advance.
Leonardo
James LoghryJames Loghry

No, the trigger in the first post only handles the insert operation, not an update.  To handle the case where the Account changes, you would need to add a bit of extra logic:

trigger MeetingParticipantTrigger on Meeting_Participant__c(before insert,before update){
    Set<Id> contactIds = new Set<Id>();
    List<Meeting_Participant__c> participantsWithUpdatedContacts = new List<Meeting_Participant__c>();

    for(Meeting_Participant__c mp : Trigger.new){
        if(Trigger.oldMap == null || (Trigger.oldMap.get(mp.Id).Contact__c == null && mp.Contact__c != null) || (Trigger.oldMap.get(mp.Id).Contact__c != mp.Contact__c)){
        contactIds.add(mp.Contact__c);
        participantsWithUpdatedContacts.add(mp);
    }
 
    Map<Id,Contact> contacts = new Map<Id,Contact>([Select AccountId From Contact Where Id in :contactIds];
 
    for(Meeting_Participant__c mp : participantsWithUpdatedContacts){
        Contact c = contacts.get(mp.Contact__c);
        mp.Account__c = c.AccountId;
    }
}

To answer your test question, yes, you would need some test class to test your Meeting_Participant__c logic if you went the trigger route.  (You don't need one if you go the process builder route, fyi).  For test code coverage, you need code coverage > 0% on triggers, and at least 75% across all apex classes in your org.  However, you should always shoot for 100% code coverage on all your apex classes and triggers.  
This was selected as the best answer
Leonardo Girgenti 5Leonardo Girgenti 5
Thanks, I guess it is too much to ask for that Test Class??

Also a question, doing the trigger if I select 2 contacts from the same Account, in the Account record I will see 2 records in the related list for the same meeting (this is because the Join shows the Contacts records associated to the Meeting). is there a way in the related list to "group" the record by meeting thus not showing multiple records? is that a visual force page?
Thanks!
L