+ Start a Discussion
Stavros McGillicuddyStavros McGillicuddy 

Need to add a loop to get all Opportunity Line Items to this code

This code grabs only one of the Opportunity Line Item names and puts it into a field.
I need to add a loop to grab all Opportunity Line Items for an Opportunity
 
{!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 From OpportunityLineItem WHERE OpportunityId = '{!Opportunity.Id}'");

record.Sample_Product_Name_1__c=retriveOpptyLineItems.records[0].PricebookEntry.Product2.Name; 

sforce.connection.update([record]); 
window.location.reload();

 
Ashish GargAshish Garg
Hi,
I have modified the code snippet here:
{!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 From OpportunityLineItem WHERE OpportunityId = '{!Opportunity.Id}'");

record.Sample_Product_Name_1__c = '';
for(oppLineItem in retriveOpptyLineItems){
	record.Sample_Product_Name_1__c += oppLineItem.PricebookEntry.Product2.Name';
}

sforce.connection.update([record]); 
window.location.reload();

 
Stavros McGillicuddyStavros McGillicuddy
Thank you Ashish,
This snippet is throwing an "Invalid or unexpected token" error
Ashish GargAshish Garg
Update:
{!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 From OpportunityLineItem WHERE OpportunityId = '{!Opportunity.Id}'");

record.Sample_Product_Name_1__c = '';
for(oppLineItem in retriveOpptyLineItems){
	record.Sample_Product_Name_1__c += oppLineItem.PricebookEntry.Product2.Name;
}

sforce.connection.update([record]); 
window.location.reload();