+ Start a Discussion
Alex Waddell 12Alex Waddell 12 

Help Apex Trigger: Update Account from Child record

Hello everyone,

I am trying to write a Trigger that looks at the number of Communication_Note__c records exist in an account and when there are 3 notes of the outcome "No Answer" update a field on the Account called "Fuzion Billing Date" with the Created date of the third note

Right now i am getting the error on line 12; "Variable does not exist: Fuzion_Billing_Date__c".

I believe this is because i need some sort of nested querie that ties the Parent Account and its Fuzion_Billing_Date__c to each Communication_Note__c record 

Can anyone help me with this please?
trigger BillingDate2 on Communication_Note__c (After insert) {
    List<Communication_Note__c> allCom = new List <Communication_Note__c> ([Select Id
                                                                          From Communication_Note__c
                                                                           where id in :Trigger.new
                                                                           AND Fuzion_Type__c = 'Initial Phone Call'
                                                         			   	   AND Outcome__c = 'No Answer']);
	List<Account> myAcc = New List<Account>([Select Id, Fuzion_Billing_Date__c
                           From Account]);

    If(allcom.size() == 3){
     myAcc.Fuzion_Billing_Date__c = Date.Today();

Amit Chaudhary 8Amit Chaudhary 8
try to update your code like below
trigger BillingDate2 on Communication_Note__c (After insert) 

	Set<ID> setAccId = new Set<ID>();
	for(Communication_Note__c aNote : Trigger.New)
		setAccId.add(aNote.Account__c) ; // Please replace Account__c with accountid

	//replace Communication_Note__r with child relationship name.
	List<Account> lstAcc = [ SELECT Id, Fuzion_Billing_Date__c ,
								(Select Id	 From Communication_Note__r  where Fuzion_Type__c = 'Initial Phone Call' AND Outcome__c = 'No Answer']
							FROM Account where id :setAccId];
	List<Account> lstAccToUpdate = new List<Account>();
	for(Account acc: lstAcc)
		List<Communication_Note__c> lstNote = acc.Communication_Note__r;
		If(lstNote.size() == 3){
			acc.Fuzion_Billing_Date__c = Date.Today();
	if(lstAccToUpdate.size() > 0 )
		update lstAccToUpdate;