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
Jessica Rainbow 4Jessica Rainbow 4 

Converting Lead using Invocable Method

Hi all, 

I wouldn't call myself a developer per se, more like an overreaching admin.

Trying to build an invocable class that converts a lead if it meets a criteria set in Process Builder. 

The class itself appears to be working, but my test is failing and I have 0% code coverage. *cries*

Class: 
---------------------------------------------------------------------------------
Public class AutoConvertLead
{
    @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);
            Leadconvert.setDoNotCreateOpportunity(TRUE); 
            Database.LeadConvertResult Leadconverts = Database.convertLead(Leadconvert);
            System.assert(Leadconverts.isSuccess());
   }
}


---------------------------------------
Test:
----------------------------------------------

@isTest 
      public class TestAutoConvertLead{
            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 = '005D0000005sPGcIAM';
      leadToCreate.LastName ='Bertha';
      leadToCreate.Company='Salesforce';
      leadToCreate.Status='Sales Qualified';
      leadToCreate.SalesCountry__r.id = 'a002000000F9XHaAAN';
      leadToCreate.Email = 'bigbertha@testing.com';
      leadToCreate.Street ='1 Union Street';
      leadToCreate.City = 'London';
      leadToCreate.State = 'London';
      leadToCreate.PostalCode = 'SW8 2LN';
      leadToCreate.Country = 'United Kingdom';
      insert leadToCreate; 
 
      Ids.add(leadToCreate.id);
      AutoConvertLead.LeadAssign(Ids);
      
      Test.stopTest();
   }
}

------------------------------------------
Error: 
--------------------------------------------
System.NullPointerException: Attempt to de-reference a null object 
Stack Trace: Class.TestAutoConvertLead.createnewlead: line 13, column 1


Any help, or a nudge in the right direction would be appreciated.  

Thanks, 
Jess 
Raj VakatiRaj Vakati
Its failing becase if this line and not able to locate the a002000000F9XHaAAN id 

  leadToCreate.SalesCountry__r.id = 'a002000000F9XHaAAN';


Do it like below 
 
@isTest 
  public class TestAutoConvertLead{
		static testMethod void createnewlead() {
  User userToCreate = [Select id from user where profile.name='System Administrator' Limit 1];
  
  Test.startTest();   

SalesCountry__c r = new SalesCountry__c() ;
r.name='qweqwe';
insert r ;
 
  Lead leadToCreate =new Lead();
  List<id> Ids= New List<Id>();
  leadToCreate.Ownerid = '005D0000005sPGcIAM';
  leadToCreate.LastName ='Bertha';
  leadToCreate.Company='Salesforce';
  leadToCreate.Status='Sales Qualified';
  leadToCreate.SalesCountry__c =r.id ;
  leadToCreate.Email = 'bigbertha@testing.com';
  leadToCreate.Street ='1 Union Street';
  leadToCreate.City = 'London';
  leadToCreate.State = 'London';
  leadToCreate.PostalCode = 'SW8 2LN';
  leadToCreate.Country = 'United Kingdom';
  insert leadToCreate; 

  Ids.add(leadToCreate.id);
  AutoConvertLead.LeadAssign(Ids);
  
  Test.stopTest();
}
}

 
Nathan EhrmannNathan Ehrmann
leadToCreate.SalesCountry__r.id = 'a002000000F9XHaAAN;

Ah, here's your offending line!

So you're attempting to do a xObject (read: "cross object") relationship, but here's the problem: your record doesn't exist yet! It is indeed "null". Instead, what you want to write is this:

leadToCreate.SalesCountry__c = 'a002000000F9XHaAAN';
So you're just setting the Id value for the Lookup field. HOWEVER, another issue is that in the isolated context of a test class, none of your data exists! So you would need to create a record (let's say in the Country__c table) to use in this reference. Otherwise, you will get the same error, since this record cannot be found.

Happy coding! Today's overreaching admins are tomorrow's developers and architects!
cvuyyurucvuyyuru
Hi jessica,

I foundtTwo mistakes in the code.

1) you don't need to assign the owner unless your logic is based on owner. Remove that line of code of assigning ownerId to lead and you can remove that query even.
2) Sales Country should be inserted in the test class and assign that Id to SalesCountry__c field on Lead.
 
Raj VakatiRaj Vakati
@isTest 
public class TestAutoConvertLead{
	static testMethod void createnewlead() {
	
	User u = new User(
 ProfileId = [SELECT Id FROM Profile WHERE Name = 'System Administrator'].Id,
 LastName = 'last',
 Email = 'puser000@amamama.com',
 Username = 'puser000@amamama.com' + System.currentTimeMillis(),
 CompanyName = 'TEST',
 Title = 'title',
 Alias = 'alias',
 TimeZoneSidKey = 'America/Los_Angeles',
 EmailEncodingKey = 'UTF-8',
 LanguageLocaleKey = 'en_US',
 LocaleSidKey = 'en_US',
 UserRoleId = r.Id
);



Test.startTest();   
System.runAs(u){
SalesCountry__c r = new SalesCountry__c() ;
r.name='qweqwe';
insert r ;

Lead leadToCreate =new Lead();
List<id> Ids= New List<Id>();
leadToCreate.Ownerid = '005D0000005sPGcIAM';
leadToCreate.LastName ='Bertha';
leadToCreate.Company='Salesforce';
leadToCreate.Status='Sales Qualified';
leadToCreate.SalesCountry__c =r.id ;
leadToCreate.Email = 'bigbertha@testing.com';
leadToCreate.Street ='1 Union Street';
leadToCreate.City = 'London';
leadToCreate.State = 'London';
leadToCreate.PostalCode = 'SW8 2LN';
leadToCreate.Country = 'United Kingdom';
insert leadToCreate; 

Ids.add(leadToCreate.id);
AutoConvertLead.LeadAssign(Ids);

Test.stopTest();
}
}
}