title: please help me to convert trigger to batch apex
body: <p>please help me in converting my after trigger to batch apex.
  This trigger fires when opportunity stage changes to won.
  It runs through line items and checks if forecast(custom objet) exists with that acunt.if yes,iit links to them..if no,itt will create a new forecat.</p>
  
  <p>my trigger works fine forr some records.but to mass update i am getting timed out error.So opting batch apex but i had never written it.pls help me.</p>
  
  <pre><code>trigger Accountforecast on Opportunity (after insert,after update) {
      List&lt;Acc_c&gt; AccproductList =new List&lt;Acc_c&gt;();
      List&lt;Opportunitylineitem&gt; opplinitemlist =new List&lt;Opportunitylineitem&gt;();
      list&lt;opportunitylineitem &gt; oppdate=  new list&lt;opportunitylineitem &gt;();
      List&lt;Acc__c&gt; accquery =new List&lt;Acc__c&gt;();
      List&lt;date&gt; dt =new List&lt;date&gt;();
      Set&lt;Id&gt; sProductIds = new Set&lt;Id&gt;();
      Set&lt;Id&gt; sAccountIds = new Set&lt;Id&gt;();
      Set&lt;id&gt; saccprodfcstids =new set&lt;Id&gt;();
      Acc__c accpro =new Acc__c();
      string aname;
      Integer i;
      Integer myIntMonth;
      Integer myIntyear;
      Integer myIntdate;
  
      opplinitemlist=[select Id,PricebookEntry.Product2.Name,opp_account__c,Opp_account_name__c,PricebookEntry.Product2.id, quantity,ServiceDate,Acc_Product_Fcst__c  from Opportunitylineitem                      WHERE Opportunityid IN :Trigger.newMap.keySet() AND Acc__c=''];
  
      for(OpportunityLineItem oli:opplinitemlist) {
      sProductIds.add(oli.PricebookEntry.Product2.id);
      sAccountIds.add(oli.opp_account__c);
      }
      accquery=[select id,Total_Qty_Ordered__c,Last_Order_Qty__c,Last_Order_Date__c,Fcst_Days_Period__c  from Acc__c where Acc__c.product__c In :sproductids and Acc__c.Account__c in :saccountids]; 
  
    for(Acc__c apf1 :accquery){
      saccprodfcstids.add(apf1.id);
      }        
      if(saccprodfcstids!=null){
      oppdate=[select servicedate from opportunitylineitem where Acc__c IN :saccprodfcstids ];
  
      i =[select count() from  Opportunitylineitem where acc_product_fcst__c in :saccprodfcstids];
      }
  
  
       for(Opportunity opp :trigger.new)
       {
           if(opp.Stagename=='Closed Won')
           {
                   for(opportunitylineitem opplist:opplinitemlist)
                   {
                       if(!accquery.isempty())
                       {
                           for(opportunitylineitem opldt :oppdate)
                           {
                               string myDate = String.valueOf(opldt);
                               myDate = myDate.substring(myDate.indexof('ServiceDate=')+12);
                               myDate = myDate.substring(0,10);                                    
                               String[] strDate = myDate.split('-');
                               myIntMonth = integer.valueOf(strDate[1]);
                               myIntYear = integer.valueOf(strDate[0]);
                               myIntDate = integer.valueOf(strDate[2]);
                               Date d = Date.newInstance(myIntYear, myIntMonth, myIntDate);
                               dt.add(d);
                           }  
                               dt.add(opp.closedate);
                               dt.sort();                   
                               integer TDays=0;
                               system.debug('*************dt:'+dt.size());
                                   for(integer c=0;c&lt;dt.size()-1;c++)
                                   {
                                       TDays=TDays+dt[c].daysBetween(dt[c+1]);
  
                                   }
                                   for(Acc_product_fcst__c apf:accquery)
                                   {
                                       apf.Fcst_Days_Period__c = TDays/i;
                                       apf.Total_Qty_Ordered__c =apf.Total_Qty_Ordered__c +opplist.quantity;
                                       apf.Last_Order_Qty__c=opplist.quantity;
                                       apf.Last_Order_Date__c=opp.CloseDate ;
                                       apf.Fcst_Qty_Avg__c=apf.Total_Qty_Ordered__c/(i+1);
                                       Opplist.Acc__c =apf.Id;  
                                   }
                                                      }
  
                                   else{
                                   accpro.Account__c=opplist.opp_account__c;
                                   accpro.product__c=opplist.PricebookEntry.Product2.Id;
                                   accpro.opplineitemid__c=opplist.id;
                                   accpro.Total_Qty_Ordered__c =opplist.quantity;
                                   accpro.Last_Order_Qty__c=opplist.quantity;
                                   accpro.Last_Order_Date__c=opp.CloseDate;
                                   accpro.Fcst_Qty_Avg__c=opplist.quantity;
                                   accpro.Fcst_Days_Period__c=7;
                                   accproductList.add(accpro);
  
  
  
                                   }
                                   }
  
               }
           }
           if(!accproductlist.isempty()){
           insert accproductlist;
           }
           update opplinitemlist;
           update accquery;         
       }
  </code></pre>