function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jeff Bryant 16Jeff Bryant 16 

How to write a Test Class for @InvocableMethod

I'm having an issue with "Method does not exist or incorrect signature" on this test class...



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);
        }
    }
}





@isTest
private class AutoConvertLeadsTest {
    @isTest
    public static void testLeadInsert ()
    {
        List<Id> lListIds = new List<Id>();
        List<Lead> lList = new List<Lead>();
        
        Lead objLead = new Lead (LastName = 'Test', Email = 'test@gmail.com', mobilephone = '+1234567890', Company = 'Test company');
        insert objLead; 
        
        lList = [SELECT Id FROM Lead];
        
        for(Lead l : lList){
            lListIds.add(l.Id);
        }
        
        insert lList;
        
        Test.startTest();
        AutoConvertLeadsTest.LeadAssign(lListIds);
        Test.stopTest();
    }
}
 
Anthony McDougaldAnthony McDougald
Hello Jeff,
Hope that your day is off to an amazing start. Your issue stems from a class not being instantiated. Please try the below test class and report back when you get the chance. Hope this helps and may God bless you abundantly.
@isTest
private class AutoConvertLeadsTest {
    @isTest
    public static void testLeadInsert ()
    {
        AutoConvertLeads convert = new AutoConvertLeads();
        List<Id> lListIds = new List<Id>();
        List<Lead> lList = new List<Lead>();
        
        Lead objLead = new Lead (LastName = 'Test', Email = 'test@gmail.com', mobilephone = '+1234567890', Company = 'Test company');
        insert objLead; 
        
        lList = [SELECT Id FROM Lead];
        
        for(Lead l : lList){
            lListIds.add(l.Id);
        }
        
        insert lList;
        
        Test.startTest();
        convert.LeadAssign(lListIds);
        Test.stopTest();
    }
}


Best Regards,
Anthony McDougald
Jeff Bryant 16Jeff Bryant 16
Hi Anthony, thanks so much for the kind response! What would happen when I receive the "InvocableMethod methods must be declared as static" in the class itself?
Anthony McDougaldAnthony McDougald
Hello Jeff,
Hope that your day is off to an amazing start. Please try the below class and report back if you have any issues. Hope this helps and may God bless you abundantly.
public class AutoConvertLeads
{
    @InvocableMethod(label='Convert Leads')
    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);
        }
    }
}


Best Regards,
Anthony McDougald