In my Apex class i am converting leads. When i test my code through UI it is working fine but when i run my test class it get failed and shows this error:

22:21:47.413 (413465000)|FATAL_ERROR|leadconvert.BulkLeadConvert.LeadInUseByWorkflowException: 00Qi0000003QfGrEAK

(System Code)

22:21:47.413 (413480000)|FATAL_ERROR|leadconvert.BulkLeadConvert.LeadInUseByWorkflowException: 00Qi0000003QfGrEAK

(System Code) 22:21:47.414 (414323000)|DML_END|[237] 22:21:47.414 (414406000)|EXCEPTION_THROWN|[237]|System.DmlException: ConvertLead failed. First exception on row 0; first error: RECORD_IN_USE_BY_WORKFLOW, Unable to convert lead that is in use by workflow: []

I am just passing list of leads as parameter into convertlead() in Apex class :

List<Database.LeadConvertResult> lcr = Database.convertLead(listOfLeadConvert);

My test class

public class Test_CVFC_massConvertwithInputs {
public static testmethod void Test_CVFC_massConvertwithInputs(){
    Account a = new Account (); 
    a.Name = 'LeadCompany';
    insert a;

    Opportunity o = new Opportunity();
    o.AccountId = a.Id;
    o.Name = 'LeadCompany';
    O.StageName = 'Prospecting';
    O.CloseDate = date.today();

    insert o;

    Contact c = new Contact();
    c.AccountId = a.id;
    c.FirstName = 'LeadFirstname';
    c.LastName = 'Leadlastname';
    c.MailingStreet = 'LeadStreet';
    c.MailingCity = 'LeadCity';
    c.MailingState = 'LeadState';
    c.MailingPostalCode = '09877';
    insert c;

    Lead l = new Lead();
    //l.Name = 'LeadName';
    l.Company = 'LeadCompany';
    l.IsUnreadByOwner__c = true;
    l.Description = 'leadDescription';
    //l.Status = 'Closed - Converted';
    l.FirstName = 'LeadFirstname';
    l.LastName ='Leadlastname';
    l.Street = 'LeadStreet';
    l.City = 'LeadCity';
    l.State = 'LeadState';
    l.PostalCode = '09877';
    insert l; 

    List<Lead> leadLst = new List<Lead>();
    leadLst = [SELECT  FirstName, LastName, Street, City, State, PostalCode from Lead where id =: l.Id ];

    /*Database.LeadConvert lc = new Database.LeadConvert();

    LeadStatus convertStatus = [Select Id, MasterLabel from LeadStatus where IsConverted=true limit 1];

    PageReference pageRef = Page.massConvertWithInputs;
    pageRef.getParameters().put('toconvertLeadIds', l.id); 

    ApexPages.StandardController sc = new ApexPages.standardController(l);  

    CVFC_massConvertwithInputs cmc = new CVFC_massConvertwithInputs(sc);
    system.debug('Wrapper list size '+cmc.wrapperList.size());
    for(Integer i = 0; i < cmc.wrapperList.size(); i++){
        System.debug('Values in Wrapper list ' + cmc.wrapperList[i]);
    //cmc.leadIds = leadLst;



is there any thing i am missing out please guide me.

  • 4
    Is there a time-based workflow action pending on the Lead ? – techtrekker May 23 '13 at 13:32
  • I'd suggest you also look at your Account and Opportunity objects to see what the required fields are. Not satisfying those requirements may be why you're getting the workflow exceptions. You could try doing "Database Insert" using the "false" option which allows the insert to succeed even though not all of the required fields are completed. Unfortunately, your workflow rules if enabled will likely continue to throw exceptions. – crmprogdev May 27 '13 at 16:27
  • also not sure why you have seealldata=true.. – Mohith Shrivastava Oct 26 '13 at 17:29
  • Have you tried changing the field update step in your workflow as a final approval step? I got this from a similar issue found here: success.salesforce.com/answers?id=90630000000gxQfAAI – Programmable Medley Feb 5 '14 at 18:08

As noted by the commenters, this is almost assuredly that your test Lead is entering into a time-based workflow or approval process.

To verify:

  1. Use anonymous APEX to insert your test Lead
  2. Go to Setup | Monitoring | Time-Based Workflow and fill out the query form : Object equals Lead. If your test lead is displayed - you have a time-based workflow that will prevent lead conversion. Change your test data so as not to trigger the time-based workflow
  3. If not found, in #2, look for an open approval process by going to Setup | Data Management | Mass Transfer Approval Processes and fill in the query Submitter Last name equals . That said, it would be an unusual org that used Approval Processes on Leads. If present, figure out a way for your test data to not initiate the approval process

Lastly, in your post, you commented out all the lead convert code as it presumably is inside of the controllerExtension CVFC_massConvertwithInputs.convert(). It is not clear what happens within the cmc.convert() method (e.g. are you enhancing the Lead and resaving it?).

| improve this answer | |

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.