You need to sign in to do that
Don't have an account?
Jessica 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
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
leadToCreate.SalesCountry__r.id = 'a002000000F9XHaAAN';
Do it like below
Ah, here's your offending line!
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.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:
Happy coding! Today's overreaching admins are tomorrow's developers and architects!
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.