Skip to main content The Trailblazer Community will be unavailable from 2/1/2025 to 2/2/2025. Please plan your activities accordingly.
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();

 
2 answers
  1. Aug 27, 2016, 5:07 PM
    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.
Loading
0/9000