You need to sign in to do that
Don't have an account?
LFI
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?
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?
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
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;
}
}
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;
}
}
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;
}
}