+ Start a Discussion
MasieMasie 

Trigger on a join object not firing.

Hi

Me again- I have written a trigger on a join object to update the one of its parent object based on the other's details.

The two objects are Accounts and Rate Type and the join object is called Company and Rate Type Links. May someone please take a look and let me know where I am going wrong as it is not firing.

trigger UpdateGroupRates on Company_and_RateType_Link__c (after update, after insert)
{  
if(trigger.isUpdate)
{
  Set<Id> accountIds = new Set<Id>();
  Set<Id> ratesIds = new Set<Id>();
  for(Company_and_RateType_Link__c tempJ : Trigger.new)
  {
   accountIds.add(tempJ.Company__c);
   ratesIds.add(tempJ.Rate_Type__c);
  }
 
  List<Account> AccountList = [Select Id, name, Rate_Type__c, Fixed_Credit_Card_Rate__c, Fixed_Debit_Card_Rate__c from Account where Id IN: accountIds];
 
  List<Rate_Type_del__c> Ratelist = [Select id, Name, Rate_Type__c, Number_Credit_Card_Fee__c, Number_Debit_Card_Fee__c from Rate_Type_del__c where id in: ratesIds];
 
  for(Company_and_RateType_Link__c tempJ : Trigger.new)
  {
   for(Account tempAcc : AccountList)
   {
    if(tempJ.Company__c == tempAcc.Id)
    {
     for(Rate_Type_del__c tempRate : Ratelist)
     {

                       if(tempJ.Rate_Type__c == tempRate.Id )
      {
       tempAcc.Rate_Type__c = tempRate.Rate_Type__c;
                            tempAcc.Fixed_Credit_Card_Rate__c = tempRate.Number_Credit_Card_Fee__c;
                            tempAcc.Fixed_Debit_Card_Rate__c = tempRate.Number_Debit_Card_Fee__c;
      }    
    
     }
   
    }
  
   }
  
  } 
   update AccountList;
}
      
}
Best Answer chosen by Masie
Vatsal KothariVatsal Kothari
You can refer below code:

trigger UpdateGroupRates on Company_and_RateType_Link__c (after update, after insert)
{  

	Set<Id> accountIds = new Set<Id>();
	Set<Id> ratesIds = new Set<Id>();
	for(Company_and_RateType_Link__c tempJ : Trigger.new)
	{
		accountIds.add(tempJ.Company__c);
		ratesIds.add(tempJ.Rate_Type__c);
	}

	for(Account acc : [Select Id, name, Rate_Type__c, Fixed_Credit_Card_Rate__c, Fixed_Debit_Card_Rate__c from Account where Id IN: accountIds]){
		if(acc.Id != null){
			accMap.put(acc.Id,acc);
		}
	}
	
	for(Rate_Type_del__c rate : [Select id, Name, Rate_Type__c, Number_Credit_Card_Fee__c, Number_Debit_Card_Fee__c from Rate_Type_del__c where id in: ratesIds]){
		if(rate.Id != null){
			tempRateMap.put(rate.Id,rate);
		}
	}
	
	for(Company_and_RateType_Link__c tempJ : Trigger.new)
	{
			if(accMap.containsKey(tempJ.Company__c) && tempRateMap.containsKey(tempJ.Rate_Type__c)){					
				accMap.get(tempJ.Company__c).Rate_Type__c = tempRateMap.get(tempJ.Rate_Type__c).Rate_Type__c;
				accMap.get(tempJ.Company__c).Fixed_Credit_Card_Rate__c = tempRateMap.get(tempJ.Rate_Type__c).Number_Credit_Card_Fee__c;
				accMap.get(tempJ.Company__c).Fixed_Debit_Card_Rate__c = tempRateMap.get(tempJ.Rate_Type__c).Number_Debit_Card_Fee__c;					
			}
	}
	
	update accMap.values();
}

If this solves your problem, kindly mark it as the best answer.

Thanks,
Vatsal

All Answers

Vatsal KothariVatsal Kothari
Hi Masie,

You have added the if condition in the 3rd line "if(trigger.isUpdate)", which ends at the end of the code, so it will work for update only, not for insert.

MasieMasie
Hi Vatsal

Its not working for updates as well hence my question.
Vatsal KothariVatsal Kothari
You can refer below code:

trigger UpdateGroupRates on Company_and_RateType_Link__c (after update, after insert)
{  

	Set<Id> accountIds = new Set<Id>();
	Set<Id> ratesIds = new Set<Id>();
	for(Company_and_RateType_Link__c tempJ : Trigger.new)
	{
		accountIds.add(tempJ.Company__c);
		ratesIds.add(tempJ.Rate_Type__c);
	}

	for(Account acc : [Select Id, name, Rate_Type__c, Fixed_Credit_Card_Rate__c, Fixed_Debit_Card_Rate__c from Account where Id IN: accountIds]){
		if(acc.Id != null){
			accMap.put(acc.Id,acc);
		}
	}
	
	for(Rate_Type_del__c rate : [Select id, Name, Rate_Type__c, Number_Credit_Card_Fee__c, Number_Debit_Card_Fee__c from Rate_Type_del__c where id in: ratesIds]){
		if(rate.Id != null){
			tempRateMap.put(rate.Id,rate);
		}
	}
	
	for(Company_and_RateType_Link__c tempJ : Trigger.new)
	{
			if(accMap.containsKey(tempJ.Company__c) && tempRateMap.containsKey(tempJ.Rate_Type__c)){					
				accMap.get(tempJ.Company__c).Rate_Type__c = tempRateMap.get(tempJ.Rate_Type__c).Rate_Type__c;
				accMap.get(tempJ.Company__c).Fixed_Credit_Card_Rate__c = tempRateMap.get(tempJ.Rate_Type__c).Number_Credit_Card_Fee__c;
				accMap.get(tempJ.Company__c).Fixed_Debit_Card_Rate__c = tempRateMap.get(tempJ.Rate_Type__c).Number_Debit_Card_Fee__c;					
			}
	}
	
	update accMap.values();
}

If this solves your problem, kindly mark it as the best answer.

Thanks,
Vatsal
This was selected as the best answer
MasieMasie
Hi Vatsal Kothari


i am getting a compling error on line 14 where it says the method does not exist or has an incorrect signature Set Id.put(id, sobject) and when I remove the object I still get the same error.
Vatsal KothariVatsal Kothari
Sorry I forgot to declare Map.
Add below code after 5th line:
Map<Id,Account> accMap = new Map<Id,Account>();
Map<Id,Rate_Type_del__c> tempRateMap = new Map<Id,Rate_Type_del__c>();
MasieMasie
Hi Vatsal Kothari

May I say that you are the man- it works like a bomb. I clearly need to get some lessons from you.
MasieMasie
Vatsal

One other question- its is firing when inserted not when updated- is there anything I can do to fix this.