As the title indicates, trying to deploy this trigger and need to get the Code Coverage prerequisite satisfied.
As I created this Test Class I have no methods, thinking I at least established some methods to test against.
Apex Test Class
@isTest
private class UpdateOpportunityProductMonthsToFulfill{
@testSetup
static void testOLIUpdate()
{
// Obtain Apex governor limits and resources for this test
Test.startTest();
OpportunityLineItem oli = new OpportunityLineItem(Months_to_Fulfill__c='');
insert oli;
QuoteLineItem qli = new QuoteLineItem(OpportunityLineItemId=oli.Id);
insert qli;
qli.Year_Served_First__c='2018';
qli.Months_Served_Y1__c='May;June';
update qli;
system.assertEquals(oli.Months_to_Fulfill__c, 'May 2018; June 2018');
// Release governor limits and resources
Test.stopTest();
}
}
Apex Trigger
trigger UpdateOpportunityProductMonthsToFulfill on QuoteLineItem (after insert, after update) {
String monthsToFulfill = '';
map<String, String> updateMap = new map<String, String>();
// This is where the text for the Months_To_Fulfill__c field is built
for(QuoteLineItem qli : Trigger.new){
// To make a spelled-out list of months & years instead of the older "start through end"
List<String> yearOneMonths = qli.Months_Served_Y1__c.replaceAll('None(;)?', '').split(';');
for(String month : yearOneMonths){
monthsToFulfill += ( monthsToFulfill.length()==0 ? '' : '; ' ) + month + ' ' + qli.Year_Served_First__c;
}
if(!(qli.Year_Served_Second__c == null || qli.Year_Served_Second__c.equals('None'))){
List<String> yearTwoMonths = qli.Months_Served_Y2__c.replaceAll('None(;)?', '').split(';');
for(String month : yearTwoMonths){
monthsToFulfill += '; ' + month + ' ' + qli.Year_Served_Second__c;
}
}
updateMap.put(qli.OpportunityLineItemId, monthsToFulfill);
}
for(String oliID : updateMap.keySet()){
// This is the calculated value we saved from looping through the Trigger.new Collection
monthsToFulfill = updateMap.get(oliID);
// Get the OpportunityLineItems associated with this QuoteId
List<OpportunityLineItem> oliList = [Select Id, OpportunityId, Months_To_Fulfill__c
From OpportunityLineItem Where Id = :oliID ];
List<OpportunityLineItem> olisToUpdate = new List<OpportunityLineItem>();
if(oliList.size() > 0){
for (OpportunityLineItem oli : oliList){
// Populate this olisToUpdate list because we may
// want to get smarter about what's actually updated
// in the future. For now, everything is updated.
oli.Months_To_Fulfill__c = monthsToFulfill;
olisToUpdate.add(oli);
}
// Batch update the OpportunityLineItems we identified earlier
if(olisToUpdate.size() > 0){
update olisToUpdate;
}
}
}
}