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
mahesh1.396434879374128E12mahesh1.396434879374128E12 

I want to replace the standard CampaignMeberstatus values of a campaign with the values which are in CustomSetting

I wrote code for it but it throwing Error : first error: DUPLICATE_VALUE, duplicate value found: duplicates value on record with id


Here is the Code :

trigger marketingCms on Campaign (After Insert) {

List<MarketingCMS__c> cust_Mcms =MarketingCMS__c.getAll().Values();
system.debug('*********cust_Mcms **********'+cust_Mcms);                       
set<ID> cam_Set = New set<ID>(); 
RecordType RT=[SELECT Id,Name FROM RecordType WHERE sobjectType='Campaign' AND Name = 'Marketing Campaign'];
List<CampaignMemberStatus> cms2Delete = new List<CampaignMemberStatus>();
List<CampaignMemberStatus> cms2Insert = new List<CampaignMemberStatus>();  
if(trigger.isafter)  {                     
for(Campaign vCam : trigger.new)
{
    if((RT.name=='Marketing Campaign')&&(vCam.type!=null))
    {
    cam_Set.add(vCam.Id);
    CampaignMemberStatus CMSt ;
    for(MarketingCMS__c Mcm : cust_Mcms )
        {
    CMSt = New CampaignMemberStatus();
                CMSt.CampaignId = vCam.id;
                CMSt.HasResponded = Mcm.Responded__c;
                CMSt.IsDefault = Mcm.IsDefault__c ;
                CMSt.Label = Mcm.Label__c;
                CMSt.SortOrder = integer.valueOf(Mcm.SortOrder__c);
                cms2Insert.add(CMSt);
        }
    }
}
for(CampaignMemberStatus cm : [select Id, Label, CampaignId from CampaignMemberStatus where CampaignId IN :cam_Set])
    {
    if(cm.Label == 'Sent' || cm.Label == 'Responded')
         {           
            cms2Delete.add(cm);
         }
    }
    database.Delete(cms2Delete);
         
    database.Insert(cms2Insert);
}
}


Thanks In Advance.Please help me in this issue.

Tony TannousTony Tannous
Dear,

the source of this error is the  SortOrder field, Make sure that the sort order does not match any existing sort order or a Duplicate error will occur.

and also make sure the there is only one CampaignMemberStatus as default.


verify the value of these 2 line of code :

CMSt.SortOrder = integer.valueOf(Mcm.SortOrder__c);

CMSt.IsDefault = Mcm.IsDefault__c ;


Good Luck.




mahesh1.396434879374128E12mahesh1.396434879374128E12
Hi Tony

Their is no Duplicate values in the SortOrder__c and the IsDefault status is given to One CampaignMemberStatus Values.

Any More Solutions for this.
 
Tony TannousTony Tannous
Hello,

You are deleting all the CampaignMemberStatus different to Sent or responded .

scenario 1:did you verified the SortOrder__c value and the IsDefault for the Sent and responded Records.

may be what you are trying to insert from the CustomSettigns is causing the duplication.

Scenario 2 : try to insert them staticly in the class just to test what Value is causing the probleme??

Good Luck.