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
Vegaln1Vegaln1 

How to set a chield field value in a Parent-To-Child query.

Hello, I have a query to get the Opportunity and associated opportunity line items via the following SOQL which works very well:

OldOpp = [Select Id, name, Probability, StageName, CloseDate, OwnerId, AccountId, Type, Sales_Contact__c, Type_2__c, Sector__c, CurrencyIsoCode, Payment_Terms__c,Lost_Reason__c,HPR_Pipeline_Category__c,HPR_Project_Type__c, OnLine_Order_del__c, Description, NextStep, ELN_Application__c, Quote_Good_Through__c,Quote_FormattedId__c,Additional_Conditions__c,Quote_CreatedDate__c , Quote_LastModifiedDate__c,RecordTypeId,Consortia_Name__c,Client_Legal_Contact__c, LeadSource,Bill_To__c,CampaignId,Winning_Competitor__c,Pilot_Start__c, Pilot_End_FQ__c,Phase__c, (Select Id, OpportunityId , Description, PriceBookEntryId, Quantity, TotalPrice, License_Type__c,Product_Type__c from OpportunityLineItems) from opportunity where id = :OppId ];

 

In another function, I DeepClone the Opportunity line items and set values in the newly created line items, however, I am having trouble setting a value in the new line item from the value in old line item ( OldOpp.OpportunityLineItems). See the APEX snippet below:

 

OpportunityLineItem[] products = OldOpp.OpportunityLineItems.DeepClone(false);
for (OpportunityLineItem each :products) {
each.OpportunityId = NewOpp.id;
each.TotalPrice = 0;
each.Description = '[CLONED]' + each.description;
each.Quantity = 1;
each.License_Type__c = OldOpp.OpportunityLineItem.License_Type__c;
/* I've tried the following to no avail....

each.License_Type__c = OldOpp.OpportunityLineITem__r.License_Type__c;

each.License_Type__c = products.License_Type__c;


  */

 

}
insert products;

 So, what I am trying to accomplish is setting the value of License_Type__c in the cloned Line item to thevalue I queried in the first query.  Any suggestions on how to accomplish this?

 

Regards,

 

 

 

Venkat PolisettVenkat Polisett

Try the below code.

 

 

OpportunityLineItem[] products = OldOpp.OpportunityLineItems.DeepClone(false);

 

for (Integer i = 0; i < products.size(); i++)

{

OpportunityLineItem each = products[i];

OpportunityLineItem oldProduct = OldOpp.OpportunityLineItem[i];

 

each.OpportunityId = NewOpp.id;

each.TotalPrice = 0;

each.Description = '[CLONED]' + each.description;

each.Quantity = 1;

each.License_Type__c = oldProduct.License_Type__c;

}

 

insert products;

 

Vegaln1Vegaln1
Actually what worked was this:

each.License_Type__c = each.License_Type__c;

 Although I'm not sure why that would work but It does copy all the fields I need. Strange in my opinion.