You need to sign in to do that
Don't have an account?

I am receiving errors about duplicate ids in my contract list. Please help I will give kudos.
Here is the code.
Public Class ContractFunctionsCloneContractAndOLIs
{
Public static void CloneMaintContractAndLineItems(Map<Id,Contract> oldMap, Map<Id,Contract> newMap)
{
List<Id> ContractToClone = new List<Id>();
List<Id> ContractLineItemsToClone = new List<Id>();
Date StartDate;
Date EndDate;
Integer NumberOfDays = 0;
Integer NumberOfMonths = 0;
Integer NumberOfYears = 0;
Double YearTerm = 0;
Integer NumberOfLeapYearDays = 0;
Integer TermsByMonth = 0;
Integer Prevcount =0;
Boolean MaintTermMisMatch;
String OriginalCase;
Date CurrentDay;
Date PrevCurrentDay;
Integer PrevNumberOfDays =0;
Date PrevStartDate;
Date PrevEndDate;
Integer CNT = 0;
Integer PrevNumberOfLeapYearDays =0;
for(Contract c : newMap.Values())
{
ContractToClone.add(c.Id);
CNT = CNT +1;
}
for(Contract_Line_Item__c CLI :[Select id from Contract_Line_Item__c where Contract__c in :ContractToClone])
{
ContractLineItemsToClone.add(CLI.id);
}
Contract newContract = new Contract();
List<Contract> ContractsToInsert1 = new List<Contract>();
SET<Contract> ContractsToInsertSET = new SET<Contract>();
List<Contract> ContractsToInsert = new List<Contract>();
if(ContractToClone.size() > 0 && ContractLineItemsToClone.size() > 0)
{
for(Contract c : [Select id, Status, Attn_To__c, Original_Case__c, Maintenance_out_of_sync__c, NewlyCreatedNumDays__c, DateFinal__c,PrevNumberOfDays__c,
PrevStartDate__c, PrevEndDate__c, PrevNumberOfLeapYearDays__c, Term_In_Days__c, XNumberOfDays__c, AccountId, XNumberOfLeapYearDays__c, NumberOfLeapYearDays2__c,
SpecialTerms, FEndDate__c, FCompareDayEndDate__c, Total_Maintenance_Price__c,Months_between_start_and_end__c,Override_Total_Price__c,StartDate,
Contract_End_Date__c, Case__c, RecordTypeId from Contract where Id in :ContractToClone ])
{
StartDate = c.StartDate;
PrevStartDate = c.StartDate;
PrevEndDate = c.Contract_End_Date__c;
EndDate = c.Contract_End_Date__c;
OriginalCase = c.Original_Case__c;
MaintTermMisMatch = c.Maintenance_out_of_sync__c;
PrevNumberOfDays = PrevStartDate.DaysBetween(PrevEndDate)+1;
while (Prevcount < (PrevNumberOfDays))
{
if(PrevStartDate.Month() == 2 && PrevStartDate.Day() == 29)
{
PrevNumberOfLeapYearDays = PrevNumberOfLeapYearDays + 1;
}
PrevCurrentDay = PrevStartDate.addDays(Prevcount);
if(PrevCurrentDay.Month() == 2 && PrevCurrentDay.Day() == 29)
{
PrevNumberOfLeapYearDays = PrevNumberOfLeapYearDays + 1;
}
Prevcount++;
}
newContract.SpecialTerms = c.SpecialTerms;
newContract.Override_Total_Price__c = c.Override_Total_Price__c;
newContract.Total_Maintenance_Price__c = c.Total_Maintenance_Price__c;
newContract.Status = 'Pending';
newContract.AccountId = c.AccountId;
newContract.StartDate = c.Contract_End_Date__c + 1;
newContract.Attn_To__c = c.Attn_To__c;
// newContract.Case__c = c.Case__c;
newContract.Original_Case__c = OriginalCase;
newContract.Maintenance_out_of_sync__c = MaintTermMisMatch;
newContract.PrevNumberOfDays__c = PrevNumberOfDays;
newContract.PrevStartDate__c = PrevStartDate;
newContract.PrevEndDate__c = PrevEndDate;
newContract.PrevNumberOfLeapYearDays__c = PrevNumberOfLeapYearDays;
newContract.Months_between_start_and_end__c = c.StartDate.monthsBetween((c.Contract_End_Date__c +1));
Integer count = 0;
NumberOfDays = StartDate.DaysBetween(EndDate)+1;
while (count < (NumberOfDays+1))
{
if(EndDate.Month() == 2 && EndDate.Day() == 29)
{
NumberOfLeapYearDays = NumberOfLeapYearDays + 1;
}
CurrentDay = EndDate.addDays(count);
if(CurrentDay.Month() == 2 && CurrentDay.Day() == 29)
{
NumberOfLeapYearDays = NumberOfLeapYearDays + 1;
}
count++;
}
newContract.NumberOfLeapYearDays2__c = NumberOfLeapYearDays;
newContract.Count__c = count;
newContract.PrevCount__c = Prevcount;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if(NumberOfDays >= 365)
{
NumberOfDays = NumberOfDays + NumberOfLeapYearDays - PrevNumberOfLeapYearDays;
newContract.NewNumberOfDays__c = NumberOfDays;
newContract.NewlyCreatedNumDays__c = NumberOfDays - NumberOfLeapYearDays;
}
if(NumberOfDays < 365)
{
newContract.NewNumberOfDays__c = NumberOfDays;
newContract.NewlyCreatedNumDays__c = NumberOfDays;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - PrevNumberOfLeapYearDays
newContract.Contract_End_Date__c = c.Contract_End_Date__c.addDays((NumberOfDays));
newContract.RecordTypeId = ServiceClass.getRecordTypesNameMap('Contract').get('Maintenance Contract').Id;
newContract.Parent_Contract__c = c.id;
ContractsToInsert1.add(newContract);
for (Contract s : ContractsToInsert1) {
if (ContractsToInsertSET.add(s)) {
ContractsToInsert.add(s);
}
}}}
insert(ContractsToInsert);
List<Contract_Line_Item__c> ContractLineItemsToInsert = new List<Contract_Line_Item__c>();
String ContractId;
String AccountId;
String CaseId;
String CLIStatus;
for(Contract insertedContracts : [Select Id, Case__c, AccountId from Contract where id in :ContractsToInsert])
{
ContractId = insertedContracts.id;
AccountId = insertedContracts.AccountId;
CaseId = insertedContracts.Case__c;
CLIStatus = 'Pending';
for(Contract_Line_Item__c cc : [Select id, Maintenance_Sales_Price__c, Contract_Line_Item_Status__c, Product_LU__c, Quantity__c, List_Price__c from Contract_Line_Item__c where Id in :ContractLineItemsToClone])
{
Contract_Line_Item__c newCLI = new Contract_Line_Item__c();
newCLI.Contract_Line_Item_Status__c = CLIStatus;
newCLI.Contract__c = ContractId;
//newCLI.Case_Id__c = CaseId;
newCLI.Account__c = AccountId;
newCLI.Contract_Line_Item_Origin__c = 'Contract Clone Follow Up Contract';
newCLI.Product_LU__c = cc.Product_LU__c;
newCLI.Quantity__c = cc.Quantity__c;
newCLI.List_Price__c = cc.List_Price__c;
newCLI.Maintenance_Sales_Price__c = cc.Maintenance_Sales_Price__c;
if(cc.Contract_Line_Item_Status__c == 'Do Not Renew')
{
newCLI.Contract_Line_Item_Status__c = 'Do Not Renew';
}
if(cc.Contract_Line_Item_Status__c == 'Cancelled')
{
newCLI.Contract_Line_Item_Status__c = 'Cancelled';
}
ContractLineItemsToInsert.add(newCLI);
}}
insert(ContractLineItemsToInsert);
//update(ContractLineItemsToInsert);
}}
Public Class ContractFunctionsCloneContractAndOLIs
{
Public static void CloneMaintContractAndLineItems(Map<Id,Contract> oldMap, Map<Id,Contract> newMap)
{
List<Id> ContractToClone = new List<Id>();
List<Id> ContractLineItemsToClone = new List<Id>();
Date StartDate;
Date EndDate;
Integer NumberOfDays = 0;
Integer NumberOfMonths = 0;
Integer NumberOfYears = 0;
Double YearTerm = 0;
Integer NumberOfLeapYearDays = 0;
Integer TermsByMonth = 0;
Integer Prevcount =0;
Boolean MaintTermMisMatch;
String OriginalCase;
Date CurrentDay;
Date PrevCurrentDay;
Integer PrevNumberOfDays =0;
Date PrevStartDate;
Date PrevEndDate;
Integer CNT = 0;
Integer PrevNumberOfLeapYearDays =0;
for(Contract c : newMap.Values())
{
ContractToClone.add(c.Id);
CNT = CNT +1;
}
for(Contract_Line_Item__c CLI :[Select id from Contract_Line_Item__c where Contract__c in :ContractToClone])
{
ContractLineItemsToClone.add(CLI.id);
}
Contract newContract = new Contract();
List<Contract> ContractsToInsert1 = new List<Contract>();
SET<Contract> ContractsToInsertSET = new SET<Contract>();
List<Contract> ContractsToInsert = new List<Contract>();
if(ContractToClone.size() > 0 && ContractLineItemsToClone.size() > 0)
{
for(Contract c : [Select id, Status, Attn_To__c, Original_Case__c, Maintenance_out_of_sync__c, NewlyCreatedNumDays__c, DateFinal__c,PrevNumberOfDays__c,
PrevStartDate__c, PrevEndDate__c, PrevNumberOfLeapYearDays__c, Term_In_Days__c, XNumberOfDays__c, AccountId, XNumberOfLeapYearDays__c, NumberOfLeapYearDays2__c,
SpecialTerms, FEndDate__c, FCompareDayEndDate__c, Total_Maintenance_Price__c,Months_between_start_and_end__c,Override_Total_Price__c,StartDate,
Contract_End_Date__c, Case__c, RecordTypeId from Contract where Id in :ContractToClone ])
{
StartDate = c.StartDate;
PrevStartDate = c.StartDate;
PrevEndDate = c.Contract_End_Date__c;
EndDate = c.Contract_End_Date__c;
OriginalCase = c.Original_Case__c;
MaintTermMisMatch = c.Maintenance_out_of_sync__c;
PrevNumberOfDays = PrevStartDate.DaysBetween(PrevEndDate)+1;
while (Prevcount < (PrevNumberOfDays))
{
if(PrevStartDate.Month() == 2 && PrevStartDate.Day() == 29)
{
PrevNumberOfLeapYearDays = PrevNumberOfLeapYearDays + 1;
}
PrevCurrentDay = PrevStartDate.addDays(Prevcount);
if(PrevCurrentDay.Month() == 2 && PrevCurrentDay.Day() == 29)
{
PrevNumberOfLeapYearDays = PrevNumberOfLeapYearDays + 1;
}
Prevcount++;
}
newContract.SpecialTerms = c.SpecialTerms;
newContract.Override_Total_Price__c = c.Override_Total_Price__c;
newContract.Total_Maintenance_Price__c = c.Total_Maintenance_Price__c;
newContract.Status = 'Pending';
newContract.AccountId = c.AccountId;
newContract.StartDate = c.Contract_End_Date__c + 1;
newContract.Attn_To__c = c.Attn_To__c;
// newContract.Case__c = c.Case__c;
newContract.Original_Case__c = OriginalCase;
newContract.Maintenance_out_of_sync__c = MaintTermMisMatch;
newContract.PrevNumberOfDays__c = PrevNumberOfDays;
newContract.PrevStartDate__c = PrevStartDate;
newContract.PrevEndDate__c = PrevEndDate;
newContract.PrevNumberOfLeapYearDays__c = PrevNumberOfLeapYearDays;
newContract.Months_between_start_and_end__c = c.StartDate.monthsBetween((c.Contract_End_Date__c +1));
Integer count = 0;
NumberOfDays = StartDate.DaysBetween(EndDate)+1;
while (count < (NumberOfDays+1))
{
if(EndDate.Month() == 2 && EndDate.Day() == 29)
{
NumberOfLeapYearDays = NumberOfLeapYearDays + 1;
}
CurrentDay = EndDate.addDays(count);
if(CurrentDay.Month() == 2 && CurrentDay.Day() == 29)
{
NumberOfLeapYearDays = NumberOfLeapYearDays + 1;
}
count++;
}
newContract.NumberOfLeapYearDays2__c = NumberOfLeapYearDays;
newContract.Count__c = count;
newContract.PrevCount__c = Prevcount;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if(NumberOfDays >= 365)
{
NumberOfDays = NumberOfDays + NumberOfLeapYearDays - PrevNumberOfLeapYearDays;
newContract.NewNumberOfDays__c = NumberOfDays;
newContract.NewlyCreatedNumDays__c = NumberOfDays - NumberOfLeapYearDays;
}
if(NumberOfDays < 365)
{
newContract.NewNumberOfDays__c = NumberOfDays;
newContract.NewlyCreatedNumDays__c = NumberOfDays;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - PrevNumberOfLeapYearDays
newContract.Contract_End_Date__c = c.Contract_End_Date__c.addDays((NumberOfDays));
newContract.RecordTypeId = ServiceClass.getRecordTypesNameMap('Contract').get('Maintenance Contract').Id;
newContract.Parent_Contract__c = c.id;
ContractsToInsert1.add(newContract);
for (Contract s : ContractsToInsert1) {
if (ContractsToInsertSET.add(s)) {
ContractsToInsert.add(s);
}
}}}
insert(ContractsToInsert);
List<Contract_Line_Item__c> ContractLineItemsToInsert = new List<Contract_Line_Item__c>();
String ContractId;
String AccountId;
String CaseId;
String CLIStatus;
for(Contract insertedContracts : [Select Id, Case__c, AccountId from Contract where id in :ContractsToInsert])
{
ContractId = insertedContracts.id;
AccountId = insertedContracts.AccountId;
CaseId = insertedContracts.Case__c;
CLIStatus = 'Pending';
for(Contract_Line_Item__c cc : [Select id, Maintenance_Sales_Price__c, Contract_Line_Item_Status__c, Product_LU__c, Quantity__c, List_Price__c from Contract_Line_Item__c where Id in :ContractLineItemsToClone])
{
Contract_Line_Item__c newCLI = new Contract_Line_Item__c();
newCLI.Contract_Line_Item_Status__c = CLIStatus;
newCLI.Contract__c = ContractId;
//newCLI.Case_Id__c = CaseId;
newCLI.Account__c = AccountId;
newCLI.Contract_Line_Item_Origin__c = 'Contract Clone Follow Up Contract';
newCLI.Product_LU__c = cc.Product_LU__c;
newCLI.Quantity__c = cc.Quantity__c;
newCLI.List_Price__c = cc.List_Price__c;
newCLI.Maintenance_Sales_Price__c = cc.Maintenance_Sales_Price__c;
if(cc.Contract_Line_Item_Status__c == 'Do Not Renew')
{
newCLI.Contract_Line_Item_Status__c = 'Do Not Renew';
}
if(cc.Contract_Line_Item_Status__c == 'Cancelled')
{
newCLI.Contract_Line_Item_Status__c = 'Cancelled';
}
ContractLineItemsToInsert.add(newCLI);
}}
insert(ContractLineItemsToInsert);
//update(ContractLineItemsToInsert);
}}
try below code...
Public Class ContractFunctionsCloneContractAndOLIs
{
Public static void CloneMaintContractAndLineItems(Map<Id,Contract> oldMap, Map<Id,Contract> newMap)
{
List<Id> ContractToClone = new List<Id>();
List<Id> ContractLineItemsToClone = new List<Id>();
Date StartDate;
Date EndDate;
Integer NumberOfDays = 0;
Integer NumberOfMonths = 0;
Integer NumberOfYears = 0;
Double YearTerm = 0;
Integer NumberOfLeapYearDays = 0;
Integer TermsByMonth = 0;
Integer Prevcount =0;
Boolean MaintTermMisMatch;
String OriginalCase;
Date CurrentDay;
Date PrevCurrentDay;
Integer PrevNumberOfDays =0;
Date PrevStartDate;
Date PrevEndDate;
Integer CNT = 0;
Integer PrevNumberOfLeapYearDays =0;
for(Contract c : newMap.Values())
{
ContractToClone.add(c.Id);
CNT = CNT +1;
}
for(Contract_Line_Item__c CLI :[Select id from Contract_Line_Item__c where Contract__c in :ContractToClone])
{
ContractLineItemsToClone.add(CLI.id);
}
Contract newContract = new Contract();
List<Contract> ContractsToInsert1 = new List<Contract>();
SET<Contract> ContractsToInsertSET = new SET<Contract>();
List<Contract> ContractsToInsert = new List<Contract>();
if(ContractToClone.size() > 0 && ContractLineItemsToClone.size() > 0)
{
for(Contract c : [Select id, Status, Attn_To__c, Original_Case__c, Maintenance_out_of_sync__c, NewlyCreatedNumDays__c, DateFinal__c,PrevNumberOfDays__c,
PrevStartDate__c, PrevEndDate__c, PrevNumberOfLeapYearDays__c, Term_In_Days__c, XNumberOfDays__c, AccountId, XNumberOfLeapYearDays__c, NumberOfLeapYearDays2__c,
SpecialTerms, FEndDate__c, FCompareDayEndDate__c, Total_Maintenance_Price__c,Months_between_start_and_end__c,Override_Total_Price__c,StartDate,
Contract_End_Date__c, Case__c, RecordTypeId from Contract where Id in :ContractToClone ])
{
StartDate = c.StartDate;
PrevStartDate = c.StartDate;
PrevEndDate = c.Contract_End_Date__c;
EndDate = c.Contract_End_Date__c;
OriginalCase = c.Original_Case__c;
MaintTermMisMatch = c.Maintenance_out_of_sync__c;
PrevNumberOfDays = PrevStartDate.DaysBetween(PrevEndDate)+1;
while (Prevcount < (PrevNumberOfDays))
{
if(PrevStartDate.Month() == 2 && PrevStartDate.Day() == 29)
{
PrevNumberOfLeapYearDays = PrevNumberOfLeapYearDays + 1;
}
PrevCurrentDay = PrevStartDate.addDays(Prevcount);
if(PrevCurrentDay.Month() == 2 && PrevCurrentDay.Day() == 29)
{
PrevNumberOfLeapYearDays = PrevNumberOfLeapYearDays + 1;
}
Prevcount++;
}
newContract.SpecialTerms = c.SpecialTerms;
newContract.Override_Total_Price__c = c.Override_Total_Price__c;
newContract.Total_Maintenance_Price__c = c.Total_Maintenance_Price__c;
newContract.Status = 'Pending';
newContract.AccountId = c.AccountId;
newContract.StartDate = c.Contract_End_Date__c + 1;
newContract.Attn_To__c = c.Attn_To__c;
// newContract.Case__c = c.Case__c;
newContract.Original_Case__c = OriginalCase;
newContract.Maintenance_out_of_sync__c = MaintTermMisMatch;
newContract.PrevNumberOfDays__c = PrevNumberOfDays;
newContract.PrevStartDate__c = PrevStartDate;
newContract.PrevEndDate__c = PrevEndDate;
newContract.PrevNumberOfLeapYearDays__c = PrevNumberOfLeapYearDays;
newContract.Months_between_start_and_end__c = c.StartDate.monthsBetween((c.Contract_End_Date__c +1));
Integer count = 0;
NumberOfDays = StartDate.DaysBetween(EndDate)+1;
while (count < (NumberOfDays+1))
{
if(EndDate.Month() == 2 && EndDate.Day() == 29)
{
NumberOfLeapYearDays = NumberOfLeapYearDays + 1;
}
CurrentDay = EndDate.addDays(count);
if(CurrentDay.Month() == 2 && CurrentDay.Day() == 29)
{
NumberOfLeapYearDays = NumberOfLeapYearDays + 1;
}
count++;
}
newContract.NumberOfLeapYearDays2__c = NumberOfLeapYearDays;
newContract.Count__c = count;
newContract.PrevCount__c = Prevcount;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if(NumberOfDays >= 365)
{
NumberOfDays = NumberOfDays + NumberOfLeapYearDays - PrevNumberOfLeapYearDays;
newContract.NewNumberOfDays__c = NumberOfDays;
newContract.NewlyCreatedNumDays__c = NumberOfDays - NumberOfLeapYearDays;
}
if(NumberOfDays < 365)
{
newContract.NewNumberOfDays__c = NumberOfDays;
newContract.NewlyCreatedNumDays__c = NumberOfDays;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - PrevNumberOfLeapYearDays
newContract.Contract_End_Date__c = c.Contract_End_Date__c.addDays((NumberOfDays));
newContract.RecordTypeId = ServiceClass.getRecordTypesNameMap('Contract').get('Maintenance Contract').Id;
newContract.Parent_Contract__c = c.id;
ContractsToInsert1.add(newContract);
for (Contract s : ContractsToInsert1) {
if (ContractsToInsertSET.add(s)) {
ContractsToInsert.add(s);
}
}}}
insert(ContractsToInsert);
Map<Id,Contract_Line_Item__c> ContractLineItemsToInsert = new Map<Id,Contract_Line_Item__c>();
String ContractId;
String AccountId;
String CaseId;
String CLIStatus;
for(Contract insertedContracts : [Select Id, Case__c, AccountId from Contract where id in :ContractsToInsert])
{
ContractId = insertedContracts.id;
AccountId = insertedContracts.AccountId;
CaseId = insertedContracts.Case__c;
CLIStatus = 'Pending';
for(Contract_Line_Item__c cc : [Select id, Maintenance_Sales_Price__c, Contract_Line_Item_Status__c, Product_LU__c, Quantity__c, List_Price__c from Contract_Line_Item__c where Id in :ContractLineItemsToClone])
{
Contract_Line_Item__c newCLI = new Contract_Line_Item__c();
newCLI.Contract_Line_Item_Status__c = CLIStatus;
newCLI.Contract__c = ContractId;
//newCLI.Case_Id__c = CaseId;
newCLI.Account__c = AccountId;
newCLI.Contract_Line_Item_Origin__c = 'Contract Clone Follow Up Contract';
newCLI.Product_LU__c = cc.Product_LU__c;
newCLI.Quantity__c = cc.Quantity__c;
newCLI.List_Price__c = cc.List_Price__c;
newCLI.Maintenance_Sales_Price__c = cc.Maintenance_Sales_Price__c;
if(cc.Contract_Line_Item_Status__c == 'Do Not Renew')
{
newCLI.Contract_Line_Item_Status__c = 'Do Not Renew';
}
if(cc.Contract_Line_Item_Status__c == 'Cancelled')
{
newCLI.Contract_Line_Item_Status__c = 'Cancelled';
}
ContractLineItemsToInsert.put(newCLI.id,newCLI);
}}
insert ContractLineItemsToInsert.values();
//update ContractLineItemsToInsert.values();
}}
Thanks,
Rockzz