You need to sign in to do that
Don't have an account?
Taresh Pandey 19
Update account's date field while updating its associated contact's date field.
My trigger should have work, but it's not working. please advise me where went I wrong ?
trigger NextFollowUpDateofContactIntoAccount on Contact (after INSERT, after UPDATE, after DELETE){
List<Account> Acc_List = new List<Account>();
Set<ID> setAccountIDs = new Set<ID>();
if(Trigger.isInsert ||Trigger.isUpdate){
for(Contact c : Trigger.new){
setAccountIDs.add(c.AccountId);
}
}
if(Trigger.isDelete){
for(Contact c : Trigger.old){
setAccountIDs.add(c.AccountId);
}
}
Date NextFloowUpDate = null;
Date YesterdaysDate = system.today()-1;
List<Account> accounts = [SELECT Id,
Name,(
Select ID,
Name,
Next_Follow_Up_Date__c,
Important__c
FROM Contacts ORDER BY Next_Follow_Up_Date__c, systemModstamp DESC Nulls last limit 1
)
FROM Account WHERE ID IN :setAccountIDs ORDER BY SystemModstamp ];
try{
for(Account a : accounts){
for(Contact c : a.Contacts){
NextFloowUpDate = c.Next_Follow_Up_Date__c ;
if(c.Important__c == true && c.Next_Follow_Up_Date__c != null && c.Next_Follow_Up_Date__c > YesterdaysDate){
a.Next_Follow_Up_Date__c = NextFloowUpDate;
}else if(c.Important__c == false && c.Next_Follow_Up_Date__c != null && c.Next_Follow_Up_Date__c > YesterdaysDate){
a.Next_Follow_Up_Date__c = NextFloowUpDate+7;
}else{
a.Next_Follow_Up_Date__c = null ;
}
Acc_List.add(a);
}
}
}catch(Exception e){
system.debug('Got an exception');
}
upsert Acc_List;
}
trigger NextFollowUpDateofContactIntoAccount on Contact (after INSERT, after UPDATE, after DELETE){
List<Account> Acc_List = new List<Account>();
Set<ID> setAccountIDs = new Set<ID>();
if(Trigger.isInsert ||Trigger.isUpdate){
for(Contact c : Trigger.new){
setAccountIDs.add(c.AccountId);
}
}
if(Trigger.isDelete){
for(Contact c : Trigger.old){
setAccountIDs.add(c.AccountId);
}
}
Date NextFloowUpDate = null;
Date YesterdaysDate = system.today()-1;
List<Account> accounts = [SELECT Id,
Name,(
Select ID,
Name,
Next_Follow_Up_Date__c,
Important__c
FROM Contacts ORDER BY Next_Follow_Up_Date__c, systemModstamp DESC Nulls last limit 1
)
FROM Account WHERE ID IN :setAccountIDs ORDER BY SystemModstamp ];
try{
for(Account a : accounts){
for(Contact c : a.Contacts){
NextFloowUpDate = c.Next_Follow_Up_Date__c ;
if(c.Important__c == true && c.Next_Follow_Up_Date__c != null && c.Next_Follow_Up_Date__c > YesterdaysDate){
a.Next_Follow_Up_Date__c = NextFloowUpDate;
}else if(c.Important__c == false && c.Next_Follow_Up_Date__c != null && c.Next_Follow_Up_Date__c > YesterdaysDate){
a.Next_Follow_Up_Date__c = NextFloowUpDate+7;
}else{
a.Next_Follow_Up_Date__c = null ;
}
Acc_List.add(a);
}
}
}catch(Exception e){
system.debug('Got an exception');
}
upsert Acc_List;
}
I created an apex class for this and called apex method into a trigger, it's working fine now.
All Answers
Could you please provide me which error you are receiving.Otherwise, put some debug logs in if and else if and else so we can understand
our code is executing or not.
Thanks
Varaprasad
Thanks for the reply, here thing is I'm not getting any error. my trigger was working till few days before but somehow it stopped working.
The SOQL is fetching record correctly but it's not getting record in below list.
I created an apex class for this and called apex method into a trigger, it's working fine now.