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 

Tigger to add leads to campaign

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



georggeorg

in line number 16 you are assigning hub_spot_score__c to campaignid on campaignmember object.

 

From the error i can see that both data type are not matching.

 

Please check the data type and assign campaign Id filed to cm.CampaignId.

 

I did not look into the complete code..if you can't resolve it please revert back

 

sparkorgsparkorg

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

Campaign hubSpotCampaign = [SELECT Id FROM Campaign WHERE Name = 'HubS' LIMIT 1];
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

CampaignMember cm = new CampaignMember();//creating a new campaignmember
cm = new CampaignMember();
cm.LeadId = le.Id;
cm.CampaignId = hubSpotCampaign.Id;
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);
}
}

 

 

I have made couple of changes to it , but when I tested it buy editing the hub spot score of the lead the following error msg is being displayed

"Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger LeadToCampaign caused an unexpected exception, contact your administrator: LeadToCampaign: execution of AfterUpdate caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.LeadToCampaign: line 7, column 1"

 

 

I tried changing the campaign too.Please can you help me resolve this issue

 

 

Regards,

Angela Joseph

Spark Orange,LLC

asish1989asish1989
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

	Campaign hubSpotCampaign = [SELECT Id FROM Campaign WHERE Name = 'HubS' LIMIT 1];
	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

			CampaignMember cm = new CampaignMember();//creating a new campaignmember
			cm = new CampaignMember();
			cm.LeadId = le.Id;
			if(hubSpotCampaign != null && hubSpotCampaign.size()>0){
				cm.CampaignId = hubSpotCampaign.Id;
			}	
			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);
	} 
}

 

 

check whether campaign named  "HubS" exists or not

sparkorgsparkorg

Yes I have created a campaign named as "HubS"

 

A new error is dispalyed in the 12 th line!!Please can you help me resolve it.

 

That states - "Method does not exist or incorrect signature :[SOBJECT:Campaign].size()"

 

"

if(hubSpotCampaign != null && hubSpotCampaign.size()>0){

asish1989asish1989

Try this

 

if(hubSpotCampaign != null && hubSpotCampaign !=''){