You need to sign in to do that
Don't have an account?
SFDCIronMan
Help Create Test Class for this Apex Handler Class
public class Opportunity_Clone_Handler
{
public static void AfterUpdateCloneOpportunity(List<Opportunity> newReacords, Map<Id, Opportunity> oldRecords)
{
//For List of Clone Opportunity
List<Opportunity> oppsToInsert = new List<Opportunity>();
//For Mapping IDs from Opportunity to OpportunityLineItem
Map<Id,List<OpportunityLineItem>> oplitemsMap = new Map<Id,List<OpportunityLineItem>>();
//For Fetching Inserted Or Old list To Clone
List<OpportunityLineItem> oppitemlist = new List<OpportunityLineItem>();
//For Fetching Inserted IDs Or Old IDs To Clone Opportunity
Set<Id> OldID = new Set<Id>();
//For Fetching Newly Inserted Ids for OpportunityLineitem
Set<Id> parentOppIds = new set<Id>();
Set<Id> OldOppIds = new Set<Id>();
Set<Id> ChildOppIds = new Set<Id>();
//For Preventing Recursion
if(OpportunityRecursive_Trigger_Handler.isFirstTime)
{
//Resursion Handler Class
OpportunityRecursive_Trigger_Handler.isFirstTime = false;
//Fetching Insert and Old Opportunit Ids
for(opportunity opp: newReacords)
{
OldID.add(opp.id);
}//End oF 'for'
//Checking the ids
List<Opportunity> ListOpp = [Select id, Name, AccountId, Product_Type__c, CloseDate, StageName, Payment_Frequency__c, Most_Recent_Invoice_Date__c, Billing_Start_Date__c, Interval__c, Parent_Opportunity__c, Create_Child_Opportunities_Invoice__c, Cancelation_Date__c, Service_End_Date__c from Opportunity where id IN:OldID];
// List<Opportunity> ListOpp = [Select id, Name, AccountId, CloseDate, StageName from Opportunity where id IN:SetID];
//for Updated new Opportunity
for(Opportunity parentopp : ListOpp)
{
//checking Old ids checkbox
if(oldRecords.get(parentopp.Id).Create_Child_Opportunities_Invoice__c != parentopp.Create_Child_Opportunities_Invoice__c && parentopp.Create_Child_Opportunities_Invoice__c && parentopp.StageName == 'Closed Won' && parentopp.Product_Type__c == 'Subscription' && parentopp.Payment_Frequency__c != Null && parentopp.Interval__c != Null)
{
Integer Interval = Integer.valueOf(parentopp.Interval__c);
Integer Count;
Date closedate;
For(Count=1; Count<=Interval; Count++)
{
parentOppIds.add(parentopp.Id);
Opportunity childopp = new Opportunity();
childopp.Name = parentopp.Name +'_'+childopp.CloseDate;
childopp.AccountId = parentopp.AccountId;
childopp.Type = 'Invoice';
childopp.Product_Type__c = parentopp.Product_Type__c;
If(parentopp.Billing_Start_Date__c != Null && Count == 1)
{
If(parentopp.Payment_Frequency__c == 'Monthly')
{
childopp.CloseDate = parentopp.Billing_Start_Date__c.toStartOfMonth();
closeDate = childopp.CloseDate;
}
else
If(parentopp.Payment_Frequency__c == 'Quarterly')
{
childopp.CloseDate = parentopp.Billing_Start_Date__c.toStartOfMonth();
closeDate = childopp.CloseDate;
}
else
If(parentopp.Payment_Frequency__c == 'Half Yearly')
{
childopp.CloseDate = parentopp.Billing_Start_Date__c.toStartOfMonth();
closeDate = childopp.CloseDate;
}
else
{
childopp.CloseDate = parentopp.CloseDate.toStartOfMonth();
closeDate = childopp.CloseDate;
}
}
else if(parentopp.Billing_Start_Date__c != Null && Count != 1)
{
If(parentopp.Payment_Frequency__c == 'Monthly')
{
childopp.CloseDate = closeDate.addMonths(1);
closeDate = childopp.CloseDate.toStartOfMonth();
}
else
If(parentopp.Payment_Frequency__c == 'Quarterly')
{
childopp.CloseDate = closeDate.addMonths(3);
closeDate = childopp.CloseDate.toStartOfMonth();
}
else
If(parentopp.Payment_Frequency__c == 'Half Yearly')
{
childopp.CloseDate = closeDate.addMonths(6);
closeDate = childopp.CloseDate.toStartOfMonth();
}
else
{
childopp.CloseDate = closeDate;
closeDate = childopp.CloseDate.toStartOfMonth();
}
}
childopp.StageName = 'Closed Won';
childopp.Parent_Opportunity__c = parentopp.Id;
oppsToInsert.add(childopp);
}//End Of for
}//End of If Checkbox
}//End of for
//Iterating OpportunityLineitems For Opportunity to update related OpportunityLineItem if Present
for(OpportunityLineitem opl : [SELECT Id, Product2Id, Quantity, UnitPrice, Pricebookentryid, OpportunityId FROM OpportunityLineItem Where OpportunityId IN : parentoppIds])
{
//Mapping Inserted or Old opportunityIds to Clone the related OpportunityLineItem
if(!oplitemsMap.containsKey(opl.OpportunityId))
{
oplitemsMap.put(opl.OpportunityId, new List<OpportunityLineitem>{opl});
}//End of IF Old IDs
else
{
oplitemsMap.get(opl.OpportunityId).add(opl);
}//end of else
}//End Of for iteration oppoopportunityLineItem
//If Opportunity is Not Empty
if(!oppsToInsert.IsEmpty())
{
//Cloning Inserted Or Updated Opportunity
upsert oppsToInsert;
//Iterating Cloned Inserted Or Updated Opportunity for OpportunityLineItem
for(Opportunity op : oppsToInsert)
{
//for inserted Or updated Opportunity if Parent_opportunity__c is equals Opportunity
if(oplitemsMap.containsKey(op.Parent_Opportunity__c))
{
//Iterating OpportunityLineitem From Parent Opportunity For Cloning Its OpportunityLineItem
for(OpportunityLineitem ol : oplitemsMap.get(op.Parent_Opportunity__c))
{
OpportunityLineItem oli = new OpportunityLineitem();
oli.OpportunityId = op.Id;
oli.Product2Id = ol.Product2Id;
oli.Quantity = ol.Quantity;
oli.UnitPrice = ol.UnitPrice;
oli.pricebookentryid=ol.pricebookentryid;
oppitemlist.add(oli);
}//End of for
}//End of If
}//End Of For
//Cloning OpportunityLineItem From Parent Opportunity OpportunityLineItem
if(!oppitemlist.isEmpty())
{
insert oppitemlist;
}//End Of if
}//End of If for "Cloning OpportunityLineItem"
for(Opportunity parentOppty : ListOpp)
{
if(parentOppty.Cancelation_Date__c != Null || parentOppty.Service_End_Date__c != Null)
{
OldOppIds.add(parentOppty.Id);
}
}
for(Opportunity childOppty : [Select Id, CloseDate, Parent_Opportunity__c From Opportunity Where Parent_Opportunity__c IN : OldOppIds])
{
for(Opportunity parentOppty : ListOpp)
{
if(parentOppty.Cancelation_Date__c != Null && childOppty.CloseDate > parentOppty.Cancelation_Date__c || parentOppty.Service_End_Date__c != Null && childOppty.CloseDate > parentOppty.Service_End_Date__c)
{
ChildOppIds.add(childOppty.Id);
}
}
}
List<Opportunity> oppsToDelete = [Select Id, CloseDate, Parent_Opportunity__c From Opportunity Where Id IN : ChildOppIds];
if(!oppsToDelete.IsEmpty())
{
delete oppsToDelete;
}
}//End Of Recursion
}//End of after Update
}
{
public static void AfterUpdateCloneOpportunity(List<Opportunity> newReacords, Map<Id, Opportunity> oldRecords)
{
//For List of Clone Opportunity
List<Opportunity> oppsToInsert = new List<Opportunity>();
//For Mapping IDs from Opportunity to OpportunityLineItem
Map<Id,List<OpportunityLineItem>> oplitemsMap = new Map<Id,List<OpportunityLineItem>>();
//For Fetching Inserted Or Old list To Clone
List<OpportunityLineItem> oppitemlist = new List<OpportunityLineItem>();
//For Fetching Inserted IDs Or Old IDs To Clone Opportunity
Set<Id> OldID = new Set<Id>();
//For Fetching Newly Inserted Ids for OpportunityLineitem
Set<Id> parentOppIds = new set<Id>();
Set<Id> OldOppIds = new Set<Id>();
Set<Id> ChildOppIds = new Set<Id>();
//For Preventing Recursion
if(OpportunityRecursive_Trigger_Handler.isFirstTime)
{
//Resursion Handler Class
OpportunityRecursive_Trigger_Handler.isFirstTime = false;
//Fetching Insert and Old Opportunit Ids
for(opportunity opp: newReacords)
{
OldID.add(opp.id);
}//End oF 'for'
//Checking the ids
List<Opportunity> ListOpp = [Select id, Name, AccountId, Product_Type__c, CloseDate, StageName, Payment_Frequency__c, Most_Recent_Invoice_Date__c, Billing_Start_Date__c, Interval__c, Parent_Opportunity__c, Create_Child_Opportunities_Invoice__c, Cancelation_Date__c, Service_End_Date__c from Opportunity where id IN:OldID];
// List<Opportunity> ListOpp = [Select id, Name, AccountId, CloseDate, StageName from Opportunity where id IN:SetID];
//for Updated new Opportunity
for(Opportunity parentopp : ListOpp)
{
//checking Old ids checkbox
if(oldRecords.get(parentopp.Id).Create_Child_Opportunities_Invoice__c != parentopp.Create_Child_Opportunities_Invoice__c && parentopp.Create_Child_Opportunities_Invoice__c && parentopp.StageName == 'Closed Won' && parentopp.Product_Type__c == 'Subscription' && parentopp.Payment_Frequency__c != Null && parentopp.Interval__c != Null)
{
Integer Interval = Integer.valueOf(parentopp.Interval__c);
Integer Count;
Date closedate;
For(Count=1; Count<=Interval; Count++)
{
parentOppIds.add(parentopp.Id);
Opportunity childopp = new Opportunity();
childopp.Name = parentopp.Name +'_'+childopp.CloseDate;
childopp.AccountId = parentopp.AccountId;
childopp.Type = 'Invoice';
childopp.Product_Type__c = parentopp.Product_Type__c;
If(parentopp.Billing_Start_Date__c != Null && Count == 1)
{
If(parentopp.Payment_Frequency__c == 'Monthly')
{
childopp.CloseDate = parentopp.Billing_Start_Date__c.toStartOfMonth();
closeDate = childopp.CloseDate;
}
else
If(parentopp.Payment_Frequency__c == 'Quarterly')
{
childopp.CloseDate = parentopp.Billing_Start_Date__c.toStartOfMonth();
closeDate = childopp.CloseDate;
}
else
If(parentopp.Payment_Frequency__c == 'Half Yearly')
{
childopp.CloseDate = parentopp.Billing_Start_Date__c.toStartOfMonth();
closeDate = childopp.CloseDate;
}
else
{
childopp.CloseDate = parentopp.CloseDate.toStartOfMonth();
closeDate = childopp.CloseDate;
}
}
else if(parentopp.Billing_Start_Date__c != Null && Count != 1)
{
If(parentopp.Payment_Frequency__c == 'Monthly')
{
childopp.CloseDate = closeDate.addMonths(1);
closeDate = childopp.CloseDate.toStartOfMonth();
}
else
If(parentopp.Payment_Frequency__c == 'Quarterly')
{
childopp.CloseDate = closeDate.addMonths(3);
closeDate = childopp.CloseDate.toStartOfMonth();
}
else
If(parentopp.Payment_Frequency__c == 'Half Yearly')
{
childopp.CloseDate = closeDate.addMonths(6);
closeDate = childopp.CloseDate.toStartOfMonth();
}
else
{
childopp.CloseDate = closeDate;
closeDate = childopp.CloseDate.toStartOfMonth();
}
}
childopp.StageName = 'Closed Won';
childopp.Parent_Opportunity__c = parentopp.Id;
oppsToInsert.add(childopp);
}//End Of for
}//End of If Checkbox
}//End of for
//Iterating OpportunityLineitems For Opportunity to update related OpportunityLineItem if Present
for(OpportunityLineitem opl : [SELECT Id, Product2Id, Quantity, UnitPrice, Pricebookentryid, OpportunityId FROM OpportunityLineItem Where OpportunityId IN : parentoppIds])
{
//Mapping Inserted or Old opportunityIds to Clone the related OpportunityLineItem
if(!oplitemsMap.containsKey(opl.OpportunityId))
{
oplitemsMap.put(opl.OpportunityId, new List<OpportunityLineitem>{opl});
}//End of IF Old IDs
else
{
oplitemsMap.get(opl.OpportunityId).add(opl);
}//end of else
}//End Of for iteration oppoopportunityLineItem
//If Opportunity is Not Empty
if(!oppsToInsert.IsEmpty())
{
//Cloning Inserted Or Updated Opportunity
upsert oppsToInsert;
//Iterating Cloned Inserted Or Updated Opportunity for OpportunityLineItem
for(Opportunity op : oppsToInsert)
{
//for inserted Or updated Opportunity if Parent_opportunity__c is equals Opportunity
if(oplitemsMap.containsKey(op.Parent_Opportunity__c))
{
//Iterating OpportunityLineitem From Parent Opportunity For Cloning Its OpportunityLineItem
for(OpportunityLineitem ol : oplitemsMap.get(op.Parent_Opportunity__c))
{
OpportunityLineItem oli = new OpportunityLineitem();
oli.OpportunityId = op.Id;
oli.Product2Id = ol.Product2Id;
oli.Quantity = ol.Quantity;
oli.UnitPrice = ol.UnitPrice;
oli.pricebookentryid=ol.pricebookentryid;
oppitemlist.add(oli);
}//End of for
}//End of If
}//End Of For
//Cloning OpportunityLineItem From Parent Opportunity OpportunityLineItem
if(!oppitemlist.isEmpty())
{
insert oppitemlist;
}//End Of if
}//End of If for "Cloning OpportunityLineItem"
for(Opportunity parentOppty : ListOpp)
{
if(parentOppty.Cancelation_Date__c != Null || parentOppty.Service_End_Date__c != Null)
{
OldOppIds.add(parentOppty.Id);
}
}
for(Opportunity childOppty : [Select Id, CloseDate, Parent_Opportunity__c From Opportunity Where Parent_Opportunity__c IN : OldOppIds])
{
for(Opportunity parentOppty : ListOpp)
{
if(parentOppty.Cancelation_Date__c != Null && childOppty.CloseDate > parentOppty.Cancelation_Date__c || parentOppty.Service_End_Date__c != Null && childOppty.CloseDate > parentOppty.Service_End_Date__c)
{
ChildOppIds.add(childOppty.Id);
}
}
}
List<Opportunity> oppsToDelete = [Select Id, CloseDate, Parent_Opportunity__c From Opportunity Where Id IN : ChildOppIds];
if(!oppsToDelete.IsEmpty())
{
delete oppsToDelete;
}
}//End Of Recursion
}//End of after Update
}
The code provided in question does not highlight the uncovered lines. Since this code is huge and requires an understanding of your implementation, it might not be possible to provide exact suggestions. However, the below articles give a good insight into how coverage can be improved
https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines
https://salesforce.stackexchange.com/questions/244788/how-do-i-write-an-apex-unit-test
Examples of OLI test classes:
https://salesforce.stackexchange.com/questions/72883/how-to-create-opportunity-line-items-in-test-classes
https://salesforce.stackexchange.com/questions/117406/recursivecheck-prevents-trigger-from-running-during-tests
If this information helps, please mark the answer as best. Thank you