+ Start a Discussion
Micky MMicky M 

No Id's

Hi guys, im not one for posting loads of code but im a bit stuck here. I have a custom object on an opportunity and that object has a sub object related via a master detail relationship. What i want to do is convert this opportunity objecs to contract objects. So i pull out all the opportunity line items and the related line item break downs. I loop though the line items creating new contract line items and that works fine, how ever the sub object cant be inserted as the id for the contract line item hasnt been created yet. Can anyone see how i can get around this ... thanks!

 

/Get all the Opportunity MCS recurring services with break down object attached to an opportunity.
List<MCS_Opportunity_Recurring_Service__c> mcso = [SELECT Id, Asset__c, Billing_Term__c, Charging_Metric__c, Contract_Term__c,
End_Date__c, Name, Opportunity__c, Quantity__c, Start_Date__c, Unit_Cost__c, Unit_Hard_Cost__c,
Unit_Price__c, Unit_Sales_Margin__c, Unit_Sell_Price__c, Unit_Soft_Cost__c,
(SELECT Start_Quantity__c,End_Quantity__c,Id,MCS_Recurring_Service_Breakdown__c,Break_Down_Type__c,
Customer_Allowance__c FROM Opportunity_Recurring_Service_Breakdown__r) FROM MCS_Opportunity_Recurring_Service__c
where Opportunity__c = :opportunityId and id in (select MCS_Recurring_Service_Breakdown__c
from Opportunity_Recurring_Service_Breakdown__c)];


List<MCS_Contract_Recurring_Service__c> mcsc = new List<MCS_Contract_Recurring_Service__c>();
map<id, List<Contract_Recurring_Service_Breakdown__c>> contractRecurringServiceWithBreakdowns = new map<id, List<Contract_Recurring_Service_Breakdown__c>>();
List<Contract_Recurring_Service_Breakdown__c> opportunityRecurringServiceBreakdownList = new List<Contract_Recurring_Service_Breakdown__c>();

//Get the related breakdown object attached to the Opportunity Recuring service and create a map
//using the service id as the key.
for(MCS_Opportunity_Recurring_Service__c o : mcso)
{
MCS_Contract_Recurring_Service__c temp = new MCS_Contract_Recurring_Service__c();

temp.Billing_Term__c = o.Billing_Term__c;
temp.Asset__c = o.Asset__c;

temp.Start_Date__c = o.Start_Date__c;
temp.End_Date__c = o.End_Date__c;

temp.Quantity__c = o.Quantity__c;
temp.Charging_Metric__c = o.Charging_Metric__c;

temp.Unit_Price__c = o.Unit_Price__c;
temp.Unit_Cost__c = o.Unit_Cost__c;

temp.Unit_Hard_Cost__c = o.Unit_Hard_Cost__c;
temp.Unit_Soft_Cost__c = o.Unit_Soft_Cost__c;

temp.Unit_Sell_Price__c = o.Unit_Sell_Price__c;
temp.Unit_Sales_Margin__c = o.Unit_Sales_Margin__c;

temp.contract__c = contractID;
mcsc.add(temp);


for(Opportunity_Recurring_Service_Breakdown__c orsb : o.Opportunity_Recurring_Service_Breakdown__r)
{
Contract_Recurring_Service_Breakdown__c tempContractBreakDown = new Contract_Recurring_Service_Breakdown__c();
tempContractBreakDown.End_Quantity__c = 1005;
tempContractBreakDown.MCS_Recurring_Service__c = temp.id;

opportunityRecurringServiceBreakdownList.add(tempContractBreakDown);
}
contractRecurringServiceWithBreakdowns.put(temp.id, opportunityRecurringServiceBreakdownList);
system.debug('temp.id =' + temp.id);
}
insert(mcsc);

system.debug('mcsc = ' + mcsc);
system.debug('contractRecurringServiceWithBreakdowns map = ' + contractRecurringServiceWithBreakdowns);

Ashish_SFDCAshish_SFDC
Hi Mickey, 

If you have the Parent Object IDs then you can Insert the Child Object with dataloader. 

Regards,
Ashish