• Meer Zaman
  • NEWBIE
  • 30 Points
  • Member since 2015
  • Meer Zaman
  • DhoopSoft Ltd


  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 8
    Replies
I need to have a select Query in my Apex method that has this line - 

AND Pending_Agreement_DateStamp__c <= LAST_N_DAYS:pendingDays

The pendingDays in an Integer value that is passed to this method
I tried using Sting.valueOf or Integer.valueOf but they dont seem to work. Please let me know what I should use there?
  • April 10, 2019
  • Like
  • 0
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... 


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!! 

 
Hi All,
I have one field startdate__c which is of type date field when iam using this field in the soql query and displaying that field it is giving the format like : 2016-05-03 00:00:00 .How can i remove 00:00:00 and display only date ? Can someone advise .

Thanks,
Kiran
I recently upgraded to Windows 8.1, and am now having issues with the Force.com IDE. When I select Force.com->Add/remove metadata components, I'm not able to see any custom field child component in the Objects-Custom component. Does anyone have any ideas on what is causing the issue?
User-added image

Hi,

 

Is there any way that i can search for any text in a class/page while working in Developer Console. I have a long class with thousand lines of code and need to find some specific text in that class. Browsers 'Ctr + F' does not works. It only shows me the search results from the few lines of code that is visible on the page.

 

Does DC provides any search text box which i am missing which i can use to search the text from my code?

  • September 14, 2012
  • Like
  • 1

Hello,

 

I'm trialling SaleForce for our company (specifically synching data via the API)... and in doing so am changing schema quite a bit.

 

At the moment, I'm having to manually log in and regenerate schema each time (and then have an automated process to convert this into C#)

 

I would like to automate the refresh of schema.... however, I cannot see how to authenicate programmatically to the page https://eu1.salesforce.com/soap/wsdl.jsp?type=*

 

I'm probably missing something obvious - can someone help?

 

Regards,

Mark.

 

Has anyone successfully used addError on a field dynamically on an SObject?

 

Something like the below in concept (note the below doesn't work because get(FieldName) returns the value in that field, not the field itself...

 

Account a = [Select fields FROM Account WHERE stuff];

SObject obj = (SObject)a;

obj.get('FieldName__c').addError('Something or other');

 

Thank you,

Caleb