title: Apex trigger for Number of Time Record is Viewed
body: <p>I am trying to develop an Apex trigger for an Object Lead. Requirement is like  count number of times that record viewed.</p>
  
  <p>I am Sharing my Code with you.</p>
  
  <p>trigger ContactedRecord on Lead (after insert,after update) {</p>
  
  <pre><code>List&lt;ID&gt; lid=new List&lt;ID&gt;();
  
  for(Lead l:Trigger.new)
  {
      lid.add(l.id);
  }
  
  Integer i=0;
  List&lt;Lead&gt; 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()&gt;0)
      {
          for(Integer j=0;j&lt;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;
  }
  </code></pre>