You need to sign in to do that
Don't have an account?
RelaxItsJustCode
Custom Apex cloning of Opportunity and a subset of Opportunity Line Items.... Please help?
This is the class that I use to create first the opportunity header and then specific line items, PROBLEM,,,, For some reason it only takes the first of the line items and adds it to the new renewal opportunity. Please help I will give major kudos.
Heres the code:
public with sharing class RenewalOpportunityCreation { public static void CreateRenewalOpportunity(Map<Id,Opportunity> oldMap, Map<Id,Opportunity> newMap) { List<Opportunity> opptsToInsert = new List<Opportunity>(); Opportunity newOppt = new Opportunity(); List<Id> OpptyIds = new List<Id>(); for (Opportunity o : newMap.Values()) { OpptyIds.add(o.id); } for(Opportunity oppt : [Select RecordTypeId, Name, Contact__c, AccountId, id, (Select PriceBookEntry.Name, unitprice, Quantity, Classification__c, Is_Price_book_Entry_Active__c,Is_Product_Active__c From OpportunityLineItems Where (Classification__c= 'Software' OR Classification__c= 'Bundle (SW w/ HW)' OR Classification__c= 'Annual Maintenance' OR Classification__c = 'Annual Maintenance - 2nd Year & Beyond')) From Opportunity where Id in :OpptyIds]) { newOppt.Name = oppt.Name + ' Renewal Opportunity'; newOppt.AccountId = oppt.AccountId; newOppt.Contact__c = oppt.Contact__c; newOppt.RecordTypeId = ServiceClass.getRecordTypesNameMap('Opportunity').get('Renewal Opportunity').Id; newOppt.CloseDate = Date.Today()+30; newOppt.StageName = 'Renewal Opportunity'; newOppt.Type = 'Add-On Business'; newOppt.NextStep = 'Send a invoice for maintenance renewal'; newOppt.Payment_Terms__c = 'Net 30'; newOppt.ForecastCategoryName = 'Omitted'; newOppt.Owner_Location__c = 'FL'; newOppt.Original_Opportunity__c = oppt.Id; newOppt.OwnerId = '00570000001MRuL'; newOppt.Renewal_Software__c = ''; for (OpportunityLineItem lineItem :oppt.OpportunityLineItems) { newOppt.Renewal_Software__c = newOppt.Renewal_Software__c + LineItem.PriceBookEntry.Name + '\n'; } if(newOppt.Renewal_Software__c <> Null) { opptsToInsert.add(newOppt); } } if(opptsToInsert.size() > 0) { insert(opptsToInsert); List<OpportunityLineItem> opptLineItemsToInsert = new List<OpportunityLineItem>(); List<Opportunity> OpptIdToInsert = new List<Opportunity>(); Opportunity newOpptId = new Opportunity(); for(Opportunity opptInsertedIDs : [Select Id, Description from Opportunity where Id in :opptsToInsert]) { newOpptId.ID = opptInsertedIDs.id; OpportunityLineItem newOpptLineItem = new OpportunityLineItem(); for(Opportunity opptInserted : [Select RecordTypeId, Name, Contact__c, AccountId, id, Description, (Select PricebookEntry.Name,PricebookEntryid, UnitPrice, Quantity, TotalPrice, Classification__c, Is_Price_book_Entry_Active__c,Is_Product_Active__c From OpportunityLineItems Where (Classification__c = 'Software' OR Classification__c= 'Bundle (SW w/ HW)' OR Classification__c= 'Annual Maintenance' OR Classification__c = 'Annual Maintenance - 2nd Year & Beyond')) From Opportunity where Id in :OpptyIds]) { for (OpportunityLineItem lineItem2 :opptInserted.OpportunityLineItems) { newOpptLineItem.Quantity = lineItem2.Quantity; newOpptLineItem.UnitPrice = lineItem2.UnitPrice; newOpptLineItem.PricebookEntryId = lineitem2.PricebookEntry.id; } newOpptLineItem.OpportunityID = newOpptId.ID; opptLineItemsToInsert.add(newOpptLineItem); } if(opptLineItemsToInsert.size() > 0) { insert(opptLineItemsToInsert); } }} } }
Thank you,
Steve Laycock
Well there were probling in nesting
All Answers
Well there were probling in nesting
public class Opportunity_Clone_Handler
{
public static void OpportunityCloneRecord(list<Opportunity> records)
{
//For List of Clone Opportunity
List<Opportunity> oppsToUpdate = 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> SetID = new Set<Id>();
//For Fetching Newly Inserted Ids for OpportunityLineitem
Set<id> parentoppIds = 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 : records)
{
SetID.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:SetID];
//Cloning Opportunity
for(Opportunity parentopp : ListOpp)
{
//If Paramters
if(parentopp.Create_Child_Opportunities_Invoice__c && parentopp.StageName == 'Closed Won' && parentopp.Product_Type__c == 'Subscription' && parentopp.Payment_Frequency__c != Null && parentopp.Interval__c != Null && parentopp.Interval__c != Null)
{
Integer Interval = Integer.valueOf(parentopp.Interval__c);
For(Integer Count=1;Count<=Interval;Count++)
{
parentoppIds.add(parentopp.Id);
Opportunity childopp = new Opportunity();
childopp.Name = parentopp.Name + '_Clone';
childopp.AccountId = parentopp.AccountId;
childopp.Type = 'Invoice';
childopp.Product_Type__c = parentopp.Product_Type__c;
If(parentopp.Billing_Start_Date__c != Null)
{
If(parentopp.Payment_Frequency__c == 'Monthly')
{
childopp.CloseDate = parentopp.Billing_Start_Date__c.addMonths(1);
}
else
If(parentopp.Payment_Frequency__c == 'Quarterly')
{
childopp.CloseDate = parentopp.Billing_Start_Date__c.addMonths(3);
}
else
If(parentopp.Payment_Frequency__c == 'Half Yearly')
{
childopp.CloseDate = parentopp.Billing_Start_Date__c.addMonths(6);
}
}
else
{
childopp.CloseDate = parentopp.CloseDate;
}
childopp.StageName = 'Closed Won';
childopp.Payment_Frequency__c = parentopp.Payment_Frequency__c;
childopp.Interval__c = parentopp.Interval__c;
childopp.Billing_Start_Date__c = parentopp.Billing_Start_Date__c;
childopp.Parent_Opportunity__c = parentopp.Id;
childopp.Cancelation_Date__c = parentopp.Cancelation_Date__c;
childopp.Service_End_Date__c = parentopp.Service_End_Date__c;
childOppIds.add(childopp.Id);
oppsToUpdate.add(childopp);
}//End of if Checkbox
}//End of for
//Fetching 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(!oppsToUpdate.IsEmpty())
{
//Cloning Inserted Or Updated Opportunity
insert oppsToUpdate;
//Iterating Cloned Inserted Or Updated Opportunity for OpportunityLineItem
for(Opportunity op : oppsToUpdate)
{
//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
If(parentopp.Cancelation_Date__c != Null && parentopp.CloseDate > parentopp.Cancelation_Date__c || parentopp.Service_End_Date__c != Null && parentopp.CloseDate > parentopp.Cancelation_Date__c)
{
delete oppsToUpdate;
}
}//End of If for Cloning
}//End Of Preventing Recursion
}
}
}