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
Stavros McGillicuddyStavros McGillicuddy 

Quantity not defined

The code below works fine concatenating PriceBook.Entry.Product2.Name on seperate lines of a custom field with a comma at the end of each line.

I need to add Quantity and TotalCost for each Opportunity Product Line item to each line of the custom field and seperate the values with a space hyphen space like this:

PricebookEntryProduct2.Name - Quantity - TotalCost,
PricebookEntryProduct2.Name - Quantity - TotalCost,
PricebookEntryProduct2.Name - Quantity - TotalCost,
PricebookEntryProduct2.Name - Quantity - TotalCost,

When I try to add Quantity and TotalPrice like this, I get a "Quantity not defined" error
 
var strProductNames = '';
for(var i=0; i<retriveOpptyLineItems.records.length ; i++){
strProductNames += retriveOpptyLineItems.records[i].PricebookEntry.Product2.Name, Quantity, TotalPrice + ',' + '\n ';
}


 
{!REQUIRESCRIPT("/soap/ajax/32.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/32.0/apex.js")}
var record = new sforce.SObject("Opportunity");
record.Id = '{!Opportunity.Id}';
var retriveOpptyLineItems = sforce.connection.query("Select PricebookEntry.Product2.Name, Quantity, TotalPrice From OpportunityLineItem WHERE OpportunityId = '{!Opportunity.Id}' and (NOT Name like '%Discount%')");
 
var strProductNames = '';
for(var i=0; i<retriveOpptyLineItems.records.length ; i++){
strProductNames += retriveOpptyLineItems.records[i].PricebookEntry.Product2.Name + ',' + '\n ';
}
 
//eliminate the last ','
if(strProductNames.length>0){
strProductNames = strProductNames.substring(0,strProductNames.length-1);
}
record.Sample_Product_Name_1__c = strProductNames;
sforce.connection.update([record]);
window.location.reload();
Hugh Wheeler 46Hugh Wheeler 46
Try something like
 
strProductNames += retriveOpptyLineItems.records[i].PricebookEntry.Product2.Name +','+ retriveOpptyLineItems.records[i].Quantity +','+ retriveOpptyLineItems.records[i].TotalPrice + ',' + '\n ';

I think you need to tell it where to find the Quantity and Total Price fields.  Currently you are using them as if they are separately declared variables.
 
Hugh Wheeler 46Hugh Wheeler 46
On second thoughts, Just re-read your post and you just want a comma at the end and space hypen space between each.  The code should read:
 
strProductNames += retriveOpptyLineItems.records[i].PricebookEntry.Product2.Name +' - '+ retriveOpptyLineItems.records[i].Quantity +' - '+ retriveOpptyLineItems.records[i].TotalPrice + ',' + '\n ';