function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Lisa Horne 1Lisa Horne 1 

Help with Error - Attempt to de-reference a null object

I have this controller that I trying to deploy and I keep getting this error.  Can anyone help?

User-added image



126         FindAccounts();
127         system.debug('PossibleAccounts Listed: ' + PossibleAccounts.size());
128         AttachAccountName = ThisLead.Company;
129         */
130         //Set value of Account Null variable
131         AccountNull=true;
132         apexpages.message myMsg = new ApexPages.Message(ApexPages.Severity.error, 'Lead must have an Account in order to convert. Please update your lead.'); //see page 124
133          apexPages.addMessage(myMsg);        
134       }      
135       
136       if(AttachContactID != null)
137       {
138         //AttachContact = [Select c.id, c.name from Contact c where c.id = :AttachContactID];
139         Setvalues();
140         system.debug('Contact Queried: ' + AttachContact);
141         //FindContacts();
142       }
143       else
144       {
145         /***Removed no longer need to search for Contacts
146         //if(AttachAccountID != null){FindContacts();}
147         */
148         //Define the ContactNull variable
149         ContactNull = true;
150         apexpages.message myMsg = new ApexPages.Message(ApexPages.Severity.error, 'Lead must have a Contact to convert. Please update your lead'); //see page 124
151          apexPages.addMessage(myMsg);
152       }
153     }
154     else
155     {
156       apexpages.message myMsg = new ApexPages.Message(ApexPages.Severity.error, 'Could not find ID of Converting Lead. Please Try Again'); //see page 124
157        apexPages.addMessage(myMsg);
158        system.debug('Lead is empty');
159      }
160     
161     if(AttachAccount != null && AttachContact != null)
162     {
163       if(AttachContact.AccountID != AttachAccount.id)
164       {
165         apexpages.message myMsg = new ApexPages.Message(ApexPages.Severity.error, 'Your lead cannot be converted at this time. The Account of the Contact does not match the Account on the lead. Please update your lead'); //see page 124
166          apexPages.addMessage(myMsg);
167          Acccount_ContactMatch = true;
168       }
169     }
170     
171     
172     system.debug('AttachAccountID: ' + AttachAccountID); system.debug('AttachAccount: ' + AttachAccount); 
173     system.debug('PossibleAccounts: ' + PossibleAccounts.size()); system.debug('AttachContactID: ' + AttachContactID);
174     system.debug('End of Controller Constructor');
175   //END of Controller load method      
176   }
177 /**
178 * @author
179 * @date
180 * @param
181 * @description
182 * @return
183 */  
184   /****Values and Search Methods ****/
185   public PageReference SetValues()
186   {   system.debug('SETTING VALUES'); 
187     system.debug('AttachAccountID: ' + AttachAccountID); system.debug('AttachContactID: ' + AttachContactID); 
188     system.debug('AttachOpportunityID: '+ AttachOpportunityID);
189     system.debug('SearchOpportunity: '+ SearchOpportunity);
190     if(LeadId != null){
191       /*Set Account values */
192       if(AttachAccountID == null){AttachOpportunityID = null; AttachContactID = null;}
193       if(AttachAccountID != null && AttachAccountID != 'Create' && AttachAccountID != 'CreateNew' &&  AttachAccountID !='Search')
194       {
195         //Define the Attach Account based on AttachAccountID      
196         AttachAccount = [Select a.id, a.name,a.Hospital_Bed_Size__c, a.Bed_Size_Group__c from Account a where a.id = :AttachAccountID]; system.debug('Setting Attach Account');
197         //Get list of Possible Opportunities that are Open.
198         PossibleOpportunities = [Select o.id, o.name from Opportunity o 
199           where o.OwnerID = :RecordOwnerOpp.OwnerID and o.AccountID = :AttachAccountID and (NOT o.StageName like '%Closed%')];      
200         DisplayContact = true;
201         
202         FindContacts();
203       }
204       else if(AttachAccountID == 'Search' && RecordOwnerOpp.AccountID != null)
205       {
206         AttachAccount = [Select a.id, a.name,a.Hospital_Bed_Size__c, a.Bed_Size_Group__c from Account a where a.id = :RecordOwnerOpp.AccountID]; system.debug('Setting Attach Account');
207         DisplayContact = false;
208       }
209       else if(AttachAccountID == 'CreateNew')
210       {
211         RecordOwnerOpp.AccountID= null; AttachAccount.name = AttachAccountName; AttachOpportunityId = 'Create'; if(AttachContactID == null){AttachContactID='Create';}
212       }
213       else if(AttachAccountID == 'Create')
214       {
215         RecordOwnerOpp.AccountID= null; AttachAccount.name = ThisLead.Company; AttachOpportunityId = 'Create'; if(AttachContactID == null){AttachContactID='Create';}
216       }
217       /*Set Contact Values */
218       if(AttachContactID != null && AttachContactID != 'Create' && AttachContactID != 'CreateNew' &&  AttachContactID !='Search' )
219       {
220         AttachContact = [Select c.id, c.name,AccountId from Contact c where c.id = :AttachContactID]; system.debug('Setting Contact');
221       }
222       else if(AttachContactID == 'Search' && SearchContact.contact__c != null)
223       {
224         AttachContact = [Select c.id, c.name,AccountId from Contact c where c.id = :SearchContact.Contact__c]; system.debug('Setting Contact');
225       }
226       /*Set Opportunity Values*/
227       if(AttachOpportunityID != null && AttachOpportunityID != 'Search' && AttachOpportunityID != 'Create')
228       {
229         AttachOpportunity = [Select o.id, o.name from Opportunity o where o.id = :AttachOpportunityID];
230         OpportunityCreate = true;
231         ConvertStatus = 'Duplicate - Opportunity Exists';      
232       }
233       else if(AttachOpportunityID == 'Search' && SearchOpportunity.Opportunity__c != null)
234       {
235         system.debug('SearchOpportunity valid: ' + SearchOpportunity);
236         AttachOpportunity = [Select o.id, o.name from Opportunity o where o.id = :SearchOpportunity.Opportunity__c];
237         OpportunityCreate = true;
238         ConvertStatus = 'Duplicate - Opportunity Exists';
239       }
240       else if(AttachOpportunityID == 'Create')
241       {
242         system.debug('Create Opportunity Name');
243         OpportunityCreate = false; ConvertStatus = 'Qualified (Yes > Opportunity)';
244         string Products = '';string Product = '';
245         /*
246         for(integer i=0; i<LeadProducts.size(); i++)
247         {
248           if(LeadProducts[i] == 'Accupedia'){Product = 'Accu';}
249           else if(LeadProducts[i] == 'ADT Interface'){Product = 'ADT';}
250           else if(LeadProducts[i] == 'Amplifi' ){Product = 'Amp';}
251           else if(LeadProducts[i] == 'DIOne'){Product = 'DIOne';}
252           else if(LeadProducts[i] == 'King Guide'){Product = 'King';}
253           else if(LeadProducts[i] == 'Quantifi'){Product = 'QTFI';}
254           else if(LeadProducts[i] == 'RiskQI'){Product = 'Risk';}
255           else if(LeadProducts[i] == 'ScheduleRx'){Product = 'SRx';}
256           else if(LeadProducts[i] == 'Sentri7'){Product = 'S7';}
257           else if(LeadProducts[i] == 'Simplifi 797'){Product = 'S797';}
258           else if(LeadProducts[i] == 'UnitStock'){Product = 'US';}
259           else if(LeadProducts[i] == 'Medboard'){Product = 'Med';}
260           else{system.debug('Product not Found!' + LeadProducts[i]); Product = LeadProducts[i];}
261                     
262           if(Products == ''){Products = Product;}else{Products= Products + ', ' + Product;}
263         }
264         */
265         //if(ThisLead.Products__c != null){Products = ThisLead.Products__c;}
266         RecordOwnerOpp.name = AttachAccount.name + ' - ' + Products;
267       }
268     }return null;
269   }
270   
271   public void FindContacts() //Find Possible Contacts for selected Account
272   {
273     system.debug('AttachContactID input: ' + AttachContactID);
274     if(AttachAccountID != null && AttachAccountID != 'Create' && AttachAccountID != 'CreateNew' &&  AttachAccountID !='Search')
275     {        
276       system.debug('FINDING CONTACTS...');      
277       list<string>WordSearch = new list<string>();
278       WordSearch.Add(String.valueof(ThisLead.Firstname)); Wordsearch.add(String.valueof(ThisLead.LastName));    
279       system.debug('Account: ' + AttachAccount.Name); System.debug('WordSearch list: ' + WordSearch);
280       string WordVariable = null;
281       string QueryString; list<string> QueryVariable = new list<string>();
282       QueryString = 'select c.id, c.name from Contact c where (c.name like ';
283       for(integer i=0; i<WordSearch.size(); i++)
284       {      
285         WordVariable = '%' + WordSearch[i] + '%'; system.debug('WordVariable: ' +WordVariable);  
286         QueryVariable.add(WordVariable);      
287         if(QueryVariable.size() == 1){
288           QueryString = Querystring + '\''+QueryVariable[i]+'\'';}
289           else{QueryString = QueryString + ' or c.name like ' + '\''+ QueryVariable[i]+'\'';}
290       }
291       QueryString = QueryString + ') and c.AccountiD = :AttachAccountID';
292       system.debug('Contact QueryString: ' + QueryString);
293       PossibleContacts = database.query(QueryString);
294     }
295     //Added in case the name of the Lead doesn't match the name of the contact exactly
296     if(AttachContactID != null)
297     {      
298       boolean IsPresent = false;
299       for(Contact c :PossibleContacts)
300       {
301         if(AttachContact != null && c.id == AttachContact.id){IsPresent = true;}
302       }
303       if(AttachContact != null &&  IsPresent == false){PossibleContacts.add(AttachContact);}
304     }
305   }
306   public void FindAccounts()
307   {
308     system.debug('FINDING ACCOUNTS...');
309     list<string>WordSearch = string.valueof(ThisLead.Company).split(' ');
310     system.debug('Company name: ' + ThisLead.Company); System.debug('WordSearch list: ' + WordSearch);
311     string WordVariable = null;
312     string QueryString; list<string> QueryVariable = new list<string>();
313     QueryString = 'select a.id, a.name from Account a where a.name like ';
314     for(integer i=0; i<WordSearch.size(); i++)
315     {      
316       WordVariable = '%' + string.escapeSingleQuotes(WordSearch[i]) + '%'; system.debug('WordVariable: ' +WordVariable);  
317       QueryVariable.add(WordVariable);      
318       if(QueryVariable.size() == 1){
319         QueryString = Querystring + '\''+QueryVariable[i]+'\'';}
320         else{QueryString = QueryString + ' and a.name like ' + '\''+ QueryVariable[i]+'\'';}      
321     }
322     system.debug('Query Variable: ' + QueryVariable);
323     system.debug('Query String: ' + QueryString);
324     PossibleAccounts = database.query(QueryString);
325     if(AttachAccount.id != null)
326     {   boolean Match = false;
327       for(integer i=0; i< PossibleAccounts.size(); i++)
328       {
329         if(PossibleAccounts[i].id == AttachAccount.id){Match=true;}
330       }
331       if(Match==false){PossibleAccounts.Add(AttachAccount);}
332     }
333   }
334    /****END Values and Search Methods ****/
335    
336    /*****LEAD CONVERSION *****/
337    public void ConvertLeads()
338    {
339      Database.LeadConvert lc = new database.LeadConvert();
340      lc.setLeadID(ThisLead.id);   //Set ID of the Lead being converted
341      lc.setOverwriteLeadSource(false);  
342      if(AttachAccount.id != null){lc.setAccountId(AttachAccount.ID);} //Set the ID of Existing Account
343      lc.setConvertedStatus(ConvertStatus);  //Set Convert Status of Lead
344      lc.setOwnerId(RecordOwnerOpp.OwnerId); //Set the Owner of the Opportunity to be created
345      lc.setDoNotCreateOpportunity(OpportunityCreate); //Set if an Opportunity will not be created
346      lc.setSendNotificationEmail(NotifyOwner); //Sets if owner gets an e-mail
347      //Set ContactID for Exisiting Contact
348      if(AttachContact.ID != null)
349      {
350        lc.setContactID(AttachContact.id);system.debug('Contact Attached: '+ AttachContact.id);
351      }
352      //Set Name of the Opportunity to be Created if no Opportunity currently exists
353      if(AttachOpportunity.ID == null)
354      {
355        lc.setOpportunityName(RecordOwnerOpp.Name); system.debug('Set Opportunity Name: ' + RecordOwnerOpp.Name);    
356      } 
357      
358      system.debug('LeadConvert before Processed: ' + lc);
359      /*Convert Lead and get Results of Conversion*/
360    
361      lcr = null;
362      try
363      {  
364        lcr = database.convertLead(lc); //Converts Lead and returns results as a variable
365      system.debug('LeadConvert Errors: ' + lcr.getErrors());
366      //system.assert(lcr.isSuccess(),'Lead Insert Failed: ' + lcr.getErrors());         
367      } catch (exception e)
368        {ApexPages.addMessages(e);}
369      /*Send Opportunity and lead to Lead_update_class to move Lead_Activity Tasks */
370      if(AttachOpportunity.ID == null)
371      {
372        Lead_Update_class.Junction_to_Opportunity(ThisLead.id,lcr.getOpportunityId() );
373      }else
374      {
375        Lead_Update_class.Junction_to_Opportunity(ThisLead.id,AttachOpportunity.id);
376      } 
377    }
378    private transient database.LeadConvertResult lcr = null;
380    public PageReference Cust_ConvertLead()
381    {     
382      system.debug('CONVERT LEAD FIRED!');
383      system.debug('Record Owner: ' + RecordOwnerOpp.OwnerID);
384      system.debug('AttachAccount: ' + AttachAccount);
385      system.debug('AttachAccoundID:' + AttachAccountID);     
386      system.debug('AttachContact: ' + AttachContact);
387      system.debug('AttachContactID: ' + AttachContactID);
388      system.debug('AttachOpportunity: ' + AttachOpportunity);
389      system.debug('AttachOpportunityID: ' + AttachOpportunityID) ;    
390      system.debug('OpportunityName: ' + RecordOwnerOpp.Name);
391      system.debug('Do Not Create Opportunity: ' + OpportunityCreate);
392      system.debug('Convert Status: ' + ConvertStatus);
394      /*Error Messages Here*/ 
395      //Record Owner is not null
396      if(RecordOwnerOpp.OwnerID == null)
397      {


ShashForceShashForce
Hi,

line 372:
Lead_Update_class.Junction_to_Opportunity(ThisLead.id,lcr.getOpportunityId() );

So we need to look at "Lead_Update_class" and check if there are any exceptions that are ignored, like using a null reference somewhere in the class.

Thanks,
Shashank
Lisa Horne 1Lisa Horne 1
Thanks for your response.  Is this something you could help me with?  I am not very  familar with Apex.


Here is the code for the Lead update Class:

1 public with sharing class Lead_Update_class 
2 {
3   public static void Create_Lead_Activity(list<Lead> lLeads)
4   {
5     system.debug('Create Lead Activity Junction Fired!!!');
6     system.debug('Reviewing Leads: ' + lLeads.size());
7     list<Lead_Activity__c> lLead_Act = new list<Lead_Activity__c>();
8     for(Lead l : lLeads)
9     {
10       if(l.Account__c != null && l.Contact__c != null)
11       {
12         system.debug('Creating Junction Object: ' + l.id);
13         lLead_Act.add(new Lead_Activity__c (
14           Lead__c = l.id,
15           Account__c = l.Account__c,
16           Contact__c = l.Contact__c          
17           )
18         );
19       }else{system.debug('Junction not Created missing information:' + l.id);}      
20     }
21     //Insert Junctions
22     if(lLead_Act.size() > 0)
23     {
24       system.debug('Inserting Junction Objects: ' + lLead_Act.size());
25       try
26       {
27         insert lLead_act;
28       }
29       catch(system.DMLException e)
30       {
31         system.debug('ERROR ERROR!: ' + e.getmessage());
32         system.assert(false, e.getMessage());
33       }  
34     }
35   //EOM
36   }
37   @future
38   public static void Junction_to_Juntion(set<string> Lead_ActIDs, set<Id> sLeadIDs,map<string,id> mLeadID_ActID )
39   {
40     Global_Apex_Calls_Class.fireTriggers = false;
41     system.debug('Starting to move Junction to Junction!');
42     system.debug('Lead_ActIDs: ' + Lead_ActIds); System.debug('LeadIDs: ' + sLeadIds); system.debug('Number in map: ' + mleadid_actid.size());
43     list<Lead_Activity__c> lLead_Acts = [select id, Lead__c, Account__c, Contact__c from Lead_Activity__c where id in :Lead_ActIDs];
44     //list<Lead> lLeads = [select id, Account__c, Contact__c, Lead_Activity__c from Lead where id in:sLeadIds]; 
45     system.debug('Number of Lead junctions: ' + lLead_Acts.size());
46     map<Id,Lead> mLeads = new map<id,lead>();
47     for(Lead l:[select id, Account__c, Contact__c, Lead_Activity__c from Lead where id in:sLeadIds])
48     {
49       mLeads.put(l.id, l);
50     }
51     system.debug('Number of Map from Lead to Junction: ' + mLeads.size());
52     list<Task> lUpdateTasks = new list<Task>();
53     string vWhoId = null; string vWhatId=null;
54     system.debug('Updating Tasks');
55     for(Task t : [select whoid, whatid, id from Task where whatid in :Lead_ActIds])
56     {
57       vWhoID = t.WhoId; vWhatID = t.Whatid;
58       system.debug('Exisitng Whatid: ' + t.WhatID);
59       system.debug('Exisitng WhoID: ' + t.whoid);
60       system.debug('Lead Activity being moved to: '+ mLeads.get(mLeadID_ActID.get(vWhatID)).Lead_Activity__c);
61       t.WhatId =  mLeads.get(mLeadID_ActID.get(vWhatID)).Lead_Activity__c;
62       t.WhoId = mLeads.get(mLeadID_ActID.get(vWhatID)).Contact__c;
63       system.debug('New WhatId: ' + t.WhatId); system.debug('New WhoId: ' + t.WhoID);
64       if(lUpdateTasks.size() <1000)
65       {
66         lUpdateTasks.add(t); system.debug('Adding Task Updated');
67       }else
68       { 
69         system.debug('more than 1000 leads!'); 
70         update lUpdateTasks; lUpdateTasks = new list<Task>(); lupdateTasks.add(t);
71       }
72     }
73     system.debug('After looping for tasks Total tasks remaining to update: ' + lupdatetasks.size());
74     if(lUpdateTasks.size() > 0){update lUpdateTasks;}
75     //Events
76     list<Event> lUpdateEvents = new list<Event>();
77     for(Event e : [select whoid, whatid, id from Event where whatid in :Lead_ActIds])
78     {
79       vWhoID = e.WhoId; vWhatID = e.Whatid;
80       e.WhatId =  mLeads.get(mLeadID_ActID.get(vWhatID)).Lead_Activity__c;
81       e.WhoId = mLeads.get(mLeadID_ActID.get(vWhatID)).Contact__c;
82       if(lUpdateEvents.size() <1000)
83       {
84         lUpdateEvents.add(e);
85       }else{update lUpdateEvents; lUpdateEvents = new list<Event>(); lUpdateEvents.add(e);}
86     }
87     if(lUpdateEvents.size() >0){update lUpdateEvents;}
88     
89     Boolean Exists = false;
90     for(Task t : [select whoid, whatid, id from Task where whatid in :Lead_ActIds])
91     {
92       Exists = true; system.debug('Task still existis! ' + 'Who: '+ t.whoid + ' What: '+ t.whatid + ' ID: ' + t.id);
93     }
94     for(Event e : [select whoid, whatid, id from Event where whatid in :Lead_ActIds])
95     {
96       Exists = true; system.debug('Event still existis! ' + 'Who: '+ e.whoid + ' What: '+ e.whatid + ' ID: ' + e.id);
97     }  
98     if(Exists == false && lLead_Acts.size() >0)  
99     {
100       system.debug('Deleting Lead Junctions: ' + lLead_Acts); delete lLead_Acts;
101     }else
102     {
103       system.debug('Activities on old junction: ' + lLead_Acts);
104       system.assert(false, 'Activities still exist on old Junction. Contact Administrator');
105     }
106   //EOM      
107   }
108   public static void Lead_to_Junction(set<ID> sMove)
109   {
110     system.debug('FIRING CLASS LEAD TO JUNCTION!!');
111     //Junctions
112     map<ID, Lead_Activity__c> mLeadAct = new map<ID,Lead_Activity__c>();
113     for(Lead_Activity__c LA : [Select L.Id, l.Account__c,  l.Lead__c, Contact__c from Lead_Activity__c l where l.lead__c in :sMove])
114     {
115       mLeadAct.put(la.Lead__c, LA);
116     }
117     //Tasks
118     list<Task> lTasks = new list<Task>();
119     string ActWhoID = null;
120     for(Task t: [select t.id, t.whoid, t.whatid from Task t where t.whoid in :sMove])
121     {
122       ActWhoID = t.WhoID;
123       if(mLeadAct.get(ActWhoID) != null)
124       {
125         t.WhatId = mLeadAct.get(ActWhoID).id;    
126         t.WhoId = mLeadAct.get(ActWhoID).Contact__c;
127         system.debug('Task ContactID: ' + mLeadAct.get(ActWhoID).Contact__c);
128         system.assert(t.whoid == mLeadAct.get(ActWhoID).Contact__c, 'ERROR ERROR WILL ROBINSON!' );
129         if(ltasks.size()<1000)
130         {
131           lTasks.add(t);  
132         }
133         else
134         {
135           try
136           {
137             update lTasks;
138           }
139           catch(system.DMLException e)
140           {
141             system.debug('ERROR ERROR!: ' + e.getmessage());
142             system.assert(false, e.getMessage());
143           }
144           ltasks = new list<Task>();
145           ltasks.add(t);
146         }
147       }
148     //End task loop
149     }
150     if(ltasks.size() > 0)
151     {
152       try
153       {
154         update lTasks;
155       }
156       catch(system.DMLException e)
157       {
158         system.debug('ERROR ERROR!: ' + e.getmessage());
159         system.assert(false, e.getMessage());
160       }        
161     }
162     //Events
163     list<Event> lEvents = new list<Event>();    
164     for(Event ev : [Select e.id, e.whoid, e.whatid from Event e where e.whoid in :sMove])
165     {
166       ActWhoID = ev.WhoID;
167       if(mLeadAct.get(ActWhoId) != null)
168       {
169         ev.WhatId = mLeadAct.get(ActWhoID).id;    
170         ev.WhoId = mLeadAct.get(ActWhoID).Contact__c;  
171         if(lEvents.size()<1000)
172         {
173           lEvents.add(ev);  
174         }
175         else
176         {
177           try
178           {
179             update lEvents;
180           }
181           catch(system.DMLException e)
182           {
183             system.debug('ERROR ERROR!: ' + e.getmessage());
184             system.assert(false, e.getMessage());
185           }  
186           lEvents = new list<Event>();
187           lEvents.add(ev);
188         }
189       }          
190     //End Event loop
191     }
192     if(lEvents.size() > 0)
193     {
194       try
195       {
196         update lEvents;
197       }
198       catch(system.DMLException e)
199       {
200         system.debug('ERROR ERROR!: ' + e.getmessage());
201         system.assert(false, e.getMessage());
202       }
203     }    
204   //EOM  
205   }
206   /*Move Activities from Junction to Opportunity. Called from Cust_Lead_Convert_Controller upon conversion*/
207   public static void Junction_to_Opportunity(ID LeadID, ID OppID)
208   {
209     system.debug('Firing Junction_To_Opportunity!');
210     system.debug('Converted Lead: ' + LeadID); system.debug('Opportunity: ' + OppId);
211     list<Task> lTasks = new list<Task>();
212     list<Event> lEvents = new list<Event>();
213     set<Id> sLead_ActIDs = new set<Id>();
214     //Build set of Lead Activities just in case there are more than one!
215     list<Lead_Activity__c> Lead_Acts = [Select L.Id, l.Account__c,  l.Lead__c, Contact__c from Lead_Activity__c l where l.lead__c = :LeadID]; 
216     for(Lead_Activity__c L : Lead_Acts)
217     {
218       sLead_ActIDs.add(l.id);
219     }
220     //Loop through Tasks
221     for(Task t: [select t.id, t.whoid, t.whatid from Task t where t.whatid in :sLead_ActIDs])
222     {
223       if(Lead_Acts.size() >0)
224       {
225         t.WhatId = OppId;  
226                 
227         if(ltasks.size()<1000)
228         {
229           lTasks.add(t);  
230         }
231         else
232         {
233           try
234           {
235             update lTasks;
236           }
237           catch(system.DMLException e)
238           {
239             system.debug('ERROR ERROR!: ' + e.getmessage());
240             system.assert(false, e.getMessage());
241           }
242           ltasks = new list<Task>();
243           ltasks.add(t);
244         }
245       }      
246     //End Task Loop
247     }
248     if(ltasks.size() > 0)
249     {
250       try
251       {
252         update lTasks;
253       }
254       catch(system.DMLException e)
255       {
256         system.debug('ERROR ERROR!: ' + e.getmessage());
257         system.assert(false, e.getMessage());
258       }        
259     }
260     //Loop through Events
261     for(Event ev : [Select e.id, e.whoid, e.whatid from Event e where e.whatid in :sLead_ActIDs])
262     {
263       if(Lead_Acts.size() >0)
264       {
265         ev.WhatId = OppId;
266         if(lEvents.size()<1000)
267         {
268           lEvents.add(ev);  
269         }
270         else
271         {
272           try
273           {
274             update lEvents;
275           }
276           catch(system.DMLException e)
277           {
278             system.debug('ERROR ERROR!: ' + e.getmessage());
279             system.assert(false, e.getMessage());
280           }  
281           lEvents = new list<Event>();
282           lEvents.add(ev);
283         }
284       }        
285     //End event loop
286     }
287     if(lEvents.size() > 0)
288     {
289       try
290       {
291         update lEvents;
292       }
293       catch(system.DMLException e)
294       {
295         system.debug('ERROR ERROR!: ' + e.getmessage());
296         system.assert(false, e.getMessage());
297       }
298     }    
299     //Delete if no more Tasks exist
300     Boolean SafeToDelete = true;
301     for(Task t: [select t.id, t.whoid, t.whatid from Task t where t.whatid in :sLead_ActIDs])
302     {    
303       SafeToDelete =false;
304     }
305     for(Event ev : [Select e.id, e.whoid, e.whatid from Event e where e.whatid in :sLead_ActIDs])
306     {  
307       SafeToDelete =false;
308     }  
309     system.debug('SafetoDelete = ' + SafetoDelete);
310     try
311     {
312       delete Lead_Acts;
313     }
314     catch(system.DMLException e)
315     {
316         system.debug('ERROR ERROR!: ' + e.getmessage());
317         system.assert(false, e.getMessage());      
318     }
319   //EOM  
320   }
321   @future
322   public static void Lead_DoubleCheck(set <Id> sCheck)
323   {
324     list<Lead> lUpdate = [Select id, Account__c, Contact__c, Lead_Activity__c, Description from Lead where Id in :sCheck];
325     for(integer i=0; i<lUpdate.size(); i++)
326     {
327       lUpdate[i].Lead_Activity__c = null;      
328     }
329     update lUpdate;
330   //EOM
331   }  
332 //EOF
333 }