0

I have created a PB in sandbox where I am converting my leads into Contacts on the basis of custom checkbox field: Created in Admin Panel. So, my lead will only convert if this checkbox is true. I am calling an Invocable Apex class in the PB and all the conversion logic is written in this class.

I have tested everything and the functionality is working just fine in Sandbox as the PB fires as per the condition defined. However, when I deployed the same PB in Production, it fails to fire.I have also manually recreated the PB all over again but it still isn't firing. I have also tried to execute the PB with execute only when specified changes are made to the record both checked and unchecked.

Am I missing a trick or two??

Below is the screen shot for my PB:

enter image description here

enter image description here

enter image description here 2:

Below is the code for my Apex Class:

public class AutoConvertLeads {

    @InvocableMethod //Used to invoke this Class in Process Builder
    public static void assignLeads(List<Id> leadIds){
        Map<string, Id> accountMap = new map<string,id>();

        for(Account acct : [SELECT Id, Name, Company_Name_Custom__c from Account LIMIT 10000]) {
            accountMap.put(acct.Name,acct.Id);
        }

        LeadStatus convertedLeadStatus= [SELECT Id, MasterLabel 
                                 FROM LeadStatus 
                                 WHERE IsConverted = true 
                                 LIMIT 1];

        List<Database.LeadConvert> massLeadConvertList = new List<Database.LeadConvert>();

        for(Lead currentlead: [SELECT Id, Company_Name_Custom__c, created_in_admin_panel__c 
                               FROM Lead 
                               WHERE created_in_admin_panel__c = true
                               AND Id IN :leadIds]) {
            if(!accountMap.isEmpty() && accountMap.ContainsKey(currentlead.Company_Name_Custom__c)) {
                Database.LeadConvert convertLead = new Database.LeadConvert();
                convertLead.setLeadId(currentlead.Id);                
                convertLead.setConvertedStatus(convertedLeadStatus.MasterLabel);
                convertLead.setDoNotCreateOpportunity(TRUE);
                convertLead.setAccountId(accountMap.get(currentlead.Company_Name_Custom__c));
                massLeadConvertList.add(convertLead);
            } /*else {
                system.debug('@@@@InsideELSE');
                Database.LeadConvert convertLead = new Database.LeadConvert();
                convertLead.setLeadId(currentlead.Id);                
                convertLead.setConvertedStatus(convertedLeadStatus.MasterLabel);
                convertLead.setDoNotCreateOpportunity(TRUE);
                massLeadConvertList.add(convertLead);
            }  */    
        }
        if (! massLeadConvertList.isEmpty()) {
            Database.convertLead(massLeadConvertList);
        }
    }
}

and here is the Code for my Test Class:

@isTest (SeeAllData = false)
public class AutoConvertLeadsTest {

    private static testMethod void assignLeadsTest() {
        List<id> leadIds =new List<id>();
        List<Account> acctstoInsert = new List<Account>();
        List<Lead> leadstoInsert = new List<Lead>();

        for(integer h = 0; h < 4; h++){
            Account acctObj = new Account (Name = 'Test Company'+h);
            acctstoInsert.add(acctObj);
        }
        Database.insert(acctstoInsert);

        //inserting expected data like HubSpot
        for(integer i = 0; i < 2; i++){
            Lead leadObj = new Lead (LastName = 'Test Lead'+i, Company='Test Company'+i, Company_Name_Custom__c ='Test Company'+i,
                                     created_in_admin_panel__c = true);
            leadstoInsert.add(leadObj);
        }

        //inserted misc lead data
        for(integer j = 0; j < 2; j++){
            Lead leadObj = new Lead (LastName = 'Mock Lead'+j, Company='Mock Test Company'+j);
            leadstoInsert.add(leadObj);
        }
        Database.insert(leadstoInsert);

        for(Lead lead : leadstoInsert) 
        {
            leadIds.add(lead.Id);
        }

        Test.startTest();
        AutoConvertLeads.assignLeads(leadIds);
        Test.stopTest();

        Account acct = [SELECT Id, Name, (SELECT Id, LastName FROM Contacts)
                        FROM Account
                        WHERE Name ='Test Company1'
                        LIMIT 1]; 
        system.assertEquals(1, acct.Contacts.size());
    }
}

Look forward for your quick and valuable responses.

Thanks!

  • Have you checked the debug logs in production? Is it getting into apex? – Ranga Jul 13 '18 at 9:19
  • Yes it does and which is why I am wondering why it isn't working. There is no exception/error in the debug logs as well. Should I share the debug log with you if that helps you?? – tanzysfdc Jul 13 '18 at 9:41
  • That might help. Can you see how far its getting? Can you see in debug logs that your SOQL queries are getting executed and brining in data? – Ranga Jul 13 '18 at 9:44
  • break the problem down -- 1) use exec anonymous and verify the invocable apex does lead conversion ; 2) - does this happen for all profiles incl sysads? 3) put a try catch around database.leadconvert and see what, if any errors are caught – cropredy Jul 13 '18 at 16:15
  • I was able to identify the problem. In my code, I have used a LIMIT of 10000 in the SOQL query to fetch the Accounts but I had around 30k Accounts so the system was not to retrieve the accounts/company on the Leads. Then I changed my query and put the LIMIT to 35k but still my code doesn't work. I can confirm that the PB is working. Can someone among you help me to handle around 40K records. Do I need to use bulk class for the requirement. – tanzysfdc Jul 16 '18 at 6:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.