You need to sign in to do that
Don't have an account?
Jab SFDC
Help to create test for after update trigger
Help to create a test for this trigger:
Trigger :
trigger OpportunityTrigger on Opportunity (after update) {
if(trigger.isAfter){
List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();
List<OpportunityLineItem> oliListToRemove = new List<OpportunityLineItem>();
Map<String,PriceBookEntry> priceBookMap = new Map<String,PriceBookEntry>();
Set<Id> oppId = new Set<Id>();
Set<String> revenueProductSet = new Set<String>();
for(Revenue_Schedule_PriceBook__mdt revProd: [SELECT MasterLabel,NamespacePrefix from Revenue_Schedule_PriceBook__mdt]){
revenueProductSet.add(revProd.MasterLabel);
}
for(PriceBookEntry pbEntry : [SELECT Id, Product2Id, Product2.Id, Product2.Name,Pricebook2Id,Pricebook2.Name,UnitPrice FROM PriceBookEntry Where Pricebook2.Name IN: revenueProductSet]){
priceBookMap.put(pbEntry.Product2.Name,pbEntry);
}
for(Opportunity opp: Trigger.new){
if((trigger.isInsert)|| (trigger.isUpdate && (trigger.oldMap.get(opp.Id).Launch_Date__c != opp.Launch_Date__c
|| trigger.oldMap.get(opp.Id).Segment__c != opp.Segment__c || trigger.oldMap.get(opp.Id).Ramp_Up_Period_Months__c != opp.Ramp_Up_Period_Months__c
|| trigger.oldMap.get(opp.Id).Length_of_Contract__c != opp.Length_of_Contract__c
|| trigger.oldMap.get(opp.Id).Estimated_Amount__c != opp.Estimated_Amount__c
|| trigger.oldMap.get(opp.Id).Invoice_Frequency__c != opp.Invoice_Frequency__c))){
if(OpportunityTriggerHelper.check){
if(priceBookMap.containsKey(opp.Segment__c)){
OpportunityLineItem oli = new OpportunityLineItem();
oli.OpportunityId=opp.Id;
oli.Quantity = 1;
oli.PricebookEntryId= priceBookMap.get(opp.Segment__c).Id;
oli.TotalPrice = opp.Estimated_Amount__c * oli.Quantity;
//oli.TotalPrice = oli.Quantity;
oppId.add(opp.Id);
oliList.add(oli);
}
}
}
}
for(OpportunityLineItem oplTemp :[Select Id from OpportunityLineItem where OpportunityId IN: oppId]){
oliListToRemove.add(oplTemp);
}
if(oliListToRemove.size() > 0){
delete oliListToRemove;
}
if(oliList.size() > 0){
OpportunityTriggerHelper.check = false;
Insert oliList;
List<OpportunityLineItemSchedule> oppLineItemSchlist = new List<OpportunityLineItemSchedule>();
List<OpportunityLineItem> updateOppLineItemList = new List<OpportunityLineItem>();
Date currentDate = null;
for(OpportunityLineItem oppLineItem:[Select Id,Name,Invoice_Frequency__c,Opportunity.Invoice_Frequency__c,Opportunity.Estimated_Amount__c,
Length_of_Contract__c,Peak_Monthly_Revenue__c,Launch_Date__c,Yearly_Amount__c,Ramp_Up_Period__c from OpportunityLineItem
where Id =:oliList[0].Id]){
currentDate = oppLineItem.Launch_Date__c.addMonths(1);
Decimal monthlyAmount = oppLineItem.Peak_Monthly_Revenue__c;
Decimal yearlyAmount = 0.00;
Date orgFiscalYearEnd = Date.newinstance(oppLineItem.Launch_Date__c.year(), 10, 31);
Date orgFiscalYearStart = Date.newinstance(oppLineItem.Launch_Date__c.addyears(-1).year(), 11, 1);
if(oppLineItem.Invoice_Frequency__c == 'Monthly'){
for (Integer i = 1;i <= Integer.valueof(oppLineItem.Length_of_Contract__c);i++) {
OpportunityLineItemSchedule s = new OpportunityLineItemSchedule();
s.ScheduleDate = currentDate;
s.OpportunityLineItemId = oppLineItem.id;
s.Type = 'Revenue';
if(Integer.ValueOf(oppLineItem.Ramp_Up_Period__c) == 1){
if(i==1){
Integer tempValue = OpportunityTriggerHelper.percentageCalculator(monthlyAmount,50);
s.Revenue = tempValue;
}
else{
s.Revenue = monthlyAmount;
}
}
else if(Integer.ValueOf(oppLineItem.Ramp_Up_Period__c) == 2){
if(i==1){
Integer tempValue = OpportunityTriggerHelper.percentageCalculator(monthlyAmount,30);
s.Revenue = tempValue;
}
else if(i==2){
Integer tempValue = OpportunityTriggerHelper.percentageCalculator(monthlyAmount,70);
s.Revenue = tempValue;
}
else{
s.Revenue = monthlyAmount;
}
}
else if(Integer.ValueOf(oppLineItem.Ramp_Up_Period__c) == 3){
if(i==1){
Integer tempValue = OpportunityTriggerHelper.percentageCalculator(monthlyAmount,25);
s.Revenue = tempValue;
}
else if(i==2){
Integer tempValue = OpportunityTriggerHelper.percentageCalculator(monthlyAmount,50);
s.Revenue = tempValue;
}
else if(i==3){
Integer tempValue = OpportunityTriggerHelper.percentageCalculator(monthlyAmount,75);
s.Revenue = tempValue;
}
else{
s.Revenue = monthlyAmount;
}
}
else if(Integer.ValueOf(oppLineItem.Ramp_Up_Period__c) == 4){
if(i==1){
Integer tempValue = OpportunityTriggerHelper.percentageCalculator(monthlyAmount,20);
s.Revenue = tempValue;
}
else if(i==2){
Integer tempValue = OpportunityTriggerHelper.percentageCalculator(monthlyAmount,40);
s.Revenue = tempValue;
}
else if(i==3){
Integer tempValue = OpportunityTriggerHelper.percentageCalculator(monthlyAmount,60);
s.Revenue = tempValue;
}
else if(i==4){
Integer tempValue = OpportunityTriggerHelper.percentageCalculator(monthlyAmount,80);
s.Revenue = tempValue;
}
else{
s.Revenue = monthlyAmount;
}
}
else if(Integer.ValueOf(oppLineItem.Ramp_Up_Period__c) == 5){
if(i==1){
Integer tempValue = OpportunityTriggerHelper.percentageCalculator(monthlyAmount,15);
s.Revenue = tempValue;
}
else if(i==2){
Integer tempValue = OpportunityTriggerHelper.percentageCalculator(monthlyAmount,30);
s.Revenue = tempValue;
}
else if(i==3){
Integer tempValue = OpportunityTriggerHelper.percentageCalculator(monthlyAmount,45);
s.Revenue = tempValue;
}
else if(i==4){
Integer tempValue = OpportunityTriggerHelper.percentageCalculator(monthlyAmount,60);
s.Revenue = tempValue;
}
else if(i==5){
Integer tempValue = OpportunityTriggerHelper.percentageCalculator(monthlyAmount,75);
s.Revenue = tempValue;
}
else{
s.Revenue = monthlyAmount;
}
}
else if(Integer.ValueOf(oppLineItem.Ramp_Up_Period__c) == 6){
if(i==1){
Integer tempValue = OpportunityTriggerHelper.percentageCalculator(monthlyAmount,15);
s.Revenue = tempValue;
}
else if(i==2){
Integer tempValue = OpportunityTriggerHelper.percentageCalculator(monthlyAmount,30);
s.Revenue = tempValue;
}
else if(i==3){
Integer tempValue = OpportunityTriggerHelper.percentageCalculator(monthlyAmount,45);
s.Revenue = tempValue;
}
else if(i==4){
Integer tempValue = OpportunityTriggerHelper.percentageCalculator(monthlyAmount,60);
s.Revenue = tempValue;
}
else if(i==5){
Integer tempValue = OpportunityTriggerHelper.percentageCalculator(monthlyAmount,75);
s.Revenue = tempValue;
}
else if(i==6){
Integer tempValue = OpportunityTriggerHelper.percentageCalculator(monthlyAmount,90);
s.Revenue = tempValue;
}
else{
s.Revenue = monthlyAmount;
}
}
else{
s.Revenue = monthlyAmount;
}
oppLineItemSchlist.add(s);
if(currentDate <= orgFiscalYearEnd && currentDate >= orgFiscalYearStart)
yearlyAmount = yearlyAmount + s.Revenue;
currentDate = currentDate.addMonths(1);
}
}
else{
OpportunityLineItemSchedule s = new OpportunityLineItemSchedule();
s.ScheduleDate = currentDate;
s.OpportunityLineItemId = oppLineItem.id;
s.Type = 'Revenue';
s.Revenue = oppLineItem.Opportunity.Estimated_Amount__c;
oppLineItemSchlist.add(s);
if(currentDate <= orgFiscalYearEnd && currentDate >= orgFiscalYearStart)
yearlyAmount = s.Revenue;
}
OpportunityLineItem updateOLI = new OpportunityLineItem(Id =oliList[0].Id,Yearly_Amount__c = yearlyAmount);
updateOppLineItemList.add(updateOLI);
if(updateOppLineItemList.size() > 0)
update updateOppLineItemList;
}
if(oppLineItemSchlist.size() > 0){
Insert oppLineItemSchlist;
}
}
}
}
Trigger :
trigger OpportunityTrigger on Opportunity (after update) {
if(trigger.isAfter){
List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();
List<OpportunityLineItem> oliListToRemove = new List<OpportunityLineItem>();
Map<String,PriceBookEntry> priceBookMap = new Map<String,PriceBookEntry>();
Set<Id> oppId = new Set<Id>();
Set<String> revenueProductSet = new Set<String>();
for(Revenue_Schedule_PriceBook__mdt revProd: [SELECT MasterLabel,NamespacePrefix from Revenue_Schedule_PriceBook__mdt]){
revenueProductSet.add(revProd.MasterLabel);
}
for(PriceBookEntry pbEntry : [SELECT Id, Product2Id, Product2.Id, Product2.Name,Pricebook2Id,Pricebook2.Name,UnitPrice FROM PriceBookEntry Where Pricebook2.Name IN: revenueProductSet]){
priceBookMap.put(pbEntry.Product2.Name,pbEntry);
}
for(Opportunity opp: Trigger.new){
if((trigger.isInsert)|| (trigger.isUpdate && (trigger.oldMap.get(opp.Id).Launch_Date__c != opp.Launch_Date__c
|| trigger.oldMap.get(opp.Id).Segment__c != opp.Segment__c || trigger.oldMap.get(opp.Id).Ramp_Up_Period_Months__c != opp.Ramp_Up_Period_Months__c
|| trigger.oldMap.get(opp.Id).Length_of_Contract__c != opp.Length_of_Contract__c
|| trigger.oldMap.get(opp.Id).Estimated_Amount__c != opp.Estimated_Amount__c
|| trigger.oldMap.get(opp.Id).Invoice_Frequency__c != opp.Invoice_Frequency__c))){
if(OpportunityTriggerHelper.check){
if(priceBookMap.containsKey(opp.Segment__c)){
OpportunityLineItem oli = new OpportunityLineItem();
oli.OpportunityId=opp.Id;
oli.Quantity = 1;
oli.PricebookEntryId= priceBookMap.get(opp.Segment__c).Id;
oli.TotalPrice = opp.Estimated_Amount__c * oli.Quantity;
//oli.TotalPrice = oli.Quantity;
oppId.add(opp.Id);
oliList.add(oli);
}
}
}
}
for(OpportunityLineItem oplTemp :[Select Id from OpportunityLineItem where OpportunityId IN: oppId]){
oliListToRemove.add(oplTemp);
}
if(oliListToRemove.size() > 0){
delete oliListToRemove;
}
if(oliList.size() > 0){
OpportunityTriggerHelper.check = false;
Insert oliList;
List<OpportunityLineItemSchedule> oppLineItemSchlist = new List<OpportunityLineItemSchedule>();
List<OpportunityLineItem> updateOppLineItemList = new List<OpportunityLineItem>();
Date currentDate = null;
for(OpportunityLineItem oppLineItem:[Select Id,Name,Invoice_Frequency__c,Opportunity.Invoice_Frequency__c,Opportunity.Estimated_Amount__c,
Length_of_Contract__c,Peak_Monthly_Revenue__c,Launch_Date__c,Yearly_Amount__c,Ramp_Up_Period__c from OpportunityLineItem
where Id =:oliList[0].Id]){
currentDate = oppLineItem.Launch_Date__c.addMonths(1);
Decimal monthlyAmount = oppLineItem.Peak_Monthly_Revenue__c;
Decimal yearlyAmount = 0.00;
Date orgFiscalYearEnd = Date.newinstance(oppLineItem.Launch_Date__c.year(), 10, 31);
Date orgFiscalYearStart = Date.newinstance(oppLineItem.Launch_Date__c.addyears(-1).year(), 11, 1);
if(oppLineItem.Invoice_Frequency__c == 'Monthly'){
for (Integer i = 1;i <= Integer.valueof(oppLineItem.Length_of_Contract__c);i++) {
OpportunityLineItemSchedule s = new OpportunityLineItemSchedule();
s.ScheduleDate = currentDate;
s.OpportunityLineItemId = oppLineItem.id;
s.Type = 'Revenue';
if(Integer.ValueOf(oppLineItem.Ramp_Up_Period__c) == 1){
if(i==1){
Integer tempValue = OpportunityTriggerHelper.percentageCalculator(monthlyAmount,50);
s.Revenue = tempValue;
}
else{
s.Revenue = monthlyAmount;
}
}
else if(Integer.ValueOf(oppLineItem.Ramp_Up_Period__c) == 2){
if(i==1){
Integer tempValue = OpportunityTriggerHelper.percentageCalculator(monthlyAmount,30);
s.Revenue = tempValue;
}
else if(i==2){
Integer tempValue = OpportunityTriggerHelper.percentageCalculator(monthlyAmount,70);
s.Revenue = tempValue;
}
else{
s.Revenue = monthlyAmount;
}
}
else if(Integer.ValueOf(oppLineItem.Ramp_Up_Period__c) == 3){
if(i==1){
Integer tempValue = OpportunityTriggerHelper.percentageCalculator(monthlyAmount,25);
s.Revenue = tempValue;
}
else if(i==2){
Integer tempValue = OpportunityTriggerHelper.percentageCalculator(monthlyAmount,50);
s.Revenue = tempValue;
}
else if(i==3){
Integer tempValue = OpportunityTriggerHelper.percentageCalculator(monthlyAmount,75);
s.Revenue = tempValue;
}
else{
s.Revenue = monthlyAmount;
}
}
else if(Integer.ValueOf(oppLineItem.Ramp_Up_Period__c) == 4){
if(i==1){
Integer tempValue = OpportunityTriggerHelper.percentageCalculator(monthlyAmount,20);
s.Revenue = tempValue;
}
else if(i==2){
Integer tempValue = OpportunityTriggerHelper.percentageCalculator(monthlyAmount,40);
s.Revenue = tempValue;
}
else if(i==3){
Integer tempValue = OpportunityTriggerHelper.percentageCalculator(monthlyAmount,60);
s.Revenue = tempValue;
}
else if(i==4){
Integer tempValue = OpportunityTriggerHelper.percentageCalculator(monthlyAmount,80);
s.Revenue = tempValue;
}
else{
s.Revenue = monthlyAmount;
}
}
else if(Integer.ValueOf(oppLineItem.Ramp_Up_Period__c) == 5){
if(i==1){
Integer tempValue = OpportunityTriggerHelper.percentageCalculator(monthlyAmount,15);
s.Revenue = tempValue;
}
else if(i==2){
Integer tempValue = OpportunityTriggerHelper.percentageCalculator(monthlyAmount,30);
s.Revenue = tempValue;
}
else if(i==3){
Integer tempValue = OpportunityTriggerHelper.percentageCalculator(monthlyAmount,45);
s.Revenue = tempValue;
}
else if(i==4){
Integer tempValue = OpportunityTriggerHelper.percentageCalculator(monthlyAmount,60);
s.Revenue = tempValue;
}
else if(i==5){
Integer tempValue = OpportunityTriggerHelper.percentageCalculator(monthlyAmount,75);
s.Revenue = tempValue;
}
else{
s.Revenue = monthlyAmount;
}
}
else if(Integer.ValueOf(oppLineItem.Ramp_Up_Period__c) == 6){
if(i==1){
Integer tempValue = OpportunityTriggerHelper.percentageCalculator(monthlyAmount,15);
s.Revenue = tempValue;
}
else if(i==2){
Integer tempValue = OpportunityTriggerHelper.percentageCalculator(monthlyAmount,30);
s.Revenue = tempValue;
}
else if(i==3){
Integer tempValue = OpportunityTriggerHelper.percentageCalculator(monthlyAmount,45);
s.Revenue = tempValue;
}
else if(i==4){
Integer tempValue = OpportunityTriggerHelper.percentageCalculator(monthlyAmount,60);
s.Revenue = tempValue;
}
else if(i==5){
Integer tempValue = OpportunityTriggerHelper.percentageCalculator(monthlyAmount,75);
s.Revenue = tempValue;
}
else if(i==6){
Integer tempValue = OpportunityTriggerHelper.percentageCalculator(monthlyAmount,90);
s.Revenue = tempValue;
}
else{
s.Revenue = monthlyAmount;
}
}
else{
s.Revenue = monthlyAmount;
}
oppLineItemSchlist.add(s);
if(currentDate <= orgFiscalYearEnd && currentDate >= orgFiscalYearStart)
yearlyAmount = yearlyAmount + s.Revenue;
currentDate = currentDate.addMonths(1);
}
}
else{
OpportunityLineItemSchedule s = new OpportunityLineItemSchedule();
s.ScheduleDate = currentDate;
s.OpportunityLineItemId = oppLineItem.id;
s.Type = 'Revenue';
s.Revenue = oppLineItem.Opportunity.Estimated_Amount__c;
oppLineItemSchlist.add(s);
if(currentDate <= orgFiscalYearEnd && currentDate >= orgFiscalYearStart)
yearlyAmount = s.Revenue;
}
OpportunityLineItem updateOLI = new OpportunityLineItem(Id =oliList[0].Id,Yearly_Amount__c = yearlyAmount);
updateOppLineItemList.add(updateOLI);
if(updateOppLineItemList.size() > 0)
update updateOppLineItemList;
}
if(oppLineItemSchlist.size() > 0){
Insert oppLineItemSchlist;
}
}
}
}
poke sniper
tweakbox (https://www.trytutuapp.com) or solarmovie (https://www.movieboxstreamingtv.com) can even be on tutuapp (https://www.trytutuapp.com)