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
SeanGormanSeanGorman 

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.

 

 

Tim BarsottiTim Barsotti

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. 

 

Map<Id, decimal> AccountTraineeRemovedCount = new Map<Id, decimal>();
For(Training_Attendee__c att: Attendees){
	if(AccountTraineeRemovedCount.get(att.AccountID__c) == null) {
		AccountTraineeRemovedCount.put(att.AccountID__c, 1);
	} else {
		AccountTraineeRemovedCount.put(att.AccountID__c, AccountTraineeRemovedCount.get(att.AccountID__c) + 1);
	}
}

List<AccountTraining__c> AccountsToDelete = new List<AccountTraining__c>();
for(AccountTraining__c a: [select ID, RollupAttendeeCount from AccountTraining__c where Account__c in: accountIds]) {
	if(a.RollupAttendeeCount == AccountTraineeRemovedCount.get(a.Id)) {
		AccountsToDelete.Add(a);
	}
}
if(AccountsToDelete.size>0) {
	Delete AccountsToDelete;
}