3

I have a process builder in my org which checks if (fieldA = 'XYZ') and then invokes a AutoLeadConversion class (code below).

Public class AutoConvertLeads
{
    @InvocableMethod
    public static void LeadAssign(List<Id> LeadIds)
    {
        LeadStatus CLeadStatus= [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true Limit 1];
        List<Database.LeadConvert> MassLeadconvert = new List<Database.LeadConvert>();
        for(id currentlead: LeadIds){

                Database.LeadConvert Leadconvert = new Database.LeadConvert();
                Leadconvert.setLeadId(currentlead);                
                Leadconvert.setConvertedStatus(CLeadStatus.MasterLabel);
                MassLeadconvert.add(Leadconvert);
        }

        if (!MassLeadconvert.isEmpty()) {
            List<Database.LeadConvertResult> lcr = Database.convertLead(MassLeadconvert);
        }
    }
}

But if i make fieldA = XYZ and that Lead owner is a queue, then it will give error that converted lead can be owned by user only and not queue.

So i set the Account default owner as a User, if owner is a queue while converting.(Modified class)

    Public class AutoConvertLeads
{
    @InvocableMethod
    public static void LeadAssign(List<Id> LeadIds)
    {
        User u = [SELECT Id FROM User WHERE email = 'sonali.hingorani@spjain.org' Limit 1];
        LeadStatus CLeadStatus= [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true Limit 1];
        List<Database.LeadConvert> MassLeadconvert = new List<Database.LeadConvert>();
        for(id currentlead: LeadIds){               
                String LeadOwnerId = [SELECT Ownerid FROM Lead WHERE id=:currentlead Limit 1].Ownerid;
                Database.LeadConvert Leadconvert = new Database.LeadConvert();
                Leadconvert.setLeadId(currentlead);                
                Leadconvert.setConvertedStatus(CLeadStatus.MasterLabel);
                if(LeadOwnerId.startsWith('00G')){
                   Leadconvert.setOwnerId(u.Id);           
                }              
                MassLeadconvert.add(Leadconvert);
        }

        if (!MassLeadconvert.isEmpty()) {
            List<Database.LeadConvertResult> lcr = Database.convertLead(MassLeadconvert);
        }
    }
}

But i am using a query inside a for loop which is a bad practice...So anybody have any idea how to achieve this without query inside loop.

4

Just query the leads and place them in a map:

Public class AutoConvertLeads
{
    @InvocableMethod
    public static void LeadAssign(List<Id> LeadIds)
    {
        User u = [SELECT Id FROM User WHERE email = 'sonali.hingorani@spjain.org' Limit 1];
        LeadStatus CLeadStatus= [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true Limit 1];
        Map<Id, Lead> leads = new Map<Id, Lead>([SELECT OwnerId FROM Lead WHERE Id = :leadIds]);
        List<Database.LeadConvert> MassLeadconvert = new List<Database.LeadConvert>();
        for(id currentlead: LeadIds){
            Database.LeadConvert Leadconvert = new Database.LeadConvert();
            Leadconvert.setLeadId(currentlead);                
            Leadconvert.setConvertedStatus(CLeadStatus.MasterLabel);
            // Check lead owner here
            if(leads.get(currentLead).OwnerId.getSObjectType() != User.SObjectType) {
                Leadconvert.setOwnerId(u.Id);
            }
            MassLeadconvert.add(Leadconvert);
        }
        List<Database.LeadConvertResult> lcr = Database.convertLead(MassLeadconvert);
    }
}
  • That will do.....Thanks a ton! But i dont get why did you remove if (!MassLeadconvert.isEmpty()) { } – Dnyaneshwar Jan 17 '17 at 6:28
  • This list could still be empty...correct me if i am wrong :) – Dnyaneshwar Jan 17 '17 at 6:29
  • 1
    @Dnyaneshwar It is not an error to call Database.convertLead on an empty list. There is no need to check for an empty list. – sfdcfox Jan 17 '17 at 6:37

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.