You need to sign in to do that
Don't have an account?
aKallNV
Duplicate_Value on Campaign Member Status Trigger
I have written a trigger to standardize Campaign Member Status options. It works great with one exception: it throws a Dupliacte_Value error when I try to Clone a campaign...not sure where to start with debugging.
trigger autoCampaignMemberStatusTrigger on Campaign (after insert) { List<Campaign> newCamps = [select Id, RecordType.Name from Campaign where Id IN :trigger.new AND ParentID = Null AND (RecordType.Name = 'Training' OR RecordType.Name = 'Presentation')]; List<CampaignMemberStatus> cms = new List<CampaignMemberStatus>(); Set<Id> camps = new Set<Id>(); List<CampaignMemberStatus> cms2Delete = new List<CampaignMemberStatus>(); List<CampaignMemberStatus> cms2Insert = new List<CampaignMemberStatus>(); for(Campaign camp : newCamps){ camps.add(camp.Id); } for(CampaignMemberStatus cm : [select Id, Label, CampaignId from CampaignMemberStatus where CampaignId IN :camps]) { if(cm.Label == 'Sent' || cm.Label == 'Responded') { cms2Delete.add(cm); } CampaignMemberStatus cms1 = new CampaignMemberStatus(CampaignId = cm.CampaignId, HasResponded=false, Label = 'Invited', SortOrder = 3, isDefault = true); cms2Insert.add(cms1); CampaignMemberStatus cms2 = new CampaignMemberStatus(CampaignId = cm.CampaignId, HasResponded=true, Label = 'Accepted', SortOrder = 4); cms2Insert.add(cms2); CampaignMemberStatus cms3 = new CampaignMemberStatus(CampaignId = cm.CampaignId, HasResponded=true, Label = 'Attended', SortOrder = 5); cms2Insert.add(cms3); CampaignMemberStatus cms4 = new CampaignMemberStatus(CampaignId = cm.CampaignId, HasResponded=true, Label = 'Declined', SortOrder = 6); cms2Insert.add(cms4); } insert cms2Insert; delete cms2Delete; }
You could query for existing values, and if they are there already, don't insert "new" ones. Do a query and store the results in a Set, and then if !myset.contains(value) add to your list. Otherwise, move on to the next one.
All Answers
You are trying to add "Invited" once for every status in teh campaign that already exists (given your query of CMS).
This should be closer to the solution you need:
Thanks. Unfortunately, your suggestion threw the same error. However, i think your comment has helped me to understand the problem better. Since this is only happening when a Campaign is cloned the CMSs are being cloned too, and are arleady present when the trigger tries to insert the same values. I kind of already understood this but wasn't really able to ariculate it very well, until now.
Anyway, it seems that the solution would be to delete all existing CMS values, not just the defaults of 'Sent' and 'Responded' or to somehow add criteria that would filter out records that resulted from a clone, but not sure how that would be done.
Yeah-it may be tricky as you always have to have at least 1 CMS for a campaign that is default & responded, so you may need several operations:
- Clone
- Add dummy CMS
- Delete all other CMS
- Add in desired CMS
- Delete dummy CMS
You can probably merge a few steps like re-using the dummy CSM to save DML inserts/deletes.
You could query for existing values, and if they are there already, don't insert "new" ones. Do a query and store the results in a Set, and then if !myset.contains(value) add to your list. Otherwise, move on to the next one.
Thanks guys. I ended up with this, which seems to work. Rather than use a Set as x20d suggested, I decided to use a map because I didn't see how a set would handle a bulk trigger. Let me know if I'm mistaken.
Thanks for the help.
Can u plz provide me Test Class of the same code.