• sparkorg
  • NEWBIE
  • 0 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 5
    Replies

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

The main objective of the trigeer is as follows:

 

The leads are assigned to a campaign based on a custom field value of the lead called " Hub spot score"

If the Hub spot score is null , then that lead is not added to the campaign else the lead is added to the campaign . 

Note: Leads with a score is added to the campaign , no matter what the score it (hubspot score value>0. the lead is added to the campaign).

 

 

 

trigger LeadToCampaign on Lead (after update, after insert) {
set<string>existingcampaignmembers=new set<string>();//A set is created which has leads with the CampaignId in the "LeadId-CampaignId" format
For(CampaignMember cM:[SELECT LeadId ,CampaignId from CampaignMember where LeadId IN: Trigger.newMap.keyset()])//fetches all the members related to the context
{
existingcampaignmembers.add(cM.LeadId + '-' + cM.campaignId);
}
List<CampaignMember> cmember = new List<CampaignMember>();//create a list to hold all campaignmembers records to be inserted
for(Lead le : Trigger.New) {
if(le.hub_spot_score__c != null){//check whether the score is null
String uniquenessCriteria = le.Id + '-' + le.hub_spot_score__c;//create string to check the existing leads
//check in the set if this is associated with any campaign records and create new campaignmembers if they are not found
if(!existingcampaignmembers.contains(uniquenessCriteria)){
CampaignMember cm = new CampaignMember();//creating a new campaignmember
cm = new CampaignMember();
cm.LeadId = le.Id;
cm.CampaignId = le.hub_spot_score__c;
cm.Status = 'Responded';
//Add this record in the list of Campaign Members to be inserted
cmember.add(cm);
}
}

//Insert records in database
if(cmember.size() > 0)
database.insert( cmember, false);
}
}

 

 

An error is displayed that a "an illegal assignment decimal to id" in the line 16

'cm.CampaignId = le.hub_spot_score__c;'

Please can someone help me clear this error and to add a query for the specific campaign to which the leads can be added .

 

 

Thank you for time in advance

 

 

Regards,

Angela Joseph

Spark Orange ,LLC



Hello,

 

I am very new to Apex and I need some serious help in creating a trigger

 

The main objective of the trigger is as follows:

The trigger must link leads to a particular campaign ( a common single campaign ) based on the leads "Hub score". The hub score can vary from 0 to any maximum value. Hence when the lead has a 0 value no campaign is generated for that particular lead, but if the lead had any hub score (any value from 1 to Max) a campaign should be generated for that particular lead.

 

Thank you in advance,

 

Regards,

Angela

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

The main objective of the trigeer is as follows:

 

The leads are assigned to a campaign based on a custom field value of the lead called " Hub spot score"

If the Hub spot score is null , then that lead is not added to the campaign else the lead is added to the campaign . 

Note: Leads with a score is added to the campaign , no matter what the score it (hubspot score value>0. the lead is added to the campaign).

 

 

 

trigger LeadToCampaign on Lead (after update, after insert) {
set<string>existingcampaignmembers=new set<string>();//A set is created which has leads with the CampaignId in the "LeadId-CampaignId" format
For(CampaignMember cM:[SELECT LeadId ,CampaignId from CampaignMember where LeadId IN: Trigger.newMap.keyset()])//fetches all the members related to the context
{
existingcampaignmembers.add(cM.LeadId + '-' + cM.campaignId);
}
List<CampaignMember> cmember = new List<CampaignMember>();//create a list to hold all campaignmembers records to be inserted
for(Lead le : Trigger.New) {
if(le.hub_spot_score__c != null){//check whether the score is null
String uniquenessCriteria = le.Id + '-' + le.hub_spot_score__c;//create string to check the existing leads
//check in the set if this is associated with any campaign records and create new campaignmembers if they are not found
if(!existingcampaignmembers.contains(uniquenessCriteria)){
CampaignMember cm = new CampaignMember();//creating a new campaignmember
cm = new CampaignMember();
cm.LeadId = le.Id;
cm.CampaignId = le.hub_spot_score__c;
cm.Status = 'Responded';
//Add this record in the list of Campaign Members to be inserted
cmember.add(cm);
}
}

//Insert records in database
if(cmember.size() > 0)
database.insert( cmember, false);
}
}

 

 

An error is displayed that a "an illegal assignment decimal to id" in the line 16

'cm.CampaignId = le.hub_spot_score__c;'

Please can someone help me clear this error and to add a query for the specific campaign to which the leads can be added .

 

 

Thank you for time in advance

 

 

Regards,

Angela Joseph

Spark Orange ,LLC



Hello,

 

I am very new to Apex and I need some serious help in creating a trigger

 

The main objective of the trigger is as follows:

The trigger must link leads to a particular campaign ( a common single campaign ) based on the leads "Hub score". The hub score can vary from 0 to any maximum value. Hence when the lead has a 0 value no campaign is generated for that particular lead, but if the lead had any hub score (any value from 1 to Max) a campaign should be generated for that particular lead.

 

Thank you in advance,

 

Regards,

Angela