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
LFILFI 

Update Junction records after Contact update

Hello,
I have set up a custom object called Roster, and a junction object called Contact_to_Roster_Associations. What it does is that when I create a new Roster and specify the year, it generates a list of all contacts that were in that year. It works great in most cases, except when there is a change in the Contacts. For example if I have a contact with year 2015 that is already on the list, if I go and change the date to something else, say 2012, that contact still remains on the list. How can that be updated accordingly? I'm thinking the trigger needs to re-execute?
sandeep sankhlasandeep sankhla
Hi LFI,

You can create a trigger on Contact also for after update event and then you can check if year value is changed then you cna find the all junction object for that record and delete all those records because in this case new junction record should be created with new year and roster..

Thanks,
Sandeep
LFILFI
Can you help where do I start with the code? Here is the Roster trigger, I assume it should be something similar

trigger rosterTrigger on Roster__c  (after insert) {
    
        public set<String> strTypes = new set<String>();
        public set<String> strYears = new set<String>();
        public list<Contact> lstContact = new list<Contact>();
        public list<Contact_to_Roster_Association__c> lstContactAssociations = new list<Contact_to_Roster_Association__c> ();
        
         for(Roster__c objRoster : trigger.new)
         {
             strTypes.add(objRoster.Program_Type__c);
             strYears.add(objRoster.Program_Year__c);
         }
                 
         for(Contact objContact : [Select Id ,Alumni_Type__c,  Alumni_Year__c from Contact where Alumni_Type__c IN:  strTypes AND Alumni_Year__c IN : strYears])
         {
             
             if(objContact.Alumni_Type__c !=null && objContact.Alumni_Year__c !=null)
             {
                 lstContact.add(objContact);
             }
             
         }
        
         for(Roster__c objC : trigger.new)
         {
             for(Contact objContact : lstContact)
            {
                Contact_to_Roster_Association__c objRA = new Contact_to_Roster_Association__c();
                objRA.Contact__c = objContact.Id;
                objRA.Roster__c = objC.Id;
                lstContactAssociations.add(objRA);
            }
         }
        
        if(!lstContactAssociations.isEmpty())
        {
            insert lstContactAssociations;
        }
}
LFILFI
I started something like this below:

trigger contactTrigger on Contact (after insert, after update) {
    
        public set<String> strTypes = new set<String>();
        public set<String> strYears = new set<String>();
        public list<Contact> lstContact = new list<Contact>();
        public list<Contact_to_Roster_Association__c> lstContactAssociations = new list<Contact_to_Roster_Association__c> ();
        
         for(Contact objContact : trigger.new)
         {
             strTypes.add(objContact.Alumni_Type__c);
             strYears.add(objContact.Alumni_Year__c);
         }
                 
         for(Contact objContact : [Select Id ,Alumni_Type__c,  Alumni_Year__c from Contact where Alumni_Type__c IN:  strTypes AND Alumni_Year__c IN : strYears])
         {
             
             if(objContact.Alumni_Type__c !=null && objContact.Alumni_Year__c !=null)
             {
                 lstContact.add(objContact);
             }
             
         }
        
         for(Contact objC : trigger.new)
         {
             for(Contact objContact : lstContact)
            {
                Contact_to_Roster_Association__c objRA = new Contact_to_Roster_Association__c();
                objRA.Contact__c = objContact.Id;
                objRA.Roster__c = objC.Id;
                lstContactAssociations.add(objRA);
            }
         }
        
        if(!lstContactAssociations.isEmpty())
        {
            insert lstContactAssociations;
        }
}
LFILFI
Would it be easier if I change the Roster trigger to (after insert, after update)? Then I can just hit Edit/Save (unless I figure out an update button), then the trigger will run and recreate the list of Contacts? Can someone help with the code for the highlighted lines? Also would there be any issue with this approach, such remove/recreate the associations? Thanks in advance!

I'm thinking the pseudocode should be something like this:

trigger rosterTrigger on Roster__c  (after insert, after update) {
    (need Apex code) 
    check if there is an existing list of contacts associated already
        if there is, then remove the list/all associations
    recreate the list with the code below


for(Roster__c objRoster : trigger.new)
         {
             strTypes.add(objRoster.Program_Type__c);
             strYears.add(objRoster.Program_Year__c);
         }
                 
         for(Contact objContact : [Select Id ,Alumni_Type__c,  Alumni_Year__c from Contact where Alumni_Type__c IN:  strTypes AND Alumni_Year__c IN : strYears])
         {
             
             if(objContact.Alumni_Type__c !=null && objContact.Alumni_Year__c !=null)
             {
                 lstContact.add(objContact);
             }
             
         }
        
         for(Roster__c objC : trigger.new)
         {
             for(Contact objContact : lstContact)
            {
                Contact_to_Roster_Association__c objRA = new Contact_to_Roster_Association__c();
                objRA.Contact__c = objContact.Id;
                objRA.Roster__c = objC.Id;
                lstContactAssociations.add(objRA);
            }
         }
        
        if(!lstContactAssociations.isEmpty())
        {
            insert lstContactAssociations;
        }
}

    
 
LFILFI
Anyone? Thanks in advance!