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
tulasiram chtulasiram ch 

Updating parent record from child using trigger

Hi , im trying to update Account.fax = contact.fax if account.type == prospect.
Then i used helper class , but not working give me solution please. And tell how can i write test class for trigger and helper class. I am learning Apex programming...
Trigger :trigger newSupportinClassForContact on Contact (before insert, after insert, before update, after update) {
    
        supportClassForContactUpdateAccount.AccountContactCreation(trigger.new);
        

Error: Method does not exist or incorrect signature: void AccountContactCreation(List<Contact>) from the type supportClassForContactUpdateAccount
Helper Class:
public class supportClassForContactUpdateAccount {
     public static void AccountContactCreation(){
     //list<contact>  cons = new list<contact>();
         contact[]  cons;
         cons = trigger.new;
         list<id> accountIdList = new list<id>();
         for(contact newCons:cons){
             if(newCons.fax != null && newCons.AccountId !=null ){
                 accountIdList.add(newCons.Accountid);
             }
         Map<ID, account> accToUpdate = new Map<ID, account> (
    [select Id, name, fax from account
    where Id in :accountIdList]);
  
List<account> accUpd = new List<account>{};
        for(account newAcc:accToUpdate.values()){
            if(newAcc.Type=='Prospect'){
            newAcc.Fax=newCons.Fax;
            accUpd.add(newAcc);
            }
        }
 if (accUpd != null && !accUpd.isEmpty())
Database.update(accUpd);
         }
}
}
Best Answer chosen by tulasiram ch
Hemant_JainHemant_Jain
Hello, In the class you have given the method signature as AccountContactCreation(), which is without any parameters.

While when calling the method the code is,  supportClassForContactUpdateAccount.AccountContactCreation(trigger.new);

Here you are passing Trigger.new, which is List<Contacts> in this case.

So change the signature in the supportClassForContactUpdateAccount as given below
public class supportClassForContactUpdateAccount {
     public static void AccountContactCreation(list<contact> cons){
    //Remove initialization of cons(cons = trigger.new)
//Rest code remains same
}
}



 

All Answers

Hemant_JainHemant_Jain
Hello, In the class you have given the method signature as AccountContactCreation(), which is without any parameters.

While when calling the method the code is,  supportClassForContactUpdateAccount.AccountContactCreation(trigger.new);

Here you are passing Trigger.new, which is List<Contacts> in this case.

So change the signature in the supportClassForContactUpdateAccount as given below
public class supportClassForContactUpdateAccount {
     public static void AccountContactCreation(list<contact> cons){
    //Remove initialization of cons(cons = trigger.new)
//Rest code remains same
}
}



 
This was selected as the best answer
HARSHIL U PARIKHHARSHIL U PARIKH
Hello tulasiram ch,

I have implemented this without a class and via single trigger only. I am using Before Insert, Before Update, After UnDelete as a parameters.
Try to see if the below works for you.
 
Trigger ProspectAccount on Account(Before Insert, Before Update, After UnDelete){

    List<Contact> consListToUpdate = New List<Contact>();
     
    If(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUnDelete)
    {
        For(Account act : Trigger.New)
        {
            If(act.Type == 'Prospect' && act.Fax != null)
            {
                List<Contact> cons = [Select Id, AccountId, Fax FROM Contact WHERE AccountId =:act.Id];
                
                
                For(Contact EveryCon : cons)
                {
                    EveryCon.Fax = act.Fax;
                    consListToUpdate.add(EveryCon);
                }
            }
            If(consListToUpdate.Size() > 0){
                update consListToUpdate;
            }
            
        }
    }
}
Hope it helps!
 
tulasiram chtulasiram ch
Hemanth its working fine Thank you... Can you tell me how to write Test class for that helper class...