You need to sign in to do that
Don't have an account?
HTANIRS
Trigger test class code coverage for custom metadata loop
Hi Experts,
Need help in code coverage for Trigger Test Class.
Test Class not covering in Custom Metadata part which was mentioned in Bold.
Kindly suggest the changes need to be done.
Thanks.
Need help in code coverage for Trigger Test Class.
Test Class not covering in Custom Metadata part which was mentioned in Bold.
Trigger: trigger updateMembershipPrice on OpportunityLineItem (after insert) { Set<Id> oppsIds = new Set<Id>(); List<OpportunityLineItem> oliList = new List<OpportunityLineItem>(); List<OpportunityLineItem> updateOliList = new List<OpportunityLineItem>(); Map<String, String> mapOpp = new Map<String, String>(); for(OpportunityLineItem oli : Trigger.new) { oppsIds.add(oli.OpportunityId); System.debug('--- Opps Id ---' + oli.OpportunityId); } Map<String, Decimal> mapcustomProRate = new Map<String, Decimal>(); for(MembershipPrice__mdt customMetadata : [SELECT Membership_Type__c, Month__c, Pro_Rate_Price__c FROM MembershipPrice__mdt]) { mapcustomProRate.put(customMetadata.Month__c +'' + customMetadata.Membership_Type__c, customMetadata.Pro_Rate_Price__c); } Map<Id, Date> mapDateApproved = new Map<Id, Date>(); for(OpportunityLineItem olit : [SELECT Id, OpportunityId, Opportunity.Date_Approved__c, Product2.Name FROM OpportunityLineItem WHERE OpportunityId IN: oppsIds]) { if(olit.Opportunity.Date_Approved__c != NULL && olit.Product2Id != NULL) { Datetime dt = (Datetime)olit.Opportunity.Date_Approved__c; String monthname = dt.format('MMMM'); if(mapcustomProRate.containsKey(monthname +''+ olit.Product2.Name)) { OpportunityLineItem oli = new OpportunityLineItem(); oli.Id = olit.Id; oli.UnitPrice = mapcustomProRate.get(monthname +''+ olit.Product2.Name); updateOliList.add(oli); } } } if(updateOliList.size() > 0) { update updateOliList; } }
Test Class: @isTest private class updateMembershipPrice_Test { static testMethod void updateMembershipPrice_Test () { //create Account Account acc = new Account(Name = 'Test Account'); insert acc; //create Contact Contact con = new Contact(LastName = 'Test', AccountId = acc.Id, FirstName = 'Contact', Email = 'Test@sf.com', MailingStreet = '123', OtherStreet = '213'); insert con; //get standard pricebook Id pricebookId = Test.getStandardPricebookId(); //add product Product2 prd1 = new Product2 (Name='Test Product Entry 1',Description='Test Product Entry 1',productCode = 'ABC', isActive = true); insert prd1; //add pricebookentry for standard pricebook PricebookEntry pbe1 = new PricebookEntry (Product2ID = prd1.id, Pricebook2ID = pricebookId, UnitPrice = 50, isActive = true); insert pbe1; //add opportunity Opportunity opp = new Opportunity (Name = 'Opp', Date_Approved__c = Date.today(), StageName = 'Qualification', CloseDate = Date.today(), Pricebook2Id = pbe1.Pricebook2Id, AccountId = acc.id); insert opp; //add opportunity line item OpportunityLineItem lineItem = new OpportunityLineItem (OpportunityID = opp.id, PriceBookEntryID = pbe1.id, quantity = 1, totalprice = 200); insert lineItem; lineItem.quantity = 2; lineItem.UnitPrice = 400; update lineItem; } }
Kindly suggest the changes need to be done.
Thanks.
https://www.forcetalks.com/blog/custom-metadata-test-class-coverage-in-salesforce/
https://developer.salesforce.com/blogs/engineering/2015/05/testing-custom-metadata-types.html
https://salesforce.stackexchange.com/questions/111375/insert-custom-metadata-for-unit-test
let me know if you find this post helpful
Thanks,
Anudeep