You need to sign in to do that
Don't have an account?
Patrick G. Brown
Need help with simple before trigger
I'm working with a simple before delete trigger and am stumped on one piece. This trigger checks if a value is present on the Lead and if so, throws an error when trying to delete the Campaign Member. What I would like to do is check the status of this field, if it is not null, throw the error. If it is null, update the Lead Status to 'Resting". The trigger works for the error but doesn't update the Lead Status field. Any help would be most appreciated.
trigger cannotDeleteCampaignMember on CampaignMember (before delete) { if(System.Trigger.IsDelete){ Set<Id> leadIds = new Set<Id>(); for (CampaignMember cm: Trigger.old){ leadIds.add(cm.LeadId); } Map<Id, Lead> leadMap = new Map<Id, Lead>([SELECT Id, Current_Campaign__c, Status FROM Lead WHERE Id IN :leadIds]); for (CampaignMember cm: Trigger.old) {Lead associatedLead = leadMap.get(cm.LeadId); if(associatedLead.Current_Campaign__c!=null) { cm.adderror('Only the Marketing Team can delete Campaign Members. If you are on the Marketing Team and would like to delete this Campaign Member, go to the Lead and remove the value from the "Current Campaign" field first.'); } if(associatedLead.Current_Campaign__c==null) { associatedLead.Status = 'Resting'; } } } }
There is some new lines you have to add:
trigger cannotDeleteCampaignMember on CampaignMember (before delete) {
if(System.Trigger.IsDelete){
Set<Id> leadIds = new Set<Id>();
for (CampaignMember cm: Trigger.old){
leadIds.add(cm.LeadId);
}
Map<Id, Lead> leadMap = new Map<Id, Lead>([SELECT Id,Current_Campaign__c,Status FROM Lead WHERE Id IN :leadIds]);
List<Lead> Toupdate=new List<Lead>();
for (CampaignMember cm: Trigger.old)
{Lead associatedLead = leadMap.get(cm.LeadId);
if(associatedLead.Current_Campaign__c!=null)
{
cm.adderror('Only the Marketing Team can delete Campaign Members. If you are on the Marketing Team and would like to delete this Campaign Member, go to the Lead and remove the value from the "Current Campaign" field first.');
}
if(associatedLead.Current_Campaign__c==null)
{
associatedLead.Status = 'Resting';
Toupdate.add(associatedLead);
}
}
update Toupdate;
}
}
Can you please Let me know if it works or not!!!
If it helps don't forget to mark this as a best answer!!!
Thanks,
Raj
All Answers
There is some new lines you have to add:
trigger cannotDeleteCampaignMember on CampaignMember (before delete) {
if(System.Trigger.IsDelete){
Set<Id> leadIds = new Set<Id>();
for (CampaignMember cm: Trigger.old){
leadIds.add(cm.LeadId);
}
Map<Id, Lead> leadMap = new Map<Id, Lead>([SELECT Id,Current_Campaign__c,Status FROM Lead WHERE Id IN :leadIds]);
List<Lead> Toupdate=new List<Lead>();
for (CampaignMember cm: Trigger.old)
{Lead associatedLead = leadMap.get(cm.LeadId);
if(associatedLead.Current_Campaign__c!=null)
{
cm.adderror('Only the Marketing Team can delete Campaign Members. If you are on the Marketing Team and would like to delete this Campaign Member, go to the Lead and remove the value from the "Current Campaign" field first.');
}
if(associatedLead.Current_Campaign__c==null)
{
associatedLead.Status = 'Resting';
Toupdate.add(associatedLead);
}
}
update Toupdate;
}
}
Can you please Let me know if it works or not!!!
If it helps don't forget to mark this as a best answer!!!
Thanks,
Raj