You need to sign in to do that
Don't have an account?
Wendy Tanner 11
Test Class for Lead Convert. Unable to push Apex Class to production
Hello! I have an apex class that was working great in sandbox.
Using it in a process builder. Any one doing this, can you provide guidance?
Public class AutoConvertLeads
{
@InvocableMethod
public static void LeadAssign(List<Id> LeadIds)
{
Database.LeadConvert Leadconvert = new Database.LeadConvert();
Leadconvert.setLeadId(LeadIds[0]);
LeadStatus Leads= [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
Leadconvert.setConvertedStatus(Leads.MasterLabel);
Database.LeadConvertResult Leadconverts = Database.convertLead(Leadconvert);
System.assert(Leadconverts.isSuccess());
}
}
When trying to push to prod, it states i need a test class. Tried the example below and i am not able to get it to work. Any input appreciated. When i run test it says lead already converted?
@isTest
public class TestAutoConvertLeads{
static testMethod void createnewlead() {
User userToCreate = [Select id from user where profile.name='System Administrator' Limit 1];
Test.startTest();
Lead leadToCreate =new Lead();
List<id> Ids= New List<Id>();
leadToCreate.ownerid= userToCreate.id;
leadToCreate.LastName ='Test1';
leadToCreate.LeadSource='Employee Referral';
leadToCreate.Rating='';
leadToCreate.Status='';
insert leadToCreate;
Ids.add(leadToCreate.id);
AutoConvertLeads.LeadAssign(Ids);
Test.stopTest();
}
}
Using it in a process builder. Any one doing this, can you provide guidance?
Public class AutoConvertLeads
{
@InvocableMethod
public static void LeadAssign(List<Id> LeadIds)
{
Database.LeadConvert Leadconvert = new Database.LeadConvert();
Leadconvert.setLeadId(LeadIds[0]);
LeadStatus Leads= [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
Leadconvert.setConvertedStatus(Leads.MasterLabel);
Database.LeadConvertResult Leadconverts = Database.convertLead(Leadconvert);
System.assert(Leadconverts.isSuccess());
}
}
When trying to push to prod, it states i need a test class. Tried the example below and i am not able to get it to work. Any input appreciated. When i run test it says lead already converted?
@isTest
public class TestAutoConvertLeads{
static testMethod void createnewlead() {
User userToCreate = [Select id from user where profile.name='System Administrator' Limit 1];
Test.startTest();
Lead leadToCreate =new Lead();
List<id> Ids= New List<Id>();
leadToCreate.ownerid= userToCreate.id;
leadToCreate.LastName ='Test1';
leadToCreate.LeadSource='Employee Referral';
leadToCreate.Rating='';
leadToCreate.Status='';
insert leadToCreate;
Ids.add(leadToCreate.id);
AutoConvertLeads.LeadAssign(Ids);
Test.stopTest();
}
}
Please try the below changes you will get 100%.
In Lead object Company,Status are Mandatory Field. And no need of profile soql here.
@isTest
public class TestAutoConvertLeads{
static testMethod void createnewlead() {
Lead leadToCreate =new Lead();
List<id> Ids= New List<Id>();
leadToCreate.LastName ='Test1';
leadToCreate.Company ='Test Company';
leadToCreate.LeadSource='Employee Referral';
leadToCreate.Rating='Hot'; /// Mention the correct Rating as per your field values
leadToCreate.Status='Open - Not Contacted'; /// Mention the correct status as per your field values
insert leadToCreate;
Ids.add(leadToCreate.id);
Test.startTest();
AutoConvertLeads.LeadAssign(Ids);
Test.stopTest();
}
}
Thanks,
Maharajan.C
I will try, and thank You for your help!
For example in contact email is required then you have enter related lead mapped field while creating lead record in test class.
Thanks,
Maharajan.C
Stack Trace: Class.AutoConvertLeads.LeadAssign: line 11, column 1 Class.TestAutoConvertLeads.createnewlead: line 16, column 1
I feel like if i can solve for this i may be ok!