+ Start a Discussion
Bayarea 101Bayarea 101 

Contacted counter for Contacts

I need to create a field on contact. It should be recording how many time this contact has been contacted. Based on the activity history and put it as number on contact counter how many time the contact has been contacted.
ra811.3921220580267847E12ra811.3921220580267847E12
Hi ,

Create one field on Contact with APIName:count1__c .

TriggerCode:

trigger updateContactCounts on Task (after insert, after update) {

set<ID> contactIds= new Set<ID>();
for(Task tt:trigger.new)
{
if(tt.whoId!=null && string.valueof(tt.WhoId).startsWith('003'))
{

contactIDs.add(tt.whoID);
}
}

if(contactIds.isEmpty())
return;
List<Contact> cnts= new List<Contact>();

Integer number1=1;
for(Contact ct:[select id, name, count1__c from contact where id in:contactids])
{

if(ct.count1__c ==null)
ct.count1__c=0;

ct.count1__c=ct.count1__c+number1;


cnts.add(ct);
}

if(cnts.size()>0)
{
update cnts;
}
}


 
Bayarea 101Bayarea 101
hi when i try to save this trigger it generates
[Error] Error: Compile Error: Loop variable must be of type SOBJECT:Contact at line 4 column 10
Bayarea 101Bayarea 101
i trying to create this trigger on contacts.
Bayarea 101Bayarea 101
thank my mistake it working like a charm thanks and really appreciated your help
 
Bayarea 101Bayarea 101
one quick question i am trying to do same thing for leads and it is not calculating any thing. Am i doing some thing wrong. Ihad replaced Contacts to lead in this code. please advise.
Bayarea 101Bayarea 101
Here is the trigger but Count field remains blank. Am i missing some thing.
trigger UpdateLeadCounts on Task (after insert, after update) {
 
set<ID> LeadIds= new Set<ID>();
for(Task tt:trigger.new)
{
if(tt.whoId!=null && string.valueof(tt.WhoId).startsWith('003'))
{
 
LeadIDs.add(tt.whoID);
}
}
 
if(LeadIds.isEmpty())
return;
List<Lead> cnts= new List<Lead>();
 
Integer number1=1;
for(Lead ct:[select id, name, count1__c from Lead where id in:Leadids])
{
 
if(ct.count1__c ==null)
ct.count1__c=0;
 
ct.count1__c=ct.count1__c+number1;
 
 
cnts.add(ct);
}
 
if(cnts.size()>0)
{
update cnts;
}
}