-
ChatterFeed
-
0Best Answers
-
0Likes Received
-
0Likes Given
-
4Questions
-
3Replies
Apex trigger not working during data loader upload.
I need to alter my trigger to run on Batch jobs as well as when they are updated one at a time. Could someone help me do that? This trigger creates a task when a lead is created and the Lead Web Comment field is filled out.
trigger CreateTaskOnLead on Lead (after insert) {
List<Task> lTask = new List<Task>();
Task t;
if(Trigger.isAfter) {
if(Trigger.isInsert) {
for(Lead l: Trigger.new) {
if((l.Lead_Web_Comment__c != null)){
t = new Task();
t.OwnerId = l.OwnerId;
t.Subject = 'Lead Web Comment';
t.Priority = 'Normal';
t.Status = 'Completed';
t.Type = 'Lead Web Comment';
t.Description = l.Lead_Web_Comment__c;
t.ActivityDate = Date.today();
// t.CallDuration=0;
t.whoid=l.id;
lTask.add(t);
}
}
if(!lTask.IsEmpty())
insert t;
}
}
}
trigger CreateTaskOnLead on Lead (after insert) {
List<Task> lTask = new List<Task>();
Task t;
if(Trigger.isAfter) {
if(Trigger.isInsert) {
for(Lead l: Trigger.new) {
if((l.Lead_Web_Comment__c != null)){
t = new Task();
t.OwnerId = l.OwnerId;
t.Subject = 'Lead Web Comment';
t.Priority = 'Normal';
t.Status = 'Completed';
t.Type = 'Lead Web Comment';
t.Description = l.Lead_Web_Comment__c;
t.ActivityDate = Date.today();
// t.CallDuration=0;
t.whoid=l.id;
lTask.add(t);
}
}
if(!lTask.IsEmpty())
insert t;
}
}
}
- Lisa Horne 1
- August 20, 2014
- Like
- 0
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?
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 {
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 {
- Lisa Horne 1
- July 08, 2014
- Like
- 0
Help with this Roll up Trigger error
I needed a trigger that triggered a roll up summary on the Accounts (Total_Active_Contracts__c field) from Contracts(Active_Contract_Count__c Field). I keep getting this error.... could someone please help?
Error Error: Compile Error: Invalid field: 'Account' at line 25 column 27
trigger ContractRollUpActiveContractCount on Account (after delete, after insert, after update) {
//Limit the size of list by using Sets which do not contain duplicate elements
set<id> AccountIds = new set<id>();
//When adding new Contracts or updating existing Contracts
if(trigger.isInsert || trigger.isUpdate){
for(Contract p : trigger.new){
AccountIds.add(p.Account);
}
}
//When deleting Contracts
if(trigger.isDelete){
for(Contract p : trigger.old){
AccountIds.add(p.Account);
}
}
//Map will contain one Contract Id to one sum value
map<id,double> AccountMap = new map <id,double>();
//Produce a sum of Total_Active_Contracts__c and add them to the map
//use group by to have a single Contract Id with a single sum value
for(AggregateResult q : [select Account,sum(Active_Contract_Count__c)
from Contract where Account IN :AccountIdsgroup group by Account]){
AccountMap.put((Id)q.get('Account'),(Double)q.get('expr0'));
}
List AccountsToUpdate = new List();
//Run the for loop on Contract using the non-duplicate set of Contract Ids
//Get the sum value from the map and create a list of Opportunities to update
for(Account o : [Select Id, Total_Active_Contracts__c from Account where Id IN :AccountIds]){
Double Active_Contract_Count__cSum = AccountMap.get(o.Id);
o.Total_Active_Contracts__c = ContractSum;
AccountToUpdate.add(o);
}
update AccountToUpdate;
}
Error Error: Compile Error: Invalid field: 'Account' at line 25 column 27
trigger ContractRollUpActiveContractCount on Account (after delete, after insert, after update) {
//Limit the size of list by using Sets which do not contain duplicate elements
set<id> AccountIds = new set<id>();
//When adding new Contracts or updating existing Contracts
if(trigger.isInsert || trigger.isUpdate){
for(Contract p : trigger.new){
AccountIds.add(p.Account);
}
}
//When deleting Contracts
if(trigger.isDelete){
for(Contract p : trigger.old){
AccountIds.add(p.Account);
}
}
//Map will contain one Contract Id to one sum value
map<id,double> AccountMap = new map <id,double>();
//Produce a sum of Total_Active_Contracts__c and add them to the map
//use group by to have a single Contract Id with a single sum value
for(AggregateResult q : [select Account,sum(Active_Contract_Count__c)
from Contract where Account IN :AccountIdsgroup group by Account]){
AccountMap.put((Id)q.get('Account'),(Double)q.get('expr0'));
}
List AccountsToUpdate = new List();
//Run the for loop on Contract using the non-duplicate set of Contract Ids
//Get the sum value from the map and create a list of Opportunities to update
for(Account o : [Select Id, Total_Active_Contracts__c from Account where Id IN :AccountIds]){
Double Active_Contract_Count__cSum = AccountMap.get(o.Id);
o.Total_Active_Contracts__c = ContractSum;
AccountToUpdate.add(o);
}
update AccountToUpdate;
}
- Lisa Horne 1
- June 30, 2014
- Like
- 0
Help with a trigger for Rollup Summary field from Contracts
I need help with creating a trigger to create a Rollup Summary on the Accounts object from the Contracts object. The field from the Contract that I need to rollup onto the Account screen is called Active_Contract_Count__c.
Could someone please help?
Could someone please help?
- Lisa Horne 1
- June 27, 2014
- Like
- 0
Apex trigger not working during data loader upload.
I need to alter my trigger to run on Batch jobs as well as when they are updated one at a time. Could someone help me do that? This trigger creates a task when a lead is created and the Lead Web Comment field is filled out.
trigger CreateTaskOnLead on Lead (after insert) {
List<Task> lTask = new List<Task>();
Task t;
if(Trigger.isAfter) {
if(Trigger.isInsert) {
for(Lead l: Trigger.new) {
if((l.Lead_Web_Comment__c != null)){
t = new Task();
t.OwnerId = l.OwnerId;
t.Subject = 'Lead Web Comment';
t.Priority = 'Normal';
t.Status = 'Completed';
t.Type = 'Lead Web Comment';
t.Description = l.Lead_Web_Comment__c;
t.ActivityDate = Date.today();
// t.CallDuration=0;
t.whoid=l.id;
lTask.add(t);
}
}
if(!lTask.IsEmpty())
insert t;
}
}
}
trigger CreateTaskOnLead on Lead (after insert) {
List<Task> lTask = new List<Task>();
Task t;
if(Trigger.isAfter) {
if(Trigger.isInsert) {
for(Lead l: Trigger.new) {
if((l.Lead_Web_Comment__c != null)){
t = new Task();
t.OwnerId = l.OwnerId;
t.Subject = 'Lead Web Comment';
t.Priority = 'Normal';
t.Status = 'Completed';
t.Type = 'Lead Web Comment';
t.Description = l.Lead_Web_Comment__c;
t.ActivityDate = Date.today();
// t.CallDuration=0;
t.whoid=l.id;
lTask.add(t);
}
}
if(!lTask.IsEmpty())
insert t;
}
}
}
- Lisa Horne 1
- August 20, 2014
- Like
- 0
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?
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 {
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 {
- Lisa Horne 1
- July 08, 2014
- Like
- 0
Help with this Roll up Trigger error
I needed a trigger that triggered a roll up summary on the Accounts (Total_Active_Contracts__c field) from Contracts(Active_Contract_Count__c Field). I keep getting this error.... could someone please help?
Error Error: Compile Error: Invalid field: 'Account' at line 25 column 27
trigger ContractRollUpActiveContractCount on Account (after delete, after insert, after update) {
//Limit the size of list by using Sets which do not contain duplicate elements
set<id> AccountIds = new set<id>();
//When adding new Contracts or updating existing Contracts
if(trigger.isInsert || trigger.isUpdate){
for(Contract p : trigger.new){
AccountIds.add(p.Account);
}
}
//When deleting Contracts
if(trigger.isDelete){
for(Contract p : trigger.old){
AccountIds.add(p.Account);
}
}
//Map will contain one Contract Id to one sum value
map<id,double> AccountMap = new map <id,double>();
//Produce a sum of Total_Active_Contracts__c and add them to the map
//use group by to have a single Contract Id with a single sum value
for(AggregateResult q : [select Account,sum(Active_Contract_Count__c)
from Contract where Account IN :AccountIdsgroup group by Account]){
AccountMap.put((Id)q.get('Account'),(Double)q.get('expr0'));
}
List AccountsToUpdate = new List();
//Run the for loop on Contract using the non-duplicate set of Contract Ids
//Get the sum value from the map and create a list of Opportunities to update
for(Account o : [Select Id, Total_Active_Contracts__c from Account where Id IN :AccountIds]){
Double Active_Contract_Count__cSum = AccountMap.get(o.Id);
o.Total_Active_Contracts__c = ContractSum;
AccountToUpdate.add(o);
}
update AccountToUpdate;
}
Error Error: Compile Error: Invalid field: 'Account' at line 25 column 27
trigger ContractRollUpActiveContractCount on Account (after delete, after insert, after update) {
//Limit the size of list by using Sets which do not contain duplicate elements
set<id> AccountIds = new set<id>();
//When adding new Contracts or updating existing Contracts
if(trigger.isInsert || trigger.isUpdate){
for(Contract p : trigger.new){
AccountIds.add(p.Account);
}
}
//When deleting Contracts
if(trigger.isDelete){
for(Contract p : trigger.old){
AccountIds.add(p.Account);
}
}
//Map will contain one Contract Id to one sum value
map<id,double> AccountMap = new map <id,double>();
//Produce a sum of Total_Active_Contracts__c and add them to the map
//use group by to have a single Contract Id with a single sum value
for(AggregateResult q : [select Account,sum(Active_Contract_Count__c)
from Contract where Account IN :AccountIdsgroup group by Account]){
AccountMap.put((Id)q.get('Account'),(Double)q.get('expr0'));
}
List AccountsToUpdate = new List();
//Run the for loop on Contract using the non-duplicate set of Contract Ids
//Get the sum value from the map and create a list of Opportunities to update
for(Account o : [Select Id, Total_Active_Contracts__c from Account where Id IN :AccountIds]){
Double Active_Contract_Count__cSum = AccountMap.get(o.Id);
o.Total_Active_Contracts__c = ContractSum;
AccountToUpdate.add(o);
}
update AccountToUpdate;
}
- Lisa Horne 1
- June 30, 2014
- Like
- 0