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 

Copy Opportunity Line Items to other fields (see code)

The code below will take all Product Names, Quantities and Total Price for each Opportunity line item of an Opportunity and concatenate them into one custom fields and then delete the Opportunity Line Items. It works flawlessly

For reporting purposes, instead of concatenating into one field I need this code modified to copy the:..
1st Opportunity line item Name and place it in  Sample_1_Name__c
2nd Opportunity line item Name and place it in  Sample_2_Name__c
3rd Opportunity line item Name and place it in  Sample_3_Name__c
4th Opportunity line item Name and place it in  Sample_4_Name__c
5th Opportunity line item Name and place it in  Sample_5_Name__c

1st Opportunity line item Quantity and place it in Sample_1_Quantity__c
2nd Opportunity line item Quantity and place it in Sample_2_Quantity__c
3rd Opportunity line item Quantity and place it in Sample_3_Quantity__c
4th Opportunity line item Quantity and place it in Sample_4_Quantity__c
5th Opportunity line item Quantity and place it in Sample_5_Quantity__c

1st Opportunity line item Total Price and place it in Sample_1_Price__c
2nd Opportunity line item Total Price and place it in Sample_2_Price__c
3rd Opportunity line item Total Price and place it in Sample_3_Price__c
4th Opportunity line item Total Price and place it in Sample_4_Price__c
5th Opportunity line item Total Price and place it in Sample_5_Price__c
 
{!REQUIRESCRIPT("/soap/ajax/32.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/32.0/apex.js")}

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

//copy opportunity line items
result = sforce.connection.query("Select PricebookEntry.Product2.Name, Quantity, TotalPrice From OpportunityLineItem WHERE OpportunityId = '{!Opportunity.Id}' and (NOT Name like '%Discount%')");
records = result.getArray("records");

var strProductNames = '';
for(var i=0; i<records.length ; i++){
 strProductNames += 'PRODUCT NAME: ' + records[i].PricebookEntry.Product2.Name + ' --- QUANTITY: ' + records[i].Quantity + ' --- TOTAL PRICE: $ ' + records[i].TotalPrice +',\n';
}

if(strProductNames.length>0){
 strProductNames = strProductNames.substring(0,strProductNames.length-2);
}
record.Samples_Sent__c = strProductNames;

//delete opportunity line items
var lineItems = sforce.connection.query("select id from opportunitylineitem where opportunityid = '{!Opportunity.Id}'")
var oliIds = []
var qri = new sforce.QueryResultIterator(lineItems)
while(qri.hasNext())
    oliIds.push(qri.next().Id)
sforce.connection.deleteIds(oliIds)

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

 
Stavros McGillicuddyStavros McGillicuddy
I may have found a solution. I have less than 100 opportunities with more than one OLI but never more than two. I will create Bundle SKUs so the OLI will never be more than one. Then I only need to copy the OLI into three custom fields (Sample_Name__c Sample_Quantity__c Sample_Price___c) . Here is the updated code but it is throwing an error  

{faultcode:'soapenv:Client'.faultstring:"" is not valid for the type xsd:double',}
 
{!REQUIRESCRIPT("/soap/ajax/32.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/32.0/apex.js")}

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

//copy opportunity line items
result = sforce.connection.query("Select PricebookEntry.Product2.Name, Quantity, TotalPrice From OpportunityLineItem WHERE OpportunityId = '{!Opportunity.Id}' and (NOT Name like '%Discount%')");
records = result.getArray("records");

var strProductNames = '';
var strProductQuantity = '';
var strProductPrice = '';
for(var i=0; i<records.length ; i++){
 strProductNames = records[i].PricebookEntry.Product2.Name
 strProductQuantity = records[i].Quantity
 strrProductPrice = '$ ' + records[i].TotalPrice +',\n';
}

if(strProductNames.length>0){
 strProductNames = strProductNames.substring(0,strProductNames.length-2);
}
record.Sample_Name__c = strProductNames;
record.Sample_Quantity__c = strProductQuantity;
record.Sample_Price__c = strProductPrice;

//delete opportunity line items
var lineItems = sforce.connection.query("select id from opportunitylineitem where opportunityid = '{!Opportunity.Id}'")
var oliIds = []
var qri = new sforce.QueryResultIterator(lineItems)
while(qri.hasNext())
    oliIds.push(qri.next().Id)
sforce.connection.deleteIds(oliIds)

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

 
Stavros McGillicuddyStavros McGillicuddy
Changing the field type of Sample_Quantity__c from number to text and Sample_Price_cc from currency to text stopped the error so I have to believe there is something wrong with my syntax. Sample_Name_cc populated as expected. Sample_Quantity_cc populated as expected. Sample_Price_cc did not poplate at all.