You need to sign in to do that
Don't have an account?
Tanushree Singh 11
Test class running successfully but giving 0% code coverage
I have a trigger as follows:
trigger LeadConvert on Lead (after update) {
// no bulk processing; will only run from the UI
if (Trigger.new.size() == 1) {
if (Trigger.old[0].isConverted == false && Trigger.new[0].isConverted == true) {
// if a new account was created
if (Trigger.new[0].ConvertedAccountId != null) {
// update the converted account with some text from the lead
Account a = [Select a.Id, a.Initial_Payment__c, a.BPS__c,a.Monthly_Payment__c, a.X401_k_Plan_Type__c From Account a Where a.Id = :Trigger.new[0].ConvertedAccountId];
if (a.X401_k_Plan_Type__c == 'Individual')
{
a.NumberOfEmployees = Integer.Valueof(Trigger.new[0].Number_of_Owners__c);
}
update a;
}
}
}
}
It maps converted lead field to an account field.
Have written a test for the same which is giving 0% code coverage.
@isTest
private class LeadConvertTest {
@testSetup static void setup() {
User users2=createUser();
system.runAs(users2){
Lead l1=new Lead();
l1.LastName='test';
l1.Number_of_Owners__c = 25;
insert l1;
}
}
@isTest
static void updateAccount1(){
user us2=[select Id from user where Email ='xxx@ddd.com'];
system.runAs(us2){
Account a2=[select Id from Account limit 1];
a2.X401_k_Plan_Type__c = 'Individual';
update a2;
}
}
private static user createUser(){
User plannerUser2 = new User(
ProfileId = [SELECT Id,name FROM Profile where name='System Administrator'].Id,
LastName = 'last',
Email = 'xxx@ddd.com',
Username = 'xxx@ddd.com' + System.currentTimeMillis(),
CompanyName = 'TEST1',
Title = 'title',
Alias = 'alias',
TimeZoneSidKey = 'America/Los_Angeles',
EmailEncodingKey = 'UTF-8',
LanguageLocaleKey = 'en_US',
LocaleSidKey = 'en_GB',
IsActive=true);
database.insert(plannerUser2);
return plannerUser2;
}
}
I am new to development and any help woulf be graetly appreciated.
trigger LeadConvert on Lead (after update) {
// no bulk processing; will only run from the UI
if (Trigger.new.size() == 1) {
if (Trigger.old[0].isConverted == false && Trigger.new[0].isConverted == true) {
// if a new account was created
if (Trigger.new[0].ConvertedAccountId != null) {
// update the converted account with some text from the lead
Account a = [Select a.Id, a.Initial_Payment__c, a.BPS__c,a.Monthly_Payment__c, a.X401_k_Plan_Type__c From Account a Where a.Id = :Trigger.new[0].ConvertedAccountId];
if (a.X401_k_Plan_Type__c == 'Individual')
{
a.NumberOfEmployees = Integer.Valueof(Trigger.new[0].Number_of_Owners__c);
}
update a;
}
}
}
}
It maps converted lead field to an account field.
Have written a test for the same which is giving 0% code coverage.
@isTest
private class LeadConvertTest {
@testSetup static void setup() {
User users2=createUser();
system.runAs(users2){
Lead l1=new Lead();
l1.LastName='test';
l1.Number_of_Owners__c = 25;
insert l1;
}
}
@isTest
static void updateAccount1(){
user us2=[select Id from user where Email ='xxx@ddd.com'];
system.runAs(us2){
Account a2=[select Id from Account limit 1];
a2.X401_k_Plan_Type__c = 'Individual';
update a2;
}
}
private static user createUser(){
User plannerUser2 = new User(
ProfileId = [SELECT Id,name FROM Profile where name='System Administrator'].Id,
LastName = 'last',
Email = 'xxx@ddd.com',
Username = 'xxx@ddd.com' + System.currentTimeMillis(),
CompanyName = 'TEST1',
Title = 'title',
Alias = 'alias',
TimeZoneSidKey = 'America/Los_Angeles',
EmailEncodingKey = 'UTF-8',
LanguageLocaleKey = 'en_US',
LocaleSidKey = 'en_GB',
IsActive=true);
database.insert(plannerUser2);
return plannerUser2;
}
}
I am new to development and any help woulf be graetly appreciated.
Your trigger runs on after update and on converted Lead but you have not converted lead in your trigger.
Try the below code and let me know if you have any queries.
P.S: mark this as the best answer if this helped you
Thanks
Shubham Kumar
All Answers
Your trigger will execute on the update of the lead. You are updating only the account in the test class. You need to perform the update operation on the lead record. Please refer the below code.
Hope this will help you, if yes then mark it as best answer so it can also help others.
Many Thanks,
Sunil Rathore
Your trigger runs on after update and on converted Lead but you have not converted lead in your trigger.
Try the below code and let me know if you have any queries.
P.S: mark this as the best answer if this helped you
Thanks
Shubham Kumar