Skip to main content
Updated to include a for statement but invalid or unexpected token error
Source Link

Updated code but, Invalid or unexpected token is back.

{!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();

Updated code but, Invalid or unexpected token is back.

{!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();

Updated code partially working (only grabs one of the Opportunity Line Items)
Source Link

I am getting close, no errors and the field is partially populated. I think my problem was spaces in my query This works but is only grabbing one of the two Opportunity line items on my opportunity. I think this is what you meant when you said loop through but, I don't know what that should look like.

{!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();

I am getting close, no errors and the field is partially populated. I think my problem was spaces in my query This works but is only grabbing one of the two Opportunity line items on my opportunity. I think this is what you meant when you said loop through but, I don't know what that should look like.

{!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();
updated code with "sforce.connection.update([record]);"
Source Link

My Opportunities may have up to 4 Product line items. I am trying to copy the 4 Opportunity Product names to 4 Custom text fields.

This throws an OpporunityLineItem not defined error and, I'm not sure how to find all line items

Thank you

{!REQUIRESCRIPT("/soap/ajax/32.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/32.0/apex.js")} 

var record = new sforce.SObject("Opportunity"); 
record.Id = '{!Opportunity.Id}'; 

record.Sample_Product_Name_1__c=OpportunityLineItem.PricebookEntry.Name; 
record.Sample_Product_Name_2__c= <<The next line item Name>>; 
record.Sample_Product_Name_3__c= <<The next line item Name>>; 
record.Sample_Product_Name_4__c= <<The next line item Name>>; 


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

This "should" work by placing everything into one field (Sample_Product_Name_1). Right?

This throws an "Invalid or Unexpected token" error

{!REQUIRESCRIPT("/soap/ajax/32.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/32.0/apex.js")}
 
var record = new sforce.SObject("Opportunity");
record.oli ='{Select Amount,CloseDate,Name,
     (Select PricebookEntry.Product2Id,
             TotalPrice,
             UnitPrice,
             ListPrice
      From OpportunityLineItems)
   From Opportunity}; 
record.Id = '{!Opportunity.Id}';
record.Sample_Product_Name_1__c= record.oli;
sforce.connection.update([record]); window.location.reload();

Thank you for staying with me on this

This does not throw errors but it does not populate the field

{!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; 

window.location.reload();

Here is my latest attempt but it is now throwing an "Invalid or unexpected token" Is this the same code that works for you?

{!REQUIRESCRIPT("/soap/ajax/32.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/32.0/apex.js")}

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(); 

My Opportunities may have up to 4 Product line items. I am trying to copy the 4 Opportunity Product names to 4 Custom text fields.

This throws an OpporunityLineItem not defined error and, I'm not sure how to find all line items

Thank you

{!REQUIRESCRIPT("/soap/ajax/32.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/32.0/apex.js")} 

var record = new sforce.SObject("Opportunity"); 
record.Id = '{!Opportunity.Id}'; 

record.Sample_Product_Name_1__c=OpportunityLineItem.PricebookEntry.Name; 
record.Sample_Product_Name_2__c= <<The next line item Name>>; 
record.Sample_Product_Name_3__c= <<The next line item Name>>; 
record.Sample_Product_Name_4__c= <<The next line item Name>>; 


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

This "should" work by placing everything into one field (Sample_Product_Name_1). Right?

This throws an "Invalid or Unexpected token" error

{!REQUIRESCRIPT("/soap/ajax/32.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/32.0/apex.js")}
 
var record = new sforce.SObject("Opportunity");
record.oli ='{Select Amount,CloseDate,Name,
     (Select PricebookEntry.Product2Id,
             TotalPrice,
             UnitPrice,
             ListPrice
      From OpportunityLineItems)
   From Opportunity}; 
record.Id = '{!Opportunity.Id}';
record.Sample_Product_Name_1__c= record.oli;
sforce.connection.update([record]); window.location.reload();

Thank you for staying with me on this

This does not throw errors but it does not populate the field

{!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; 

window.location.reload();

My Opportunities may have up to 4 Product line items. I am trying to copy the 4 Opportunity Product names to 4 Custom text fields.

This throws an OpporunityLineItem not defined error and, I'm not sure how to find all line items

Thank you

{!REQUIRESCRIPT("/soap/ajax/32.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/32.0/apex.js")} 

var record = new sforce.SObject("Opportunity"); 
record.Id = '{!Opportunity.Id}'; 

record.Sample_Product_Name_1__c=OpportunityLineItem.PricebookEntry.Name; 
record.Sample_Product_Name_2__c= <<The next line item Name>>; 
record.Sample_Product_Name_3__c= <<The next line item Name>>; 
record.Sample_Product_Name_4__c= <<The next line item Name>>; 


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

This "should" work by placing everything into one field (Sample_Product_Name_1). Right?

This throws an "Invalid or Unexpected token" error

{!REQUIRESCRIPT("/soap/ajax/32.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/32.0/apex.js")}
 
var record = new sforce.SObject("Opportunity");
record.oli ='{Select Amount,CloseDate,Name,
     (Select PricebookEntry.Product2Id,
             TotalPrice,
             UnitPrice,
             ListPrice
      From OpportunityLineItems)
   From Opportunity}; 
record.Id = '{!Opportunity.Id}';
record.Sample_Product_Name_1__c= record.oli;
sforce.connection.update([record]); window.location.reload();

Thank you for staying with me on this

This does not throw errors but it does not populate the field

{!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; 

window.location.reload();

Here is my latest attempt but it is now throwing an "Invalid or unexpected token" Is this the same code that works for you?

{!REQUIRESCRIPT("/soap/ajax/32.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/32.0/apex.js")}

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(); 
updated code
Source Link
Loading
Added code
Source Link
Loading
Source Link
Loading