You need to sign in to do that
Don't have an account?
sagar077
Create an apex trigger that will count number of contacts associated with an account(create a field at account level) . Must update the count in insertion and deletion of a contact.
Create an Apex Trigger that will count the number of contacts associated with an account(create a field at account level). Must update the count in insertion and deletion of a contact.
CODE-
trigger CountonAccountofcontact on Contact (after insert,after delete)
{
Set<Id> mysetid = new Set <Id>();
if(Trigger.isinsert)
{
System.debug('Insert new contact for trigger.new '+ Trigger.new);
for(Contact contac :trigger.new)
{
mysetid.add(contac.Accountid);
}
List<Account> Acc = [Select Id,Number_Of_Contact_Count__c from Account where Id in : mysetid];
List<Contact> Con = [Select Id from Contact where Accountid in : mysetid];
for(Account A: Acc)
{
A.Number_Of_Contact_Count__c = Con.size();
}
update Acc;
System.debug('Number of count is ' + Acc);
}
if(Trigger.isdelete)
{
System.debug('The Delete Contact Name For Trigger.old'+ Trigger.Old);
for(Contact contac : Trigger.Old)
{
mysetid.add(contac.Accountid);
}
List<Account> Acc = [Select id,Number_Of_Contact_Count__c from Account where id in: mysetid];
List<Contact> Con = [Select id from Contact where Accountid in : mysetid];
for(Account A :Acc)
{
A.Number_Of_Contact_Count__c = Con.size();
}
update Acc;
System.debug('The Update number is '+ Acc);
}
}
NOTE- This code is running but I want for After Update event also and Plz Help me in that
CODE-
trigger CountonAccountofcontact on Contact (after insert,after delete)
{
Set<Id> mysetid = new Set <Id>();
if(Trigger.isinsert)
{
System.debug('Insert new contact for trigger.new '+ Trigger.new);
for(Contact contac :trigger.new)
{
mysetid.add(contac.Accountid);
}
List<Account> Acc = [Select Id,Number_Of_Contact_Count__c from Account where Id in : mysetid];
List<Contact> Con = [Select Id from Contact where Accountid in : mysetid];
for(Account A: Acc)
{
A.Number_Of_Contact_Count__c = Con.size();
}
update Acc;
System.debug('Number of count is ' + Acc);
}
if(Trigger.isdelete)
{
System.debug('The Delete Contact Name For Trigger.old'+ Trigger.Old);
for(Contact contac : Trigger.Old)
{
mysetid.add(contac.Accountid);
}
List<Account> Acc = [Select id,Number_Of_Contact_Count__c from Account where id in: mysetid];
List<Contact> Con = [Select id from Contact where Accountid in : mysetid];
for(Account A :Acc)
{
A.Number_Of_Contact_Count__c = Con.size();
}
update Acc;
System.debug('The Update number is '+ Acc);
}
}
NOTE- This code is running but I want for After Update event also and Plz Help me in that
I think you would be able to implement this particular scenario using the flow, please check the below link for reference.
Implementation using flow: https://www.xgeek.net/salesforce/create-roll-up-summary-field-to-count-contacts-on-account-with-process-builder-and-flows/
Additionally, I found this below code you can check which I found in a salesforce StackExchange question:
I hope this helps and in case if this comes handy can you please choose this as best answer so that it can be used by others in the future.
Regards,
Anutej