You need to sign in to do that
Don't have an account?
Working out whether or not to delete row in joining table
I have an issue and I'm struggling to work it out.
Object: Training
Object: Training Attendee
Object: Contact
Object: Account
Object: Account Training
User creates Training and adds attendees (Contacts)
Trigger automatically create new relationship between Account and Training in Account Training.
NB. If 10 attendees from 'CompanyA' are added then there will be 1 row in AccountTrainee
If attendee is removed from training then we need to check to see if the row in AccountTrainee should be deleted too. This may happen in bulk so.... it's not very easy.
public void RemovingTraining_Method(list<Training_Attendee__c> Attendees) { map<id, id> ContacttoAtt = new map<id, id>(); map<id, id> AttToTraining = new map<id, id>(); map<id, list<Training__c>> AttTrName = new map<id, list<Training__c>>(); map<id, id> AccounttoTraining = new map<id, id>(); map<id, id> TrainingtoAccount = new map<id, id>(); map<id, id> AccounttoTraining_toDelete = new map<id, id>(); list<Training_Attendee__c> Atts_forDelt = new list <Training_Attendee__c>(); set<id> Contacts = new set<id>(); set<id> trainingIds = new set<id>(); set<id> attendsIds = new set<id>(); set<id> accountIds = new set<id>(); set<id> accountIds_toRemove = new set<id>(); set<id> trainingIds_toRemove = new set<id>(); list<id> Trainings = new list<id>(); list<Contact> ConsToUpdate = new list<Contact>(); // find the last attendee record for the contact for(Training_Attendee__c att: Attendees) { Contacts.add(att.Attendee__c); attendsIds.add(att.Attendee__c); trainingIds.add(att.Training__c); accountIds.add(att.AccountID__c); AccounttoTraining.put( att.AccountID__c, att.Training__c); TrainingtoAccount.put( att.Training__c, att.AccountID__c); } system.debug('accountIds from all deleted attendees' + accountIds); system.debug('trainingIds from all deleted attendees ' + trainingIds); Atts_forDelt = [select id, Attendee__c, Training__c, AccountID__c from Training_Attendee__c where AccountID__c in: accountIds and Training__c in :trainingIds ]; system.debug('All attendees Atts_forDelt ' + Atts_forDelt); if(Atts_forDelt.size() > 0) { for(Training_Attendee__c attendee : Atts_forDelt) { if(1==0) { accountIds_toRemove.add(attendee.AccountID__c); } { system.debug('in the else'); } } } else { for(Training_Account__c AccTrains :[select id, Training__c, Accounts__c from Training_Account__c where Accounts__c in :accountIds and Training__c in :trainingIds order by Accounts__c desc]) { accountIds_toRemove.add(AccTrains.id); } } system.debug('Those account training we plan to delete ' + accountIds_toRemove); // account training for(Training_Account__c AccTrains :[select id, Training__c, Accounts__c from Training_Account__c where Accounts__c in :accountIds_toRemove and Training__c in :trainingIds order by Accounts__c desc]) { // if this account does not already have this training if(AccTrains.Training__c == AccounttoTraining.get(AccTrains.Accounts__c) && AccTrains.Accounts__c == TrainingtoAccount.get(AccTrains.Training__c)) { continue; } else { // a collection of account ids to trainings accountIds_toRemove.add(AccTrains.Accounts__c); AccounttoTraining_toDelete.put(AccTrains.Accounts__c, AccounttoTraining.get(AccTrains.Accounts__c)); system.debug('Those accounts with their matching Trainings ' + AccounttoTraining_toDelete); } }
Please feel free to nit-pick too. This is totally a work in progress.
I would create a rollup summary field to count the number of attendees for each AccountTraining. Then use this as a comparision in your trigger. Example code - written on the fly so I'm sure it won't compile esp since the RollupAttendeeCount field has not been created yet.
Hope this helps.