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
sparkorgsparkorg 

Unit test : trigger that assigns campaign to leads based on the lead Hubscore.

Hello ,

 

I am very new to Apex code and I am facing difficulties in my unit test code. The main functionality of the trigger is to assign a standard campaign to the leads based on the lead hub score. I wrote a unit test the code and it is as follows

 

@istest
public class LeadCampaignTestClass{
private static testMethod void testclass1 () {
//Create one campaign record
Campaign ca = new Campaign(Name='Test Campaign');
//required fields
insert ca;
// create a lead record
Lead le = new lead();
//required fields
le.hub_spot_score__c = 25;
insert le;
// adding a campaign member
List<CampaignMember> members = [SELECT Id FROM CampaignMember
WHERE LeadId
= :le.Id AND CampaignID = :ca.Id];
System.assertEquals(1, members.size());
}
// Negative test class
private static testMethod void testclass2(){
//Create one campaign record
Campaign ca = new Campaign(Name='Test Campaign');
insert ca;
//create lead record
Lead le = new lead();
//No hub score is added
insert le;
List<CampaignMember> members = [SELECT Id FROM CampaignMember WHERE LeadId
= :le.Id AND CampaignID = :ca.Id];
System.assertEquals(1, members.size());
}
}

 

 

When I tried to run the test , the foowing error was displayed :

 

Apex Test Result Detail  

Time Started 8/20/2013 10:44 AM Class LeadCampaignTestClass Method Name testclass2 Pass/Fail Fail Error Message System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [LastName, Company]: [LastName, Company] Stack Trace

Class.LeadCampaignTestClass.testclass2: line 27, column 1

 

 

Can some one help me with this issue asap .

 

Regards,

Angela Joseph

VaasuVaasu
You are insertying lead with out giving data in required fields.
need to give values in required fields
le.Company = 'my company';
le.lastname = 'somename';
insert le;
Sgt_KillerSgt_Killer

While inserting the leads using insert statement, ensure that you have updated the field values such as LastName and Company as they are mandatory fields on lead object.

 

Lead le = new lead();

le.Name = 'xxx';

le.Company='yyy';

 

insert le;

sparkorgsparkorg

@istest
public class LeadCampaignTestClass{
private static testMethod void testclass1 () {
//Create one campaign record
Campaign ca = new Campaign(Name='Test Campaign');
//required fields
insert ca;
// create a lead record
Lead le = new lead();
le.lastname = 'Scored';
le.Company = 'Sparkorange';
le.hub_spot_score__c = 25;
insert le;
// adding a campaign member
List<CampaignMember> members = [SELECT Id FROM CampaignMember
WHERE LeadId
= :le.Id AND CampaignID = :ca.Id];
System.assertEquals(1, members.size());
}
// Negative test class
private static testMethod void testclass2(){
//Create one campaign record
Campaign ca = new Campaign(Name='Test Campaign');
insert ca;
//create lead record
Lead le = new lead();
le.Lastname = 'null';
le.Company = 'Sparkorange';
insert le;
List<CampaignMember> members = [SELECT Id FROM CampaignMember WHERE LeadId
= :le.Id AND CampaignID = :ca.Id];
System.assertEquals(1, members.size());
}
}

 

 

I made the changes but the following error is appearing:

Apex Test Result Detail  

Time Started 8/20/2013 3:09 PM Class LeadCampaignTestClass Method Name testclass2 Pass/Fail Fail Error Message System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, LeadToCampaign: execution of AfterInsert

caused by: System.QueryException: List has no rows for assignment to SObject

Trigger.LeadToCampaign: line 4, column 1: [] Stack Trace Class.LeadCampaignTestClass.testclass2: line 29, column 1

 

Please can you help me help me with this .

 

Regards,

Angela Joseph

sushant sussushant sus

change object instance name in

 

private static testMethod void testclass2(){

like

Campaign camp = new Campaign(Name='Test Campaign');

Lead lea = new lead();

 

then it will work

 

in your case you are inserting same instance of object 2 times

 

 

sparkorgsparkorg

I made the changes as u said ,but dont know whats going wrong again  this is what I get:

 

@istest
public class LeadCampaignTestClass{
private static testMethod void testclass1 () {
//Create one campaign record
Campaign ca = new Campaign(Name='Test Campaign');
//required fields
insert ca;
// create a lead record
Lead le = new lead();
le.lastname = 'Scored';
le.Company = 'Sparkorange';
le.hub_spot_score__c = 25;
insert le;
// adding a campaign member
List<CampaignMember> members = [SELECT Id FROM CampaignMember
WHERE LeadId
= :le.Id AND CampaignID = :ca.Id];
System.assertEquals(1, members.size());
}
// Negative test class
private static testMethod void testclass2(){
//Create one campaign record
Campaign camp = new Campaign(Name='Test Campaign');
insert camp;
//create lead record
Lead lea = new lead();
lea.Lastname = 'null';
lea.Company = 'Sparkorange';
insert lea;
List<CampaignMember> members = [SELECT Id FROM CampaignMember WHERE LeadId
= :lea.Id AND CampaignID = :camp.Id];
System.assertEquals(1, members.size());
}
}

 

Error 1:

Error MessageSystem.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, LeadToCampaign: execution of AfterInsert

caused by: System.QueryException: List has no rows for assignment to SObject

Trigger.LeadToCampaign: line 4, column 1: []Stack TraceClass.LeadCampaignTestClass.testclass2: line 29, column 1

 

Error 2 :

Error MessageSystem.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, LeadToCampaign: execution of AfterInsert

caused by: System.QueryException: List has no rows for assignment to SObject

Trigger.LeadToCampaign: line 4, column 1: []Stack TraceClass.LeadCampaignTestClass.testclass1: line 13, column 1

 

 

Regards,

Angela Joseph

Mani_SFDCMani_SFDC

The error are the result of the two SOQL queries.

 

1. List<CampaignMember> members = [SELECT Id FROM CampaignMember
WHERE LeadId
= :le.Id AND CampaignID = :ca.Id];

 

2. List<CampaignMember> members = [SELECT Id FROM CampaignMember WHERE LeadId
= :lea.Id AND CampaignID = :camp.Id];

 


Both the above queries are returning null. Kindly relate the Campaign ca and the Lead le to get the result in the 1st query and similarly relate lea and camp in the second query