• Trung Hieu Tran 5
  • NEWBIE
  • 0 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
Hi, I am new to salesforce code.
I am building an Auto Convert Lead process when LeadSource = 'Web' and Rating = 'Hot'.
I have written the Apex Code (I will bulikfy it when it works and I fully understand the process) and a Test Class which runs successfully.
I have a Process Bulder that kicks it off. (passes through LeadIds = Lead.Id)
User-added imageApex Code:
Public class AutoConvertLeads
{
    @InvocableMethod
    public static void LeadAssign(List<Id> LeadIds)
    {
        system.debug('1');
        Database.LeadConvert leadConvert = new Database.LeadConvert();
        system.debug('2');
        leadConvert.setLeadId(LeadIds[0]);

        system.debug('3');
        LeadStatus LeadStat= [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];

        system.debug('4');
        leadConvert.setConvertedStatus(LeadStat.MasterLabel);
        //Leadconvert.setDoNotCreateOpportunity(TRUE); //Remove this line if you want to   create an opportunity from Lead Conversion 

        system.debug('5');
        Database.LeadConvertResult lcr = Database.ConvertLead(leadConvert);
        system.debug('6');
        System.assert(lcr.isSuccess());
        system.debug('7');
    }
}

Test Class:
@isTest
private class AutoConvertLeadsTest {

    @isTest static void testLeadConversion() {
        Lead Ld = new Lead();
        Ld.FirstName = 'TestFirstName';
        Ld.LastName = 'TestLastName';
        Ld.Company = 'TestCompany';
        Ld.LeadSource = 'Web';
        Ld.Rating = 'Warm';
        Ld.Status = 'Open - Not Contacted';
        Ld.Email = 'test@testdwr.com.zz';
        system.debug('a');
        insert Ld;
        system.debug('b');
        Ld.Rating = 'Hot';
        update Ld;
        system.debug('c');

    }
}

When I edit the LeadSource = 'Web' and Rating = 'Hot' on the UI it kicks off the Process and then Apex code.
I get the following error:
Review the errors on this page.
We can't save this record because the “Auto Convert Lead” process failed. Give your Salesforce admin these details. An Apex error occurred: System.DmlException: ConvertLead failed. First exception on row 0; first error: DUPLICATES_DETECTED, Use one of these records?: [] Error ID: 839700365-14885 (-1370628323)


I am mystified what the source of the error is.
Hoping you can help me.