i have a Trigger class with metadata and i want to make a test class, but i don't know how i do with custom metadata. The custom metadata is a list that automatically adds member status to each campaign depending on its record type
Who can help me ? I have created a test class but it doesn't work and i know.
TRGHDL_Campaign_Member_Status
public with sharing class TRGHDL_Campaign_Member_Status extends TriggerHandler{
Map<Id, Campaign> mcs = new Map<Id, Campaign>();
Integer campaignStatusEmailOrder = 0;
Map<Id, Id> recordTypeIdbyCampaignId = new Map<Id, Id>();
List <CampaignMemberStatus> lstMemberStatus = new List<CampaignMemberStatus>();
Id recordTypeEmail;
Id recordTypeMarketing;
public override void bulkAfter(Sobject[] newSoList,Map<Id,SObject> oldSoMap){
recordTypeMarketing = Schema.SObjectType.Campaign.getRecordTypeInfosByDeveloperName().get('AM_Campagnes_evenements_marketing').getRecordTypeId();
recordTypeEmail = Schema.SObjectType.Campaign.getRecordTypeInfosByDeveloperName().get('AM_Campagne_mass_email').getRecordTypeId();
if(Trigger.isInsert){
Set<Id> campaignIds = new Set<Id>();
for(SObject so : newSoList){
if (so.get('Id') != null){
campaignIds.add((Id)so.get('Id'));
recordTypeIdbyCampaignId.put((Id)so.get('Id') , (Id)so.get('RecordTypeID'));
}
}
System.debug(recordTypeIdbyCampaignId);
}
}
public override void afterInsert(SObject so){
if (recordTypeIdbyCampaignId.get((Id)so.get('Id')) == ''){
lstMemberStatus.addAll(getListMemberStatusMarketing(so.Id));
}
if (recordTypeIdbyCampaignId.get((Id)so.get('Id')) == ''){
lstMemberStatus.addAll(getListMemberStatusEmail(so.Id));
}
}
public static List<CampaignMemberStatus> getListMemberStatusMarketing (Id campagneId){
List<Statut_de_campagne_marketing__mdt> mcs = Statut_de_campagne_marketing__mdt.getAll().values();
List<CampaignMemberStatus> cms = new list<CampaignMemberStatus>();
for (Statut_de_campagne_marketing__mdt mc : mcs){
CampaignMemberStatus ms = new CampaignMemberStatus();
ms.CampaignId = campagneId;
ms.Label = mc.label;
ms.SortOrder = (Integer)mc.SortOrder__c;
cms.add(ms);
}
return cms;
}
public static List<CampaignMemberStatus> getListMemberStatusEmail (Id campagneId) {
List<Statut_de_campagne_email__mdt> mcs = Statut_de_campagne_email__mdt.getAll().values();
List<CampaignMemberStatus> cms = new list<CampaignMemberStatus>();
for (Statut_de_campagne_email__mdt mc : mcs){
CampaignMemberStatus ms = new CampaignMemberStatus();
ms.CampaignId = campagneId;
ms.Label = mc.Label;
ms.SortOrder = (Integer)mc.SortOrder__c;
cms.add(ms);
}
return cms;
}
}
TRGHDL_Campaign_Member_Status_TEST
@isTest
public with sharing class TRGHDL_Campaign_Member_Status_TEST {
private static String TU_PREFIX = 'TRGHDL_Campaign_Member_Status';
@TestSetup
static void makeCampaign() {
id recordTypeMarketing = Schema.SObjectType.Campaign.getRecordTypeInfosByDeveloperName().get('Campagnes_evenements_marketing').getRecordTypeId();
id recordTypeEmail = Schema.SObjectType.Campaign.getRecordTypeInfosByDeveloperName().get('Campagne_mass_email').getRecordTypeId();
Campaign testcampagne1 = DataFactory_TEST.genCampaign(TU_PREFIX + 'testcampagne1', null);
testcampagne1.RecordTypeId = recordTypeMarketing;
Campaign testcampagne2 = DataFactory_TEST.genCampaign(TU_PREFIX + 'testcampagne2', null);
testcampagne2.RecordTypeId = recordTypeEmail;
insert new List <Campaign> {testcampagne1, testcampagne2};
}
@isTest
static void TEST_TRGHDL_Campaign_Member_Status_Marketing (){
Campaign testcampagne1 = [SELECT Name, Id FROM Campaign WHERE Name =:TU_PREFIX + 'testcampagne1'];
List <CampaignMemberStatus> lstMemberStatus = new List<CampaignMemberStatus>();
List <Campaign> lstCampaign = new List <Campaign> ();
Campaign testCampaign = new Campaign();
testCampaign.Name = 'nouveau';
insert testCampaign;
lstCampaign.add(testCampaign);
}
}