You need to sign in to do that
Don't have an account?
How to write a test class for Apex Trigger that uses isClone?
Hello all,
I have very simple trigger that loads a custom field NewLead.NetNewLead__c=TRUE when a Lead is Cloned.
trigger Load_Cloned_NetNewLead_Trigger on Lead (before update) {
for (Lead NewLead : Trigger.new) {
if (NewLead.isClone() ){
NewLead.NetNewLead__c=TRUE;
}
}
}
I am unable to write a test class to meet code coverage requirement. How do I simulate a Cloned Lead?
Thanks for your help.
@isTest
private class Load_Cloned_NetNewLead_TriggerTest {
static testMethod void testEx(){
Test.startTest();
Account a = new Account(
Name ='test 1', Industry='Academia', SI_Type__c='Prospect', BillingCountry='United States');
insert a;
Schema.SObjectType.Contact.getRecordTypeInfosByName().get('Business_Contact').getRecordTypeId();
//CREATE CONTACT
Contact c=new Contact(
FirstName='fname',recordtypeid ='0121A0000007rgsQAA',
LastName = 'lname', accountid = a.Id ,
Email = 'Test_002.email@gmail.com',
Clinical_Services__c = True,
Phone = '9743800309');
insert c;
//CREATE 1ST LEAD
Lead l = new Lead(FirstName='FirstName-Test1', LastName='LastName-Test1', Company='Company-Test1', Status='Untouched' , Email='Test_001.email@gmail.com', Industry='Academia' ,LeadSource='Advertising(Online)', Almac_Division__c='CS', NetNewLead__c=TRUE );
insert l ;
//CREATE 2ND LEAD
Lead l1 = new Lead(FirstName='FirstName-Test2', LastName='LastName-Test2', Company='Company-Test2', Status='Untouched', Email='Test_002.email@gmail.com', Industry='Academia' ,LeadSource='Advertising(Online)', Almac_Division__c='CS', NetNewLead__c=FALSE );
insert l1 ;
//ATTEMPT TO CLONE LEAD???
l1.FirstName='FirstName-Test3';
l1.LastName='LastName-Test3';
l1.Company='Company-Test3';
l1.Status='Untouched';
update l1 ;
System.assertEquals(true, l1.isClone());
Test.stopTest();
}
}
That issue is caused because your trigger is set to run only before update. In that case, you have to make an update to the cloned record after you insert. That will make you trigger be called and get coveraged.
All Answers
try the below one:
@isTest
public class LeadCloneTest {
static testmethod void LeadClonetriggerTest(){
Lead l = new Lead();
l.LastName = 'Test Lead';
l.Company = 'test Comp';
l.Status = 'Untouched';
// Add all Remaining mandatory Fields to insert the Lead
insert l;
Lead l1 = l.clone(false, true);
insert l1;
}
}
Thanks,
Maharajan.C
You can use the clone method of the sObject class:
You may check the parameters on the clone method here: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_sobject.htm
If it helped solve your issue, please mark as the best answer.
Best regards.
Maharajan, Gabriel,
Thanks for your suggestions, I have tried both to no avail.
//insert lead l
Lead l = new Lead(FirstName='FirstName-Test1', LastName='LastName-Test1', Company='Company-Test1', Status='Untouched' , Email='Test_001.email@gmail.com', Industry='Academia' ,LeadSource='Advertising(Online)', Almac_Division__c='CS', Interested_In__c='STEMS', NetNewLead__c=TRUE );
insert l ;
//insert lead l1
Lead l1 = new Lead(FirstName='FirstName-Test2', LastName='LastName-Test2', Company='Company-Test2', Status='Untouched', Email='Test_002.email@gmail.com', Industry='Academia' ,LeadSource='Advertising(Online)', Almac_Division__c='CS', Interested_In__c='STEMS', NetNewLead__c=FALSE );
insert l1 ;
//clone above lead
//l1.Email='Test_003.email@gmail.com';
Lead clonedLead = l1.clone(false, false, false, false);
insert clonedLead;
//manual clone
l1.LastName='LastName-Test4';
l1.Email='Test_003.email@gmail.com';
update L1;
Without the //manual clode I get 0 coverage. What am I doing wrong?
That issue is caused because your trigger is set to run only before update. In that case, you have to make an update to the cloned record after you insert. That will make you trigger be called and get coveraged.
Gabriel,
I am so grateful for your guidance!
I changed my code as you instructed and reached 100%!
//clone above lead
l1.Email='Test_003.email@gmail.com';
Lead clonedLead = l1.clone(false, false, false, false);
insert clonedLead;
//force before update trigger to fire
update clonedLead;
Thank you again!
Robert
To help keep our fórum clear, is recommended that you choose a best answer, but you can give like to every post that helped you solved your issue.
Best Regards
Gabriel