You need to sign in to do that
Don't have an account?
Justin Cairns 14
How can I have a delete trigger a recalculation?
I am creating a trigger that will call code to count contacts at the account level meeting certain criteria. It's working for Adds and Updates, but when I delete a contact the trigger is not updating anything. Here is the trigger. Do I need to place the isDelete somewhere else so that it will use the current values instead of the values before the action?
trigger TAFS_Contact_Trigger on Contact(after insert, after update, after delete){
if(Trigger.isAfter){
if(Trigger.isInsert){
TAFS_ContactTriggerHandler.contactInsert(Trigger.new);
}
if(Trigger.isDelete){
TAFS_ContactTriggerHandler.contactInsert(Trigger.old);
}
if(Trigger.isUpdate){
TAFS_ContactTriggerHandler.contactInsert(Trigger.new);
}
}
}
trigger TAFS_Contact_Trigger on Contact(after insert, after update, after delete){
if(Trigger.isAfter){
if(Trigger.isInsert){
TAFS_ContactTriggerHandler.contactInsert(Trigger.new);
}
if(Trigger.isDelete){
TAFS_ContactTriggerHandler.contactInsert(Trigger.old);
}
if(Trigger.isUpdate){
TAFS_ContactTriggerHandler.contactInsert(Trigger.new);
}
}
}
Check you class "TAFS_ContactTriggerHandler" in which you are querying or not for number of contact for a particular account. Id not then get contacts related Account & then update count field.
Regards
Tej Pal Kumawat
Skype : tejpalkumawat1991
If this answers your question mark Best Answer it as solution and then hit Like!
public static void contactInsert(List<Contact> contactList){
Set<Id> leadSetIds = new Set<Id>();
// Added for extending the trigger to Client contacts
Set<Id> clientSetIds = new Set<Id>();
List<Contact> contacts = [select id,TAFS_Contact_Type__c,TAFS_Lead__r.id,Account.id, Account.name from Contact where id in :contactList];
for(Contact contactObj : contacts){
// Getting Leads Ids for new Lead contacts
if(String.IsNotBlank(contactObj.TAFS_Lead__c) && (contactObj.Account.name == 'TAFS Inc.' || contactObj.Account.name == '')){
leadSetIds.add(contactObj.TAFS_Lead__c);
system.debug(contactObj+',1 lead id: '+contactObj.TAFS_Lead__c);
}
// Getting Client Ids for new Client contacts
if(contactObj.Account!=null && contactObj.Account.name != 'TAFS Inc.' ){
clientSetIds.add(contactObj.Account.id);
}
}
system.debug(contactList[0].id+', lead id: '+contactList[0].TAFS_Lead__c);
// update the number of owner contacts in Leads
List<Lead> leadList = [select Id,TAFS_Number_of_Contacts__c,TAFS_General_Manager_tag__c,(SELECT Id,name FROM Contacts__r) FROM Lead where Id IN :leadSetIds AND isConverted = False];
for(Lead leadObj:leadList){
leadObj.TAFS_Number_of_Contacts__c = leadObj.Contacts__r.size();
//Dynamically assign last signer role to GM based on the number of owner contacts
Integer docusignRoutingNo = leadObj.Contacts__r.size() + 1;
leadObj.TAFS_General_Manager_tag__c = 's'+docusignRoutingNo;
}
Database.update(leadList);
// update the number of owner contacts in Account
List<Account> clientList = [select Id,TAFS_Number_of_Owner_Contacts__c,TAFS_General_Manager_tag__c,(SELECT Id,name FROM Contacts WHERE TAFS_Contact_Type__c = 'Owner') FROM Account where Id IN :clientSetIds];
for(Account clientObj : clientList){
clientObj.TAFS_Number_of_Owner_Contacts__c = clientObj.Contacts.size();
//Dynamically assign last signer role to GM based on the number of owner contacts
Integer docusignRoutingNo = clientObj.Contacts.size() + 1;
clientObj.TAFS_General_Manager_tag__c = 's'+docusignRoutingNo;
}
try{
Database.update(clientList);
}
catch(Exception ex){
for(Contact clientObj : contactList){
clientObj.addError(Label.TAFS_Contact_Validation_Message);
}
}
}
}
Your code look like fine, Just check conditions those you are testing matches with trigger condtions.
Regards
Tej Pal Kumawat
Skype : tejpalkumawat1991
If this answers your question mark Best Answer it as solution and then hit Like!