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
EMHDevEMHDev 

How do I navigate Opportunity line items (child/parent results)?

I have written the following code:
Code:
function GetProductLineItems(OppId) {
 
 var i=0;
 var ProdName = "";
 var _Query = "Select o.TotalPrice, o.PricebookEntry.Name From OpportunityLineItem o WHERE  o.OpportunityId='" + OppId + "'";
 var queryResult = sforce.connection.query(_Query);
 var lineItemList = queryResult.getArray("records"); 
 
 var JIS = new sforce.SObject("Job_Information_Sheet__c"); //create JIS sobject
    

 if (lineItemList.length > 0) {
// Initialise values that will be grouped
   JIS.Background_Value__c = 0;
   JIS.Training_value__c = 0;
   JIS.Messaging_Value__c = 0;
   JIS.Movie_value__c = 0;
   JIS.Slide_Production_Value__c = 0;
   JIS.Other_Value__c = 0;

  for (i=0; i<lineItemList.length; i++) { 
     ProdName  = lineItemList[i].get("PricebookEntry.Name");
alert("Product is "+ProdName);
     switch(ProdName) {

 However, ProdName always comes up null and when I do the query in the Eclipse Explorer, the results give me a value for the TotalPrice but in the PricebookEntry column it just shows "PricebookEntry", which I have to click on to see the pricebookentry name.  I want to copy the prices to fields in a custom object but the field to copy to depends on what the pricebook entry is, hence the switch statement and the need to extract the product (pricebookentry) name.

Can anyone tell me how to code it to traverse into the pricebookentry and extract the name?  I've tried a few approaches and none of them work.

Many thanks,
Erica



Best Answer chosen by Admin (Salesforce Developers) 
EMHDevEMHDev
I've managed to work this out, so I'm posting the code for anyone else with the same problem:
Code:
if (lineItemList.length > 0) {
 // Initialise values that will be grouped
   JIS.Background_Value__c = 0;
   JIS.Training_value__c = 0;
   JIS.Messaging_Value__c = 0;
   JIS.Movie_value__c = 0;
   JIS.Slide_Production_Value__c = 0;
   JIS.Other_Value__c = 0;
 
  for (i=0; i<lineItemList.length; i++) { 
   var PBEntry = lineItemList[i].get("PricebookEntry");
   ProdName = PBEntry.Name;


   switch(ProdName) {

 The get is done on the PricebookEntry and then the assignment is done by adding the Name property to the lineitem entry returned.

Hope this helps someone else.

Erica