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
Pranav ChitransPranav Chitrans 

Trigger AfterInsert

Hello guys..plz help
Error: Compile Error: Invalid field AccountId for SObject Contact__c at line 62 column 32
I had created the Account and contact As a custom object on seperate app.so dont get cnfused. I also mentioned the line 62 in apex class..wher i am getting error..
***APEX Class****
public class fees
{
public void onBeforeInsert(list<Contact__c>triggerNew)
 {
  feesCheckInsert(triggerNew);
 }
  
 public void onBeforeUpdate(list<Contact__c>triggerNew,map<Id,Contact__c>triggerOldMap)
 {
  feesCheckUpdate(triggerNew);
  feesCheckOnChange(triggerNew,triggerOldMap);
 }
 
 public void onAfterInsert(list<Contact__c>triggerNew)
 {
  manageContactFees(triggerNew);
 }
 
 void feesCheckInsert(list<Contact__c>triggerNew)
 {
  for(Contact__c f : triggerNew )   
  {
   if(f.Fees__c ==null || f.Fees__c<1000)
   {
    f.Fees__c.addError('Cannot be NULL and greater than 1000 ');
   }
  }  
 }
 
 void feesCheckUpdate(list<Contact__c>triggerNew)
 {
  for(Contact__c f : triggerNew )   
  {
   if(f.Fees__c == null)
   {
    f.Fees__c.addError('can not be null');
   }
  }  
 }
 
 void feesCheckOnChange(list<Contact__c>triggerNew,map<Id,Contact__c>triggerOldMap)
 {
  for(Contact__c f : triggerNew )   
  {
   if(f.Fees__c !=Null && f.Fees__c!=triggerOldMap.get(f.Id).Fees__c)
   {
    f.Fees__c.addError('Cannot chnage the value');
   }
  }  
 }
 
 void manageContactFees(list<Contact__c> triggerNew)
 {
    list<Account__c> lstAcc = new list<Account__c>();
    map<Id,list<Contact__c>> mapAccIdToContact = new map<Id,list<contact__c>>();
    for(Contact__c f : triggerNew)
    {
     if(f.AccountId != null)
     {
        if(mapAccIdToContact.get(f.AccountId)== null)
        {
         mapAccIdToContact.put(f.AccountId,new list<Contact__c>());
        }   
        mapAccIdToContact.get(f.AccountId).add(f);      
     }
    }
    if(mapAccIdToContact.size() > 0)
    {
     for(Account__c f : [select Id,Total_Fees__c from Account__c where Id in : mapAccIdToContact.keySet()])
     {
       double dblTotalFees = 0;
                list<Contact__c> lstCon = mapAccIdToContact.get(f.Id);
                for(Contact__c objC : lstCon)
                {
                    if(objC.Fees__c == null)
                    {
                        objC.Fees__c = 0;
                    }
                    dblTotalFees += objC.Fees__c;
                }
                
                if(f.Total_Fees__c == null)
                {
                    f.Total_Fees__c = 0;
                }
                
                f.Total_Fees__c += dblTotalFees;
                
                lstAcc.add(f);
     }
     if(lstAcc.size() > 0)
     {
       update lstAcc;
     }
    }
 } 
}
****APEX Trigger****
trigger FeesCheck1 on Contact__c(before Insert, before Update, after Insert)
{
 fees FeesHelper = new fees();
 if(trigger.isInsert && trigger.Isbefore)
 {
    FeesHelper.onBeforeInsert(trigger.New);

 }
  if(trigger.isUpdate && trigger.Isbefore)
 {
    FeesHelper.onBeforeUpdate(trigger.New,trigger.OldMap);
 }
 if(trigger.isInsert && trigger.isAfter)
 {
  FeesHelper.onAfterInsert(trigger.New);
 }
}

 
Best Answer chosen by Pranav Chitrans
Abhishek BansalAbhishek Bansal
Hi Pranav,

I have modified the code of manageContactFees method of your class.
Please replace your method with below code :
void manageContactFees(list<Contact__c> triggerNew){
	list<Account__c> lstAcc = new list<Account__c>();
    map<Id,list<Contact__c>> mapAccIdToContact = new map<Id,list<contact__c>>();
    for(Contact__c f : triggerNew) {
	    if(f.Account__c != null)// I have replaced AccountId with Account__c If Account__c is not the API name of your Account__c lookup field created on Contact__c than please change Account__c with the API name of the Account__c field created on Contact__c
	    {
	    	if(!mapAccIdToContact.containsKey(f.Account__c)){
	    		mapAccIdToContact.put(f.Account__c,new list<Contact__c>());
	        }   
	        mapAccIdToContact.get(f.Account__c).add(f);      
	    }
    }
    if(mapAccIdToContact.size() > 0){
    	for(Account__c f : [select Id,Total_Fees__c from Account__c where Id in : mapAccIdToContact.keySet()]){
    		double dblTotalFees = 0;
            list<Contact__c> lstCon = mapAccIdToContact.get(f.Id);
            for(Contact__c objC : lstCon){
            	if(objC.Fees__c == null){
            		objC.Fees__c = 0;
                }
            	dblTotalFees += objC.Fees__c;
            }
            if(f.Total_Fees__c == null){
                f.Total_Fees__c = 0;
            }
            f.Total_Fees__c += dblTotalFees;
            lstAcc.add(f);
    	}
    	if(lstAcc.size() > 0){
    		update lstAcc;
    	}
    }
 }

Please make sure that you correctly replace Account__c with the API name of Account lookup field on Contact__c object.
Let me know if you need any help on this.

Regards,
Abhishek

All Answers

ClintLeeClintLee
AccountId is a standard field that's accessible on the standard Contact object.

If you've created a custom object such as Contact__c then the AccountId field is not on that object.

On your Contact__c object you probably created a lookup field to the Account__c object.  You need to use the name of that lookup field in your code. 

Hope that helps,

Clint
Abhishek BansalAbhishek Bansal
Hi Pranav,

I have modified the code of manageContactFees method of your class.
Please replace your method with below code :
void manageContactFees(list<Contact__c> triggerNew){
	list<Account__c> lstAcc = new list<Account__c>();
    map<Id,list<Contact__c>> mapAccIdToContact = new map<Id,list<contact__c>>();
    for(Contact__c f : triggerNew) {
	    if(f.Account__c != null)// I have replaced AccountId with Account__c If Account__c is not the API name of your Account__c lookup field created on Contact__c than please change Account__c with the API name of the Account__c field created on Contact__c
	    {
	    	if(!mapAccIdToContact.containsKey(f.Account__c)){
	    		mapAccIdToContact.put(f.Account__c,new list<Contact__c>());
	        }   
	        mapAccIdToContact.get(f.Account__c).add(f);      
	    }
    }
    if(mapAccIdToContact.size() > 0){
    	for(Account__c f : [select Id,Total_Fees__c from Account__c where Id in : mapAccIdToContact.keySet()]){
    		double dblTotalFees = 0;
            list<Contact__c> lstCon = mapAccIdToContact.get(f.Id);
            for(Contact__c objC : lstCon){
            	if(objC.Fees__c == null){
            		objC.Fees__c = 0;
                }
            	dblTotalFees += objC.Fees__c;
            }
            if(f.Total_Fees__c == null){
                f.Total_Fees__c = 0;
            }
            f.Total_Fees__c += dblTotalFees;
            lstAcc.add(f);
    	}
    	if(lstAcc.size() > 0){
    		update lstAcc;
    	}
    }
 }

Please make sure that you correctly replace Account__c with the API name of Account lookup field on Contact__c object.
Let me know if you need any help on this.

Regards,
Abhishek
This was selected as the best answer
Pranav ChitransPranav Chitrans
Thanks Abhishek....
I had tried by changing the name Account__c it works but having one issue.. when ever i insert the fees while creating new contact... the associated object(Account) there I had create the filed name total fess... to count the total fees of all the contact under that.. The total fees get incresed by creating new contact.. but when ever i changed the fess amt from contact the total fees remains the same on account object. I want to update the total fees(Account Object) according to chnges made on fess(Contact Object)...
Abhishek BansalAbhishek Bansal
Hi Pranav,

You are calling your manageContactFees method only on "after insert" event that is why account is not updated on update.
Please call this method on "After Update" event also.

If you still have any issue than please let me know.

Regards,
Abhishek