You need to sign in to do that
Don't have an account?
Debugging - Trigger+Class Methods
This trigger ends up passing nothing to the two class methods I'm calling. Can someone help me debug? Here is my Trigger.
Purpose - if one of two contact lookups is changed in the account - it should update the related contact lookup field on a related Property_Account__c object.
trigger AccountUpdate on Account (before update) {
Map<Id,Account> newAccountMap = Trigger.newMap;
Map<Id,Account> oldAccountMap = Trigger.oldMap;
List<Account> accspmig = new list<Account>();
List<Account> accsppay = new list<Account>();
for(Id AccountId: newAccountMap.keySet()){
Account myNewAccount = newAccountMap.get(AccountId);
Account myOldAccount = oldAccountMap.get(Accountid);
IF(myNewAccount.MigrationContacta__r <> myOldAccount.MigrationContacta__r){
accspmig.add(myNewAccount);
}
IF(myNewAccount.PaymentsContacta__r <> myOldAccount.PaymentsContacta__r){
accsppay.add(myNewAccount);
}
}
AccountUpdate.MigContact(accspmig);
AccountUpdate.PayContact(accsppay);
}
I didn't get any feedback - but here is the solution I ended up with. I actually moved all my code to the Apex Class. Here is the code for Trigger and Class:
trigger AccountUpdate on Account (before update) {
//Create instance of my class
public AccountUpdate clsAccountUpdate=new AccountUpdate();
//assign array to the public variables declared in my class
clsAccountUpdate.lstNewAccounts = Trigger.new;
clsAccountUpdate.lstOldAccounts = Trigger.old;
//trigger class methos
clsAccountUpdate.MigContact();
}
public class AccountUpdate{
//Declare class variables
Public List<Account> lstNewAccounts = new List<Account>();
Public List<Account> lstOldAccounts = new List<Account>();
public void MigContact() {
//declare method varaibles
String migid;
Map<Id,Account> mapVerifyOldAccounts=new Map< Id,Account>();
List<Account> accsm = new list<Account>();
//map old data
For(Account accOld : lstOldAccounts){ mapVerifyOldAccounts.put(accOld.id,accOld);}
//For any records where value changed add to array of records to update
For(Account accNew: lstNewAccounts){
IF(accNew.MigrationContacta__c != mapVerifyOldAccounts.get(accNew.Id).MigrationContacta__c){
migid=String.valueOf(accNew.MigrationContacta__c);
accsm.add(accNew);
}
}
//pull list of child records to update
Property_Account__c[] pmigs =[Select Id,MigrationContacta__c FROM Property_Account__c Where Account__c IN: accsm];
List<Property_Account__c> listpmig = new list<Property_Account__c>();
//update child records
For(Property_Account__c b :pmigs){
b.MigrationContacta__c=migid;
listpmig.add(b);
}
Update listpmig;
}
}
All Answers
Here are the class methods the trigger is calling:
public class AccountUpdate {
public Static void MigContact(Account[] accspmig) {
Contact migid;
List<Account> accsm = new list<Account>();
for (Account a :accspmig){
migid= a.MigrationContacta__r;
accsm.add(a);
}
Property_Account__c[] pmigs =[Select Id,MigrationContacta__c FROM Property_Account__c Where Account__c IN: accsm];
List<Property_Account__c> listpmig = new list<Property_Account__c>();
For(Property_Account__c b :pmigs){
b.MigrationContacta__r=migid;
listpmig.add(b);
}
Update listpmig;
}
public Static void PayContact(Account[] accsppay) {
Contact payid;
List<Account> accsp = new list<Account>();
for (Account c :accsppay){
payid= c.PaymentsContacta__r;
accsp.add(c);
}
Property_Account__c[] ppays =[Select Id,PaymentsContacta__c FROM Property_Account__c Where Account__c IN: accsp];
List<Property_Account__c> listppay = new list<Property_Account__c>();
For(Property_Account__c d :ppays){
d.PaymentsContacta__r=payid;
listppay.add(d);
}
Update listppay;
}
}
I didn't get any feedback - but here is the solution I ended up with. I actually moved all my code to the Apex Class. Here is the code for Trigger and Class:
trigger AccountUpdate on Account (before update) {
//Create instance of my class
public AccountUpdate clsAccountUpdate=new AccountUpdate();
//assign array to the public variables declared in my class
clsAccountUpdate.lstNewAccounts = Trigger.new;
clsAccountUpdate.lstOldAccounts = Trigger.old;
//trigger class methos
clsAccountUpdate.MigContact();
}
public class AccountUpdate{
//Declare class variables
Public List<Account> lstNewAccounts = new List<Account>();
Public List<Account> lstOldAccounts = new List<Account>();
public void MigContact() {
//declare method varaibles
String migid;
Map<Id,Account> mapVerifyOldAccounts=new Map< Id,Account>();
List<Account> accsm = new list<Account>();
//map old data
For(Account accOld : lstOldAccounts){ mapVerifyOldAccounts.put(accOld.id,accOld);}
//For any records where value changed add to array of records to update
For(Account accNew: lstNewAccounts){
IF(accNew.MigrationContacta__c != mapVerifyOldAccounts.get(accNew.Id).MigrationContacta__c){
migid=String.valueOf(accNew.MigrationContacta__c);
accsm.add(accNew);
}
}
//pull list of child records to update
Property_Account__c[] pmigs =[Select Id,MigrationContacta__c FROM Property_Account__c Where Account__c IN: accsm];
List<Property_Account__c> listpmig = new list<Property_Account__c>();
//update child records
For(Property_Account__c b :pmigs){
b.MigrationContacta__c=migid;
listpmig.add(b);
}
Update listpmig;
}
}