You need to sign in to do that
Don't have an account?
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****
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); } }
I have modified the code of manageContactFees method of your class.
Please replace your method with below code :
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
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
I have modified the code of manageContactFees method of your class.
Please replace your method with below code :
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
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)...
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