You need to sign in to do that
Don't have an account?
tulasiram 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);
}
}
}
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);
}
}
}
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
All Answers
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
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.
Hope it helps!