You need to sign in to do that
Don't have an account?
Kyle Golik
Test Method for Apex Test Class for Code Coverage for Apex Trigger
Trying to deploy this trigger and as we all know there is a prerequisite for Salesforce for Code Coverage on your Trigger. I have created a Test Class for this Trigger and the problem I have is there are no Test Methods, I thought I established a method in the Test Class.
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; } } } }
Try this code and mark this answer as the solution to help others.