You need to sign in to do that
Don't have an account?

how to write Test class for map collection object
Hi All,
I need to write Test Method for the code which is iteration collection object allSubs(means allSubscriptions) in BOLD.
I am able to write Test code for getConForProduct method but stuck with the later part.
Please help me!!
Thanks in Advance.
Apex Class:
Global class getContactsMatrixOrders {
public static List<Map<String, Id>> getConForProduct(Map<String, Id> allProducts){
// This method will return contacts who are eligible for C031 & C041 Testing done
List<Map<String, Id>> conAndPro = new List<Map<String, Id>>();
List<OrderApi__Subscription__c> allSubs = [SELECT id , OrderApi__Contact__r.id , OrderAPi__Contact__r.AccountId,OrderApi__Contact__r.apfy_products_received__c
, OrderApi__Subscription_Plan__r.Name,OrderApi__Item__r.Name
FROM OrderApi__Subscription__c
WHERE OrderApi__Status__c = 'Active'
AND OrderApi__Contact__r.CUSTOMER_CLASS__c = 'Member'
AND OrderApi__Contact__r.Customer_Status__c = 'Active'
AND OrderApi__Contact__r.DonorApi__Spouse__r.CUSTOMER_CLASS__c = 'Member'
AND OrderApi__Contact__r.DonorApi__Spouse__r.Customer_Status__c = 'DECEASED'
AND OrderApi__Sales_Order_Line__r.OrderApi__Sales_Order__r.OrderApi__Source_Code__r.Name = 'WIDOW'];
System.debug('allSubs'+allSubs);
for(OrderApi__Subscription__c sub : allSubs){
String itemName = sub.OrderApi__Item__r.Name;
Id conId = sub.OrderApi__Contact__r.id;
Id accId = sub.OrderAPi__Contact__r.AccountId;
String proReceived = sub.OrderApi__Contact__r.apfy_products_received__c;
List<String> productsReceived = new List<String>();
if(proReceived!=null){
productsReceived = proReceived.split(',');
}
Map<String,Id> singleCon = new Map<String, Id>();
if((itemName == 'BASIC Membership for Surviving Spouse' || itemName == 'PREMIUM Membership for Surviving Spouse') && !productsReceived.contains('C031')){
singleCon.put('conId',conId);
singleCon.put('accId',accId);
singleCon.put('proId',allProducts.get('C031'));
conAndPro.add(singleCon);
}else if(itemName == 'LIFE Membership for Surviving Spouse' && !productsReceived.contains('C041')){
singleCon.put('conId',conId);
singleCon.put('accId',accId);
singleCon.put('proId',allProducts.get('C041'));
conAndPro.add(singleCon);
}
}
return conAndPro;
}
I need to write Test Method for the code which is iteration collection object allSubs(means allSubscriptions) in BOLD.
I am able to write Test code for getConForProduct method but stuck with the later part.
Please help me!!
Thanks in Advance.
Apex Class:
Global class getContactsMatrixOrders {
public static List<Map<String, Id>> getConForProduct(Map<String, Id> allProducts){
// This method will return contacts who are eligible for C031 & C041 Testing done
List<Map<String, Id>> conAndPro = new List<Map<String, Id>>();
List<OrderApi__Subscription__c> allSubs = [SELECT id , OrderApi__Contact__r.id , OrderAPi__Contact__r.AccountId,OrderApi__Contact__r.apfy_products_received__c
, OrderApi__Subscription_Plan__r.Name,OrderApi__Item__r.Name
FROM OrderApi__Subscription__c
WHERE OrderApi__Status__c = 'Active'
AND OrderApi__Contact__r.CUSTOMER_CLASS__c = 'Member'
AND OrderApi__Contact__r.Customer_Status__c = 'Active'
AND OrderApi__Contact__r.DonorApi__Spouse__r.CUSTOMER_CLASS__c = 'Member'
AND OrderApi__Contact__r.DonorApi__Spouse__r.Customer_Status__c = 'DECEASED'
AND OrderApi__Sales_Order_Line__r.OrderApi__Sales_Order__r.OrderApi__Source_Code__r.Name = 'WIDOW'];
System.debug('allSubs'+allSubs);
for(OrderApi__Subscription__c sub : allSubs){
String itemName = sub.OrderApi__Item__r.Name;
Id conId = sub.OrderApi__Contact__r.id;
Id accId = sub.OrderAPi__Contact__r.AccountId;
String proReceived = sub.OrderApi__Contact__r.apfy_products_received__c;
List<String> productsReceived = new List<String>();
if(proReceived!=null){
productsReceived = proReceived.split(',');
}
Map<String,Id> singleCon = new Map<String, Id>();
if((itemName == 'BASIC Membership for Surviving Spouse' || itemName == 'PREMIUM Membership for Surviving Spouse') && !productsReceived.contains('C031')){
singleCon.put('conId',conId);
singleCon.put('accId',accId);
singleCon.put('proId',allProducts.get('C031'));
conAndPro.add(singleCon);
}else if(itemName == 'LIFE Membership for Surviving Spouse' && !productsReceived.contains('C041')){
singleCon.put('conId',conId);
singleCon.put('accId',accId);
singleCon.put('proId',allProducts.get('C041'));
conAndPro.add(singleCon);
}
}
return conAndPro;
}
Hi,
Do some changes according to your code. I have written almost 90% of your test class.
Please mark it as the Best Answer.
Thank You
All Answers
Hi,
Do some changes according to your code. I have written almost 90% of your test class.
Please mark it as the Best Answer.
Thank You