1

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

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

3
  • did you put update statement? Aug 7, 2016 at 2:18
  • Is Sample_Product_Name_1__c a lookup field or a text field? Because if it's a lookup you need to pass an Id of the Opportunity Line Item and not just it's name.. Aug 7, 2016 at 11:04
  • Sample_Productt_Name__c is a text field. Aug 7, 2016 at 12:45

1 Answer 1

0

Updated code will be as follows:

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; 

I have shown you to retrieve first record from the dataset.

You have to loop through the array to update records.

Final Code

{!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}'");

var strProductNames = '';
for(var i=0; i<retriveOpptyLineItems.records.length ; i++){
strProductNames += retriveOpptyLineItems.records[i].PricebookEntry.Product2.Name + ',';
}

//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();
12
  • his "should" work by placing everything into one field (Sample_Product_Name_1). Right? This throws an "Invalid or Unexpected token" error Aug 6, 2016 at 19:46
  • Thats the concept, you need to change query Aug 6, 2016 at 19:50
  • do want change the question, it will create confusion Aug 6, 2016 at 20:08
  • @Stavros see the updated answer Aug 6, 2016 at 20:48
  • I've posted updated code. No errors but not populating field Aug 6, 2016 at 21:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .