-
ChatterFeed
-
0Best Answers
-
0Likes Received
-
0Likes Given
-
3Questions
-
1Replies
UNABLE_TO_LOCK_ROW problem
Hello everyone!
At the beginning we were receiving this error randomly a couple of times a week, but with the fast growth of our database I am finding these errors more often and I would like to know if is there a possible solution for it or we should "live" with it...
The error is the following:
Salesforce could not create this lead because of the reason listed below. For more information about this error or help with Web-to-Case-Lead, please contact Customer Support.
Reason: Apex trigger leadAutoConvert caused an unexpected exception, contact your administrator: leadAutoConvert: execution of AfterInsert
caused by: System.DmlException: ConvertLead failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, System.DmlException: Update failed. First exception on row 0 with id 0031a00000bVrTtAAK; first error: UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record or 1 records: 0031a00000bVrTtAAK: []
Class.leadconvert.BulkLeadConvert.handleRegularContactUpdates: line 350, column 1
Class.leadconvert.BulkLeadConvert.convertLead: line 98, column 1: []: Trigger.leadAutoConvert: line 71, column 1
Lead Capture Page: https://webto.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8
What we have is a process that automatically converts Leads into Contacts and if the lead (having the email as the key field) was already converted then it updates the Contact for some fields stated in the Class, thus preventing duplicates (although I can see some from time to time)
This is the code of the leadAutoConvert trigger that is the one that I think is failing...
We also have this other Trigger, that I do not know if it can affect something...
Finally we have this class, that is the one that I touch whenever I want to add a hierarchy status or a field to override..
The error doesn´t happen always, but when someone clicks to fast in a submit button or something similar... Also, let you know that we have 40 Processes inside Process Builder...
Any recommendation, solution, question, answer is deeply appreciated..
Thanks!!
At the beginning we were receiving this error randomly a couple of times a week, but with the fast growth of our database I am finding these errors more often and I would like to know if is there a possible solution for it or we should "live" with it...
The error is the following:
Salesforce could not create this lead because of the reason listed below. For more information about this error or help with Web-to-Case-Lead, please contact Customer Support.
Reason: Apex trigger leadAutoConvert caused an unexpected exception, contact your administrator: leadAutoConvert: execution of AfterInsert
caused by: System.DmlException: ConvertLead failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, System.DmlException: Update failed. First exception on row 0 with id 0031a00000bVrTtAAK; first error: UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record or 1 records: 0031a00000bVrTtAAK: []
Class.leadconvert.BulkLeadConvert.handleRegularContactUpdates: line 350, column 1
Class.leadconvert.BulkLeadConvert.convertLead: line 98, column 1: []: Trigger.leadAutoConvert: line 71, column 1
Lead Capture Page: https://webto.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8
What we have is a process that automatically converts Leads into Contacts and if the lead (having the email as the key field) was already converted then it updates the Contact for some fields stated in the Class, thus preventing duplicates (although I can see some from time to time)
This is the code of the leadAutoConvert trigger that is the one that I think is failing...
Trigger leadAutoConvert on Lead (after insert) { Set<String> leademails = new Set<String>(); List<Lead> newLeads = new List<Lead>(); // Get all the new leads for(Lead l : system.trigger.new){ newLeads.add(l); if(l.email!=null) leademails.add(l.email); } /* Make some maps of Contact and email addresses */ List<Contact> ContactList = [select Id, Email, OwnerId,accountid from Contact where Email IN: leademails]; Map<ID, String> Contacts = new Map<ID, String>(); Map<ID, String> accountsids = new Map<ID, String>(); Map<String,ID> contactOwnerMap = new Map<String,ID>(); // Generic map for preventing loss of ids for(Contact c : ContactList){ Contacts.put(c.id, c.Email); accountsids.put(c.accountid, c.Email); contactOwnerMap.put(c.Email,c.OwnerId); } // We will need this to get the id from the email address Map<String, ID> accountFlipped = new Map<String, ID>(); for(ID i : accountsids.keyset()){ accountFlipped.put(accountsids.get(i), i); } Map<String, ID> ContactFlipped = new Map<String, ID>(); for(ID i : Contacts.keyset()){ ContactFlipped.put(Contacts.get(i), i); } Map<String,String> LeadWithLeadRegionMap = new Map<String,String>(); Map<String,String> AccountWithLeadRegionMap = new Map<String,String>(); Map<String,String> ContactWithLeadRegionMap = new Map<String,String>(); /* System Conversion Requirements */ leadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1]; List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>(); Database.LeadConvert lc = new Database.LeadConvert(); /* Configuring Payload */ for (Lead nl : newleads) { lc = new Database.LeadConvert(); lc.setLeadId(nl.id); lc.setOverwriteLeadSource(false); lc.setConvertedStatus(convertStatus.MasterLabel); system.debug(Contacts.size()); LeadWithLeadRegionMap.put(nl.id,nl.Region__c); // Check to see if Contact already exists if(!Contacts.isEmpty()){ if(nl.email != null && ContactFlipped.get(nl.email)!=null && accountFlipped.get(nl.email) != null){ lc.setAccountId(accountFlipped.get(nl.email)); lc.setContactId(ContactFlipped.get(nl.email)); lc.setDoNotCreateOpportunity(true); } } else { // In the event an Contact doesn't exist lc.setOwnerId(nl.OwnerId); lc.setDoNotCreateOpportunity(true); } leadConverts.add(lc); } // Fire Payload Database.LeadConvertResult[] lcr = Database.convertLead(leadConverts); System.debug(LoggingLevel.INFO, lcr); Set<ID> AccountIDSet = new Set<ID>(); Set<ID> ContactIDSet = new Set<ID>(); for(Database.LeadConvertResult dbresult : lcr ){ system.debug('====lead id==='+dbresult.getLeadId()); system.debug('====Account id==='+dbresult.getAccountId()); system.debug('====Contact id==='+dbresult.getContactId()); system.debug('====LeadWithLeadRegionMap.get(dbresult.getLeadId())==='+LeadWithLeadRegionMap.get(dbresult.getLeadId())); AccountIDSet.add(dbresult.getAccountId()); ContactIDSet.add(dbresult.getContactId()); if(LeadWithLeadRegionMap.get(dbresult.getLeadId()) != null){ AccountWithLeadRegionMap.put(dbresult.getAccountId(),LeadWithLeadRegionMap.get(dbresult.getLeadId())); ContactWithLeadRegionMap.put(dbresult.getContactId(),LeadWithLeadRegionMap.get(dbresult.getLeadId())); }else{ AccountWithLeadRegionMap.put(dbresult.getAccountId(),''); ContactWithLeadRegionMap.put(dbresult.getContactId(),''); } } List<User> BreadUser = [ Select id,Name from User where name = 'Brad Heringer']; List<User> JakeUser = [ Select id,Name from User where name = 'Jake Strich']; if(AccountIDSet != null && AccountIDSet.size() > 0){ List<Account> Accountlist = [Select id,Name,OwnerID from Account where id in: AccountIDSet]; if(Accountlist != null && Accountlist.size() > 0){ for(Account ac : Accountlist){ if(AccountWithLeadRegionMap.get(ac.id) != null) { String Regionname = AccountWithLeadRegionMap.get(ac.id); if(Regionname == 'San Francisco' || Regionname == 'Silicon Valley' || Regionname == 'Los Angeles' || Regionname == 'Seattle' || Regionname == 'Online' ) { if(JakeUser != null && JakeUser.size() > 0) ac.OwnerID = JakeUser.get(0).id; } if(Regionname == '' || Regionname == 'New York' || Regionname == 'Boston' || Regionname == 'Austin' || Regionname == 'Chicago' ) { if(BreadUser != null && BreadUser.size() > 0) ac.OwnerID = BreadUser.get(0).id; } }else{ if(BreadUser != null && BreadUser.size() > 0) ac.OwnerID = BreadUser.get(0).id; } } Update Accountlist; } } if(ContactIDSet != null && ContactIDSet.size() > 0){ List<Contact> ContactlisttoUpdate = [Select id,Name,Email,OwnerID from Contact where id in: ContactIDSet]; if(ContactlisttoUpdate != null && ContactlisttoUpdate.size() > 0){ for(Contact con : ContactlisttoUpdate){ if(ContactWithLeadRegionMap.get(con.id) != null) { String Regionname = ContactWithLeadRegionMap.get(con.id); if((Regionname == null || Regionname == '') && contactOwnerMap.containsKey(con.Email)){ con.OwnerID = contactOwnerMap.get(con.Email); }else if(Regionname == 'San Francisco' || Regionname == 'Silicon Valley' || Regionname == 'Los Angeles' || Regionname == 'Seattle' || Regionname == 'Online' ) { if(JakeUser != null && JakeUser.size() > 0) con.OwnerID = JakeUser.get(0).id; }else if(Regionname == '' || Regionname == 'New York' || Regionname == 'Boston' || Regionname == 'Austin' || Regionname == 'Chicago' ) { if(BreadUser != null && BreadUser.size() > 0) con.OwnerID = BreadUser.get(0).id; } }else{ if(BreadUser != null && BreadUser.size() > 0) con.OwnerID = BreadUser.get(0).id; } } Update ContactlisttoUpdate; } } }
We also have this other Trigger, that I do not know if it can affect something...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 trigger customizationOnLeadConvert on Lead (after update) { Set<ID> leadIDSet = new Set<ID>(); for (Lead l : trigger.new) { if (l.ConvertedContactId != null) { leadIDSet.add(l.id); } if (l.convertedAccountId != null) { leadIDSet.add(l.id); } } LeadFutureClass.CustomizationOnLeadConvertMethod(leadIDSet); }
Finally we have this class, that is the one that I touch whenever I want to add a hierarchy status or a field to override..
Global class LeadFutureClass { @future public static void CustomizationOnLeadConvertMethod(set<ID> LeadIDSet) { Set<String> ContactIDSet = new Set<String>(); Set<String> AccountIDSet = new Set<String>(); List<Lead> Leadlist = [Select id,Source__c,name,ConvertedContactId,Master_Record_Type__c,Course_Type_Applied__c,Coding_Course_Status__c,Speakers_Status__c,Instructors_Status__c,Asked_something_through_Olark__c,Registered_at_Blog_Newsletter__c,Registered_at_PS_Slack_Group__c,Registered_at_Sumome__c,Requested_Invitation_to_Slack_PS_Group__c,convertedAccountId,FirstName,LastName,Email,Phone,Status__c,Region__c,LinkedIn__c,Last_Event_Attended__c,Last_Event_Attendee_Status__c,When_are_you_looking_to_take_the_course__c,Book_requested__c,TESTFIELD2__c from Lead where id in: LeadIDSet]; for (Lead l : Leadlist) { if (l.ConvertedContactId != null) { ContactIDSet.add(l.ConvertedContactId); } if (l.convertedAccountId != null) { AccountIDSet.add(l.convertedAccountId); } } List<Account> Accountlist = new List<Account>(); List<Contact> Contactlist = new List<Contact>(); if(AccountIDSet != null && AccountIDSet.size() > 0 && ContactIDSet != null && ContactIDSet.size() > 0 ) { Map<Id,Account> AccountMap = new Map<Id,Account>(); for(Account ac : [SELECT Id FROM Account WHERE Id in: AccountIDSet]){ AccountMap.put(ac.id,ac); } Map<Id,Contact> ContactMap = new Map<Id,Contact>(); for(Contact con : [Select Id,Status__c,Coding_Course_Status__c,Speakers_Status__c,Instructors_Status__c,Name From Contact Where Id in: ContactIDSet ]){ ContactMap.put(con.id,con); } for (Lead l : Leadlist) { Map<String,Integer > statusvalues = new Map<String,Integer >(); statusvalues.put('Graduate',1); statusvalues.put('Student',2); statusvalues.put('Cohort Completed',3); statusvalues.put('Past Speaker',4); statusvalues.put('Currently Teaching',5); statusvalues.put('Onboarding Step 2',6); statusvalues.put('Onboarding Step 1',7); statusvalues.put('Confirmed Instructor',8); statusvalues.put('Confirmed Speaker',9); statusvalues.put('Payment Received',10); statusvalues.put('Approved Not Confirmed',11); statusvalues.put('Contract Sent',12); statusvalues.put('Event Details Sent',13); statusvalues.put('Payment Link Sent',14); statusvalues.put('Onsite Visit Scheduled / Videocall',15); statusvalues.put('Invited to Event',16); statusvalues.put('Presentation Scheduled',17); statusvalues.put('Casual Meeting Scheduled',18); statusvalues.put('Phone Call Scheduled',19); statusvalues.put('Met at Event / Personal Interaction',20); statusvalues.put('Replied',21); statusvalues.put('Follow Up Required',22); statusvalues.put('Application Submitted - Emailed No Response',23); statusvalues.put('Application Submitted',24); statusvalues.put('New Leads - Emailed No Response',25); statusvalues.put('New Leads',26); statusvalues.put('Future Instructor',27); statusvalues.put('Future Speaker',28); statusvalues.put('Future Course',29); statusvalues.put('Promotional Workshop - Emailed No Response',30); statusvalues.put('Promotional Workshop',31); statusvalues.put('Regular Workshop - Emailed No Response',32); statusvalues.put('Regular Workshop',33); statusvalues.put('RSVP',34); statusvalues.put('Potential Instructor - Emailed No Response',35); statusvalues.put('Potential Instructor',36); statusvalues.put('Closed - Lost',37); statusvalues.put('Potential Speaker - Emailed No Response',38); statusvalues.put('Potential Speaker',39); statusvalues.put('Never Replied',40); statusvalues.put('Early Stage - Emailed',41); statusvalues.put('Early Stage',42); if (l.convertedAccountId != null) { Account acc = new Account(); acc = AccountMap.get(l.convertedAccountId); if(l.FirstName!=null) acc.Name = l.FirstName+' '+l.LastName; else acc.Name = l.LastName; Accountlist.add(acc); } if(l.ConvertedContactId != null) { Contact c = new Contact(); c = ContactMap.get(l.ConvertedContactId); if(l.Phone!=null) c.Phone = l.Phone; if(l.Email!=null) c.Email = l.Email; if(l.Status__c!=null ) { if(c.Status__c == null) c.Status__c= l.Status__c; else if(statusvalues.get(c.Status__c) > statusvalues.get(l.Status__c)) c.Status__c= l.Status__c; } if(l.Coding_Course_Status__c!=null ) { if(c.Coding_Course_Status__c == null) c.Coding_Course_Status__c= l.Coding_Course_Status__c; else if(statusvalues.get(c.Coding_Course_Status__c) > statusvalues.get(l.Coding_Course_Status__c)) c.Coding_Course_Status__c= l.Coding_Course_Status__c; } if(l.Speakers_Status__c!=null ) { if(c.Speakers_Status__c == null) c.Speakers_Status__c= l.Speakers_Status__c; else if(statusvalues.get(c.Speakers_Status__c) > statusvalues.get(l.Speakers_Status__c)) c.Speakers_Status__c= l.Speakers_Status__c; } if(l.Instructors_Status__c!=null ) { if(c.Instructors_Status__c == null) c.Instructors_Status__c= l.Instructors_Status__c; else if(statusvalues.get(c.Instructors_Status__c) > statusvalues.get(l.Instructors_Status__c)) c.Instructors_Status__c= l.Instructors_Status__c; } if(l.Region__c!=null) c.Region__c = l.Region__c; if(l.Course_Type_Applied__c!=null) c.Course_Type_Applied__c = l.Course_Type_Applied__c; if(l.Master_Record_Type__c!=null) c.Master_Record_Type__c = l.Master_Record_Type__c; if(l.Source__c!=null) c.Source__c = l.Source__c; if(l.Asked_something_through_Olark__c!=null) c.Asked_something_through_Olark__c = l.Asked_something_through_Olark__c; if(l.Registered_at_PS_Slack_Group__c!=null) c.Registered_at_PS_Slack_Group__c= l.Registered_at_PS_Slack_Group__c; if(l.Registered_at_Sumome__c!=null) c.Registered_at_Sumome__c = l.Registered_at_Sumome__c; if(l.Registered_at_Blog_Newsletter__c!=null) c.Registered_at_Blog_Newsletter__c = l.Registered_at_Blog_Newsletter__c; if(l.Requested_Invitation_to_Slack_PS_Group__c!=null) c.Requested_Invitation_to_Slack_PS_Group__c = l.Requested_Invitation_to_Slack_PS_Group__c; if(l.Last_Event_Attended__c!=null) c.Last_Event_Attended__c = l.Last_Event_Attended__c; if(l.FirstName!=null && !(l.FirstName.toLowerCase().Contains('not') && l.FirstName.toLowerCase().Contains('provided'))) c.FirstName = l.FirstName; if(l.LastName!=null && !(l.LastName.toLowerCase().Contains('not') && l.LastName.toLowerCase().Contains('provided')) ) c.LastName = l.LastName; if(l.LinkedIn__c!=null) c.LinkedIn__c = l.LinkedIn__c; if(l.Last_Event_Attendee_Status__c!=null) c.Last_Event_Attendee_Status__c = l.Last_Event_Attendee_Status__c; if(l.When_are_you_looking_to_take_the_course__c!=null) c.When_are_you_looking_at_taking_the_cours__c = l.When_are_you_looking_to_take_the_course__c; if(l.Book_requested__c!=null) c.Book_requested__c = l.Book_requested__c; if(l.TESTFIELD2__c!=null) c.TESTFIELD2__c = l.TESTFIELD2__c; Contactlist.add(c); } } if(Contactlist != null && Contactlist.size() > 0){ Update Contactlist; } if(Accountlist != null && Accountlist.size() > 0){ Update Accountlist; } } } }
The error doesn´t happen always, but when someone clicks to fast in a submit button or something similar... Also, let you know that we have 40 Processes inside Process Builder...
Any recommendation, solution, question, answer is deeply appreciated..
Thanks!!
- Stany Yeh
- March 08, 2017
- Like
- 0
Process Builder not working properly with Data Import Wizard
Hi all,
We have implemented a script in our website that allows us to pass Utm_source, medium and campaign info of all our leads to Salesforce through custom fields. What I wanted to accomplish is to have another field that via Process Builder gathers the information of the first source that the lead has and is mantained invariable. In order to achieve that, this is done in the Contact Object only when the Contact is created not updated (we have Lead Auto Convert trigger) and it is working good for the leads that enter through the Web-2-Lead forms but...
We also do Imports through Import Wizard every week, these leads are generated through attendee lists of our weekly events... This list is a csv which also contains source data (given by Eventbrite). Unfortunately, all my Process Builder is not working for this imported leads...
Is there some kind of conflict between Process Builder and Data Import Wizard? How can avoid this?
Thanks a lot,
Dani
We have implemented a script in our website that allows us to pass Utm_source, medium and campaign info of all our leads to Salesforce through custom fields. What I wanted to accomplish is to have another field that via Process Builder gathers the information of the first source that the lead has and is mantained invariable. In order to achieve that, this is done in the Contact Object only when the Contact is created not updated (we have Lead Auto Convert trigger) and it is working good for the leads that enter through the Web-2-Lead forms but...
We also do Imports through Import Wizard every week, these leads are generated through attendee lists of our weekly events... This list is a csv which also contains source data (given by Eventbrite). Unfortunately, all my Process Builder is not working for this imported leads...
Is there some kind of conflict between Process Builder and Data Import Wizard? How can avoid this?
Thanks a lot,
Dani
- Stany Yeh
- July 18, 2016
- Like
- 0
Change Record Type depending on the Web-2-Lead form used
Hi!
I am trying to solve this problem a couple of days without getting anything so I have to ask here..
The situation is that we have just started a new business line (Corporate Training) that we want to differentiate from our main goal, that is a B2C process. For that I want to use two different record types. I have proceeded with the steps and I have managed to create two different record types and assign them ok and now if we want to create a contact in a manual way, we can choose the Record Type and everything goes ok..
However... what I need to accomplish is to assign this Corporate record type to a Lead/Contact (we automatically convert every Lead into a Contact) that has entered from a specific Web-to-Lead form in our website. Just in that form, the other ones will remain the same. I have introduced a hidden field in the form (we use Wordpress) with the Lead Record Type id but even though every other field is being recorded ok, the Record Type still stays as the B2C one... I have already changed the settings to the Keep bla bla thing... And nothing...
Anyone can provide any idea?
Thanks a lot!!
Dani
I am trying to solve this problem a couple of days without getting anything so I have to ask here..
The situation is that we have just started a new business line (Corporate Training) that we want to differentiate from our main goal, that is a B2C process. For that I want to use two different record types. I have proceeded with the steps and I have managed to create two different record types and assign them ok and now if we want to create a contact in a manual way, we can choose the Record Type and everything goes ok..
However... what I need to accomplish is to assign this Corporate record type to a Lead/Contact (we automatically convert every Lead into a Contact) that has entered from a specific Web-to-Lead form in our website. Just in that form, the other ones will remain the same. I have introduced a hidden field in the form (we use Wordpress) with the Lead Record Type id but even though every other field is being recorded ok, the Record Type still stays as the B2C one... I have already changed the settings to the Keep bla bla thing... And nothing...
Anyone can provide any idea?
Thanks a lot!!
Dani
- Stany Yeh
- April 19, 2016
- Like
- 0
UNABLE_TO_LOCK_ROW problem
Hello everyone!
At the beginning we were receiving this error randomly a couple of times a week, but with the fast growth of our database I am finding these errors more often and I would like to know if is there a possible solution for it or we should "live" with it...
The error is the following:
Salesforce could not create this lead because of the reason listed below. For more information about this error or help with Web-to-Case-Lead, please contact Customer Support.
Reason: Apex trigger leadAutoConvert caused an unexpected exception, contact your administrator: leadAutoConvert: execution of AfterInsert
caused by: System.DmlException: ConvertLead failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, System.DmlException: Update failed. First exception on row 0 with id 0031a00000bVrTtAAK; first error: UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record or 1 records: 0031a00000bVrTtAAK: []
Class.leadconvert.BulkLeadConvert.handleRegularContactUpdates: line 350, column 1
Class.leadconvert.BulkLeadConvert.convertLead: line 98, column 1: []: Trigger.leadAutoConvert: line 71, column 1
Lead Capture Page: https://webto.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8
What we have is a process that automatically converts Leads into Contacts and if the lead (having the email as the key field) was already converted then it updates the Contact for some fields stated in the Class, thus preventing duplicates (although I can see some from time to time)
This is the code of the leadAutoConvert trigger that is the one that I think is failing...
We also have this other Trigger, that I do not know if it can affect something...
Finally we have this class, that is the one that I touch whenever I want to add a hierarchy status or a field to override..
The error doesn´t happen always, but when someone clicks to fast in a submit button or something similar... Also, let you know that we have 40 Processes inside Process Builder...
Any recommendation, solution, question, answer is deeply appreciated..
Thanks!!
At the beginning we were receiving this error randomly a couple of times a week, but with the fast growth of our database I am finding these errors more often and I would like to know if is there a possible solution for it or we should "live" with it...
The error is the following:
Salesforce could not create this lead because of the reason listed below. For more information about this error or help with Web-to-Case-Lead, please contact Customer Support.
Reason: Apex trigger leadAutoConvert caused an unexpected exception, contact your administrator: leadAutoConvert: execution of AfterInsert
caused by: System.DmlException: ConvertLead failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, System.DmlException: Update failed. First exception on row 0 with id 0031a00000bVrTtAAK; first error: UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record or 1 records: 0031a00000bVrTtAAK: []
Class.leadconvert.BulkLeadConvert.handleRegularContactUpdates: line 350, column 1
Class.leadconvert.BulkLeadConvert.convertLead: line 98, column 1: []: Trigger.leadAutoConvert: line 71, column 1
Lead Capture Page: https://webto.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8
What we have is a process that automatically converts Leads into Contacts and if the lead (having the email as the key field) was already converted then it updates the Contact for some fields stated in the Class, thus preventing duplicates (although I can see some from time to time)
This is the code of the leadAutoConvert trigger that is the one that I think is failing...
Trigger leadAutoConvert on Lead (after insert) { Set<String> leademails = new Set<String>(); List<Lead> newLeads = new List<Lead>(); // Get all the new leads for(Lead l : system.trigger.new){ newLeads.add(l); if(l.email!=null) leademails.add(l.email); } /* Make some maps of Contact and email addresses */ List<Contact> ContactList = [select Id, Email, OwnerId,accountid from Contact where Email IN: leademails]; Map<ID, String> Contacts = new Map<ID, String>(); Map<ID, String> accountsids = new Map<ID, String>(); Map<String,ID> contactOwnerMap = new Map<String,ID>(); // Generic map for preventing loss of ids for(Contact c : ContactList){ Contacts.put(c.id, c.Email); accountsids.put(c.accountid, c.Email); contactOwnerMap.put(c.Email,c.OwnerId); } // We will need this to get the id from the email address Map<String, ID> accountFlipped = new Map<String, ID>(); for(ID i : accountsids.keyset()){ accountFlipped.put(accountsids.get(i), i); } Map<String, ID> ContactFlipped = new Map<String, ID>(); for(ID i : Contacts.keyset()){ ContactFlipped.put(Contacts.get(i), i); } Map<String,String> LeadWithLeadRegionMap = new Map<String,String>(); Map<String,String> AccountWithLeadRegionMap = new Map<String,String>(); Map<String,String> ContactWithLeadRegionMap = new Map<String,String>(); /* System Conversion Requirements */ leadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1]; List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>(); Database.LeadConvert lc = new Database.LeadConvert(); /* Configuring Payload */ for (Lead nl : newleads) { lc = new Database.LeadConvert(); lc.setLeadId(nl.id); lc.setOverwriteLeadSource(false); lc.setConvertedStatus(convertStatus.MasterLabel); system.debug(Contacts.size()); LeadWithLeadRegionMap.put(nl.id,nl.Region__c); // Check to see if Contact already exists if(!Contacts.isEmpty()){ if(nl.email != null && ContactFlipped.get(nl.email)!=null && accountFlipped.get(nl.email) != null){ lc.setAccountId(accountFlipped.get(nl.email)); lc.setContactId(ContactFlipped.get(nl.email)); lc.setDoNotCreateOpportunity(true); } } else { // In the event an Contact doesn't exist lc.setOwnerId(nl.OwnerId); lc.setDoNotCreateOpportunity(true); } leadConverts.add(lc); } // Fire Payload Database.LeadConvertResult[] lcr = Database.convertLead(leadConverts); System.debug(LoggingLevel.INFO, lcr); Set<ID> AccountIDSet = new Set<ID>(); Set<ID> ContactIDSet = new Set<ID>(); for(Database.LeadConvertResult dbresult : lcr ){ system.debug('====lead id==='+dbresult.getLeadId()); system.debug('====Account id==='+dbresult.getAccountId()); system.debug('====Contact id==='+dbresult.getContactId()); system.debug('====LeadWithLeadRegionMap.get(dbresult.getLeadId())==='+LeadWithLeadRegionMap.get(dbresult.getLeadId())); AccountIDSet.add(dbresult.getAccountId()); ContactIDSet.add(dbresult.getContactId()); if(LeadWithLeadRegionMap.get(dbresult.getLeadId()) != null){ AccountWithLeadRegionMap.put(dbresult.getAccountId(),LeadWithLeadRegionMap.get(dbresult.getLeadId())); ContactWithLeadRegionMap.put(dbresult.getContactId(),LeadWithLeadRegionMap.get(dbresult.getLeadId())); }else{ AccountWithLeadRegionMap.put(dbresult.getAccountId(),''); ContactWithLeadRegionMap.put(dbresult.getContactId(),''); } } List<User> BreadUser = [ Select id,Name from User where name = 'Brad Heringer']; List<User> JakeUser = [ Select id,Name from User where name = 'Jake Strich']; if(AccountIDSet != null && AccountIDSet.size() > 0){ List<Account> Accountlist = [Select id,Name,OwnerID from Account where id in: AccountIDSet]; if(Accountlist != null && Accountlist.size() > 0){ for(Account ac : Accountlist){ if(AccountWithLeadRegionMap.get(ac.id) != null) { String Regionname = AccountWithLeadRegionMap.get(ac.id); if(Regionname == 'San Francisco' || Regionname == 'Silicon Valley' || Regionname == 'Los Angeles' || Regionname == 'Seattle' || Regionname == 'Online' ) { if(JakeUser != null && JakeUser.size() > 0) ac.OwnerID = JakeUser.get(0).id; } if(Regionname == '' || Regionname == 'New York' || Regionname == 'Boston' || Regionname == 'Austin' || Regionname == 'Chicago' ) { if(BreadUser != null && BreadUser.size() > 0) ac.OwnerID = BreadUser.get(0).id; } }else{ if(BreadUser != null && BreadUser.size() > 0) ac.OwnerID = BreadUser.get(0).id; } } Update Accountlist; } } if(ContactIDSet != null && ContactIDSet.size() > 0){ List<Contact> ContactlisttoUpdate = [Select id,Name,Email,OwnerID from Contact where id in: ContactIDSet]; if(ContactlisttoUpdate != null && ContactlisttoUpdate.size() > 0){ for(Contact con : ContactlisttoUpdate){ if(ContactWithLeadRegionMap.get(con.id) != null) { String Regionname = ContactWithLeadRegionMap.get(con.id); if((Regionname == null || Regionname == '') && contactOwnerMap.containsKey(con.Email)){ con.OwnerID = contactOwnerMap.get(con.Email); }else if(Regionname == 'San Francisco' || Regionname == 'Silicon Valley' || Regionname == 'Los Angeles' || Regionname == 'Seattle' || Regionname == 'Online' ) { if(JakeUser != null && JakeUser.size() > 0) con.OwnerID = JakeUser.get(0).id; }else if(Regionname == '' || Regionname == 'New York' || Regionname == 'Boston' || Regionname == 'Austin' || Regionname == 'Chicago' ) { if(BreadUser != null && BreadUser.size() > 0) con.OwnerID = BreadUser.get(0).id; } }else{ if(BreadUser != null && BreadUser.size() > 0) con.OwnerID = BreadUser.get(0).id; } } Update ContactlisttoUpdate; } } }
We also have this other Trigger, that I do not know if it can affect something...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 trigger customizationOnLeadConvert on Lead (after update) { Set<ID> leadIDSet = new Set<ID>(); for (Lead l : trigger.new) { if (l.ConvertedContactId != null) { leadIDSet.add(l.id); } if (l.convertedAccountId != null) { leadIDSet.add(l.id); } } LeadFutureClass.CustomizationOnLeadConvertMethod(leadIDSet); }
Finally we have this class, that is the one that I touch whenever I want to add a hierarchy status or a field to override..
Global class LeadFutureClass { @future public static void CustomizationOnLeadConvertMethod(set<ID> LeadIDSet) { Set<String> ContactIDSet = new Set<String>(); Set<String> AccountIDSet = new Set<String>(); List<Lead> Leadlist = [Select id,Source__c,name,ConvertedContactId,Master_Record_Type__c,Course_Type_Applied__c,Coding_Course_Status__c,Speakers_Status__c,Instructors_Status__c,Asked_something_through_Olark__c,Registered_at_Blog_Newsletter__c,Registered_at_PS_Slack_Group__c,Registered_at_Sumome__c,Requested_Invitation_to_Slack_PS_Group__c,convertedAccountId,FirstName,LastName,Email,Phone,Status__c,Region__c,LinkedIn__c,Last_Event_Attended__c,Last_Event_Attendee_Status__c,When_are_you_looking_to_take_the_course__c,Book_requested__c,TESTFIELD2__c from Lead where id in: LeadIDSet]; for (Lead l : Leadlist) { if (l.ConvertedContactId != null) { ContactIDSet.add(l.ConvertedContactId); } if (l.convertedAccountId != null) { AccountIDSet.add(l.convertedAccountId); } } List<Account> Accountlist = new List<Account>(); List<Contact> Contactlist = new List<Contact>(); if(AccountIDSet != null && AccountIDSet.size() > 0 && ContactIDSet != null && ContactIDSet.size() > 0 ) { Map<Id,Account> AccountMap = new Map<Id,Account>(); for(Account ac : [SELECT Id FROM Account WHERE Id in: AccountIDSet]){ AccountMap.put(ac.id,ac); } Map<Id,Contact> ContactMap = new Map<Id,Contact>(); for(Contact con : [Select Id,Status__c,Coding_Course_Status__c,Speakers_Status__c,Instructors_Status__c,Name From Contact Where Id in: ContactIDSet ]){ ContactMap.put(con.id,con); } for (Lead l : Leadlist) { Map<String,Integer > statusvalues = new Map<String,Integer >(); statusvalues.put('Graduate',1); statusvalues.put('Student',2); statusvalues.put('Cohort Completed',3); statusvalues.put('Past Speaker',4); statusvalues.put('Currently Teaching',5); statusvalues.put('Onboarding Step 2',6); statusvalues.put('Onboarding Step 1',7); statusvalues.put('Confirmed Instructor',8); statusvalues.put('Confirmed Speaker',9); statusvalues.put('Payment Received',10); statusvalues.put('Approved Not Confirmed',11); statusvalues.put('Contract Sent',12); statusvalues.put('Event Details Sent',13); statusvalues.put('Payment Link Sent',14); statusvalues.put('Onsite Visit Scheduled / Videocall',15); statusvalues.put('Invited to Event',16); statusvalues.put('Presentation Scheduled',17); statusvalues.put('Casual Meeting Scheduled',18); statusvalues.put('Phone Call Scheduled',19); statusvalues.put('Met at Event / Personal Interaction',20); statusvalues.put('Replied',21); statusvalues.put('Follow Up Required',22); statusvalues.put('Application Submitted - Emailed No Response',23); statusvalues.put('Application Submitted',24); statusvalues.put('New Leads - Emailed No Response',25); statusvalues.put('New Leads',26); statusvalues.put('Future Instructor',27); statusvalues.put('Future Speaker',28); statusvalues.put('Future Course',29); statusvalues.put('Promotional Workshop - Emailed No Response',30); statusvalues.put('Promotional Workshop',31); statusvalues.put('Regular Workshop - Emailed No Response',32); statusvalues.put('Regular Workshop',33); statusvalues.put('RSVP',34); statusvalues.put('Potential Instructor - Emailed No Response',35); statusvalues.put('Potential Instructor',36); statusvalues.put('Closed - Lost',37); statusvalues.put('Potential Speaker - Emailed No Response',38); statusvalues.put('Potential Speaker',39); statusvalues.put('Never Replied',40); statusvalues.put('Early Stage - Emailed',41); statusvalues.put('Early Stage',42); if (l.convertedAccountId != null) { Account acc = new Account(); acc = AccountMap.get(l.convertedAccountId); if(l.FirstName!=null) acc.Name = l.FirstName+' '+l.LastName; else acc.Name = l.LastName; Accountlist.add(acc); } if(l.ConvertedContactId != null) { Contact c = new Contact(); c = ContactMap.get(l.ConvertedContactId); if(l.Phone!=null) c.Phone = l.Phone; if(l.Email!=null) c.Email = l.Email; if(l.Status__c!=null ) { if(c.Status__c == null) c.Status__c= l.Status__c; else if(statusvalues.get(c.Status__c) > statusvalues.get(l.Status__c)) c.Status__c= l.Status__c; } if(l.Coding_Course_Status__c!=null ) { if(c.Coding_Course_Status__c == null) c.Coding_Course_Status__c= l.Coding_Course_Status__c; else if(statusvalues.get(c.Coding_Course_Status__c) > statusvalues.get(l.Coding_Course_Status__c)) c.Coding_Course_Status__c= l.Coding_Course_Status__c; } if(l.Speakers_Status__c!=null ) { if(c.Speakers_Status__c == null) c.Speakers_Status__c= l.Speakers_Status__c; else if(statusvalues.get(c.Speakers_Status__c) > statusvalues.get(l.Speakers_Status__c)) c.Speakers_Status__c= l.Speakers_Status__c; } if(l.Instructors_Status__c!=null ) { if(c.Instructors_Status__c == null) c.Instructors_Status__c= l.Instructors_Status__c; else if(statusvalues.get(c.Instructors_Status__c) > statusvalues.get(l.Instructors_Status__c)) c.Instructors_Status__c= l.Instructors_Status__c; } if(l.Region__c!=null) c.Region__c = l.Region__c; if(l.Course_Type_Applied__c!=null) c.Course_Type_Applied__c = l.Course_Type_Applied__c; if(l.Master_Record_Type__c!=null) c.Master_Record_Type__c = l.Master_Record_Type__c; if(l.Source__c!=null) c.Source__c = l.Source__c; if(l.Asked_something_through_Olark__c!=null) c.Asked_something_through_Olark__c = l.Asked_something_through_Olark__c; if(l.Registered_at_PS_Slack_Group__c!=null) c.Registered_at_PS_Slack_Group__c= l.Registered_at_PS_Slack_Group__c; if(l.Registered_at_Sumome__c!=null) c.Registered_at_Sumome__c = l.Registered_at_Sumome__c; if(l.Registered_at_Blog_Newsletter__c!=null) c.Registered_at_Blog_Newsletter__c = l.Registered_at_Blog_Newsletter__c; if(l.Requested_Invitation_to_Slack_PS_Group__c!=null) c.Requested_Invitation_to_Slack_PS_Group__c = l.Requested_Invitation_to_Slack_PS_Group__c; if(l.Last_Event_Attended__c!=null) c.Last_Event_Attended__c = l.Last_Event_Attended__c; if(l.FirstName!=null && !(l.FirstName.toLowerCase().Contains('not') && l.FirstName.toLowerCase().Contains('provided'))) c.FirstName = l.FirstName; if(l.LastName!=null && !(l.LastName.toLowerCase().Contains('not') && l.LastName.toLowerCase().Contains('provided')) ) c.LastName = l.LastName; if(l.LinkedIn__c!=null) c.LinkedIn__c = l.LinkedIn__c; if(l.Last_Event_Attendee_Status__c!=null) c.Last_Event_Attendee_Status__c = l.Last_Event_Attendee_Status__c; if(l.When_are_you_looking_to_take_the_course__c!=null) c.When_are_you_looking_at_taking_the_cours__c = l.When_are_you_looking_to_take_the_course__c; if(l.Book_requested__c!=null) c.Book_requested__c = l.Book_requested__c; if(l.TESTFIELD2__c!=null) c.TESTFIELD2__c = l.TESTFIELD2__c; Contactlist.add(c); } } if(Contactlist != null && Contactlist.size() > 0){ Update Contactlist; } if(Accountlist != null && Accountlist.size() > 0){ Update Accountlist; } } } }
The error doesn´t happen always, but when someone clicks to fast in a submit button or something similar... Also, let you know that we have 40 Processes inside Process Builder...
Any recommendation, solution, question, answer is deeply appreciated..
Thanks!!
- Stany Yeh
- March 08, 2017
- Like
- 0