I am trying to develop an Apex trigger for an Object Lead. Requirement is like count number of times that record viewed.
I am Sharing my Code with you.
trigger ContactedRecord on Lead (after insert,after update) {
List<ID> lid=new List<ID>();
for(Lead l:Trigger.new)
{
lid.add(l.id);
}
Integer i=0;
List<Lead> leadlist=[Select id,No_of_Times_Contacted__c from Lead where id IN:lid];
System.debug('**List size**'+leadlist.size());
for(Lead li: leadlist)
{
/*Lead l=new Lead();
Lead olddata=Trigger.oldMap.get(li.id);*/
system.debug('No of Contacts');
if(li.size()>0)
{
for(Integer j=0;j<li.Size(0);j++)
{
if(li.No_of_Times_Contacted__c ==li[j].id)
{
i++;
li[j].No_of_Times_Contacted__c =i;
}
}
}
update li;
}