0

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

  • did you put update statement? – Santanu Boral Aug 7 '16 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.. – Yonathan Goriachnick Aug 7 '16 at 11:04
  • Sample_Productt_Name__c is a text field. – Stavros_McGillicuddy Aug 7 '16 at 12:45
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();
| improve this answer | |
  • his "should" work by placing everything into one field (Sample_Product_Name_1). Right? This throws an "Invalid or Unexpected token" error – Stavros_McGillicuddy Aug 6 '16 at 19:46
  • Thats the concept, you need to change query – Santanu Boral Aug 6 '16 at 19:50
  • do want change the question, it will create confusion – Santanu Boral Aug 6 '16 at 20:08
  • @Stavros see the updated answer – Santanu Boral Aug 6 '16 at 20:48
  • I've posted updated code. No errors but not populating field – Stavros_McGillicuddy Aug 6 '16 at 21:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.