Hello Friends,
trigger Campaign_AI on Campaign (after insert,after update) {
List<CampaignMemberStatus> cms2Delete = new List<CampaignMemberStatus>();
List<CampaignMemberStatus> cms2Insert = new List<CampaignMemberStatus>();
RecordType rt = [Select Id, Name from RecordType where Name = 'Meeting' limit 1];
//change default member statuses (Sent and Responded) for //select campaigns
Set <Id> cmpns = new Set <Id>();
for (Campaign c: trigger.new){
if (c.RecordTypeId == rt.Id)
cmpns.add(c.Id);
}
for (CampaignMemberStatus cm: [Select Id, Label, CampaignID FROM CampaignMemberStatus WHERE CampaignID IN :cmpns]){
if(cm.Label == 'Responded' ){
cms2Delete.add(cm);
} else if(cm.Label == 'Sent'){
// cms2Delete.add(cm);
cm.SortOrder = 5;
cms2Insert.add(cm);
CampaignMemberStatus cms1 = new CampaignMemberStatus(CampaignId=cm.CampaignID, Label='Active', HasResponded=false, IsDefault = True, SortOrder=3);
System.debug(cms1);
cms2Insert.add(cms1);
CampaignMemberStatus cms6 = new CampaignMemberStatus(CampaignId=cm.CampaignID, Label='Send Email', HasResponded=false, SortOrder=4);
System.debug(cms6);
cms2Insert.add(cms6);
CampaignMemberStatus cms3 = new CampaignMemberStatus(CampaignId=cm.CampaignID, Label='Accepted', HasResponded=true, SortOrder=6);
System.debug(cms3);
cms2Insert.add(cms3);
CampaignMemberStatus cms4 = new CampaignMemberStatus(CampaignId=cm.CampaignID, Label='Declined', HasResponded=true, SortOrder=7);
System.debug(cms4);
cms2Insert.add(cms4);
CampaignMemberStatus cms8 = new CampaignMemberStatus(CampaignId=cm.CampaignID, Label='Tentative', HasResponded=true, SortOrder=8);
System.debug(cms8);
cms2Insert.add(cms8);
CampaignMemberStatus cms5 = new CampaignMemberStatus(CampaignId=cm.CampaignID, Label='Attended', HasResponded=true, SortOrder=9);
System.debug(cms5);
cms2Insert.add(cms5);
CampaignMemberStatus cms7 = new CampaignMemberStatus(CampaignId=cm.CampaignID, Label='Cancelled', HasResponded=true, SortOrder=10);
System.debug(cms7);
cms2Insert.add(cms7);
CampaignMemberStatus cms9 = new CampaignMemberStatus(CampaignId=cm.CampaignID, Label='Invited', HasResponded=true, SortOrder=11);
System.debug(cms9);
cms2Insert.add(cms9);
}
}
//perform insert before delete because system requires at //least one CMS for a Campaign
upsert cms2Insert;
delete cms2Delete;
}
Today am gonna show you how to add custom status values for a campaign member while adding campaign members to the campaign in salesforce.com.
Requirement:- Business needs to have additional values to choose while adding campaign members to a particular campaign recordtype. Salesforce provides only two values to choose from while adding campaign members to a campaign. Now a specific set of business users need to have additional values to choose while adding up campaignmembers.
Solution:
We cannot add or remove the values to this dropdown through customization's. In order to achieve this business need, I have written a trigger on the campaign object to remove the old values and Add new values to the dropdown.
trigger Campaign_AI on Campaign (after insert,after update) {
List<CampaignMemberStatus> cms2Delete = new List<CampaignMemberStatus>();
List<CampaignMemberStatus> cms2Insert = new List<CampaignMemberStatus>();
RecordType rt = [Select Id, Name from RecordType where Name = 'Meeting' limit 1];
//change default member statuses (Sent and Responded) for //select campaigns
Set <Id> cmpns = new Set <Id>();
for (Campaign c: trigger.new){
if (c.RecordTypeId == rt.Id)
cmpns.add(c.Id);
}
for (CampaignMemberStatus cm: [Select Id, Label, CampaignID FROM CampaignMemberStatus WHERE CampaignID IN :cmpns]){
if(cm.Label == 'Responded' ){
cms2Delete.add(cm);
} else if(cm.Label == 'Sent'){
// cms2Delete.add(cm);
cm.SortOrder = 5;
cms2Insert.add(cm);
CampaignMemberStatus cms1 = new CampaignMemberStatus(CampaignId=cm.CampaignID, Label='Active', HasResponded=false, IsDefault = True, SortOrder=3);
System.debug(cms1);
cms2Insert.add(cms1);
CampaignMemberStatus cms6 = new CampaignMemberStatus(CampaignId=cm.CampaignID, Label='Send Email', HasResponded=false, SortOrder=4);
System.debug(cms6);
cms2Insert.add(cms6);
CampaignMemberStatus cms3 = new CampaignMemberStatus(CampaignId=cm.CampaignID, Label='Accepted', HasResponded=true, SortOrder=6);
System.debug(cms3);
cms2Insert.add(cms3);
CampaignMemberStatus cms4 = new CampaignMemberStatus(CampaignId=cm.CampaignID, Label='Declined', HasResponded=true, SortOrder=7);
System.debug(cms4);
cms2Insert.add(cms4);
CampaignMemberStatus cms8 = new CampaignMemberStatus(CampaignId=cm.CampaignID, Label='Tentative', HasResponded=true, SortOrder=8);
System.debug(cms8);
cms2Insert.add(cms8);
CampaignMemberStatus cms5 = new CampaignMemberStatus(CampaignId=cm.CampaignID, Label='Attended', HasResponded=true, SortOrder=9);
System.debug(cms5);
cms2Insert.add(cms5);
CampaignMemberStatus cms7 = new CampaignMemberStatus(CampaignId=cm.CampaignID, Label='Cancelled', HasResponded=true, SortOrder=10);
System.debug(cms7);
cms2Insert.add(cms7);
CampaignMemberStatus cms9 = new CampaignMemberStatus(CampaignId=cm.CampaignID, Label='Invited', HasResponded=true, SortOrder=11);
System.debug(cms9);
cms2Insert.add(cms9);
}
}
//perform insert before delete because system requires at //least one CMS for a Campaign
upsert cms2Insert;
delete cms2Delete;
}
use sort order to arrange the dropwown values.
once this trigger is saved we can go back and crosscheck the to see the Add with Status values that are available while adding campaign members to a campaign.
** make sure you go the campaign that is of the record type that we mentioned in the trigger.(In this case "Meeting")
This is how i met the business needs.
hopefully this will help you.
happy coding.
i am getting invalid record type
ReplyDeleteAnand
its an error
DeleteRastogi:
ReplyDeleteI m my Case i Have written it to work for only Campaigns that are of the Record Type "Meeting". May be you don't have any record Type that is with Meting as a name. That is the reason you are getting that error.
you have to change the following line:
RecordType rt = [Select Id, Name from RecordType where Name = 'Meeting' limit 1];