function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
mba75mba75 

syntax to clone with child record

hi guys,

any body has an idea of the syntax to clone a opportunity with its  product
my idea is bit with child record

opportunity opp_clone= opp.clone ;
Insert opp_clone; 


Thank you


JasonRogersJasonRogers
You need to clone the OpportunityLineItems as well.

Code:
List<OpportunityLineItem> theLineItems = opty.OpportunityLineItems.deepclone(false);
for (OpportunityLineItem oli : theLineItems) {
 oli.OpportunityId = upsale.Id;
 oli.Originating_Opportunity_ID__c = opty.Id;
}

 

mba75mba75
Hi Jason ,

Thank you

I want to clone the Opportunitylineitem from a trigger on opportunity after insert


trigger Oli_update on Opportunity (After insert) {



List<OpportunityLineItem> updatedoli = trigger.new.OpportunityLineItems.deepclone(false);
for (OpportunityLineItem oli : updatedoli) {
oli.OpportunityId = trigger.new.Id;
oli.Quote_Reference__c= null;
oli.ServiceDate= null;
updatedoli.add(oli);
}

update updatedoli;

and I get this error Message

Error: Compile Error: Initial term of field expression must be a concrete SObject:

Other wise my real issue is to write a test method to test my update on the opportunitylineitem this update is fire by a trigger after insert on the opportunity

I get only 17 % coverage

The idea is when I clone a opportunity. I have to clear some field at the opportunity level. It works but my test method does not go into the opportunityline item iteration (80 %) of my classes. so I can not deliver my jobs in production.

I think I need to do insert of my opportunity with my opportunitylineitem in one block. Otherwise when the trigger is fire the opportunityline item are not here .