I'm trying to clone an Opportunity and its Synced Quote along with the Quote Line Items associated with that Synced Quote, but I can't seem to insert the cloned Quote Line Items. It doesn't throw any errors when I run the Visualforce Page that calls the Apex function, and checking the debug logs tells me that the insert went fine, but when I look at the cloned Synced Quote or query for Quote Line Items I don't find the clones that I supposedly inserted.
Here is my code:
public class OpportunityExtension {
private final Opportunity o;
private final Quote activeQuote;
private final List<QuoteLineItem> lineItems;
public OpportunityExtension(apexpages.standardcontroller std) {
this.o = (Opportunity)std.getrecord();
this.activeQuote = this.o.SyncedQuote;
List<Quote> temp = [SELECT (SELECT Description, Discount, Product2Id, PricebookEntryId, Quantity, UnitPrice, CurrencyIsoCode, ServiceDate, SortOrder, Subtotal FROM QuoteLineItems) FROM Quote WHERE Id = :activeQuote.Id];
if (temp.get(0).QuoteLineItems != null) {
this.lineItems = temp.get(0).QuoteLineItems;
}
}
public PageReference cloneOpportunity() {
Opportunity newO = new Opportunity(
Type = o.Type,
Name = o.Name + ' - CLONE',
StageName = o.StageName,
CurrencyIsoCode = o.CurrencyIsoCode,
CloseDate = Date.today(),
Account = o.Account,
AccountId = o.AccountId
);
insert newO;
if (o.SyncedQuote != null) {
Quote newQ = new Quote(
Name = activeQuote.Name,
Pricebook2Id = activeQuote.Pricebook2Id,
opportunityId = newO.Id
);
insert newQ;
List<QuoteLineItem> newLineItems = new List<QuoteLineItem>();
for (QuoteLineItem ql : lineItems) {
QuoteLineItem newQL = ql.clone();
newQL.QuoteId = newQ.Id;
newLineItems.add(newQL);
}
insert newLineItems;
newO.SyncedQuoteId = newQ.Id;
update newO;
}
PageReference newPage = new PageReference('/'+newO.Id);
newPage.setRedirect(true);
return newPage;
}
}
<apex:pageMessages>
on your VF page