1

At the click of a custom button on an Opportunity object, this is supposed to:

  1. Capture all Opportunity line items
  2. Concatenate all line items except ones that have DISCOUNT in the name into a Samples_Sent__c field
  3. Delete all Opportunity line items

It executes perfectly if the net count (opportunity line items that do not have DISCOUNT in the name) of the items being concatenated is two or more but does nothing (does not concatenate and no errors) if the net count is one.

{!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, Quantity, TotalPrice From OpportunityLineItem WHERE OpportunityId = '{!Opportunity.Id}' and (NOT Name like '%Discount%')");

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

//eliminate the last ','
if(strProductNames.length>0){
strProductNames = strProductNames.substring(0,strProductNames.length-1);
}
record.Samples_Sent__c = strProductNames;

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

Are you suggesting replacing

var retriveOpptyLineItems = sforce.connection.query("Select PricebookEntry.Product2.Name, Quantity, TotalPrice From OpportunityLineItem WHERE OpportunityId = '{!Opportunity.Id}' and (NOT Name like '%Discount%')");

with

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

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

1 Answer 1

0

If you log your query result:

var retriveOpptyLineItems = sforce.connection.query...
console.log(retriveOpptyLineItems);

You will see for 1 record something like:

Object { done: "true", queryLocator: null, records: Object, size: "1" }

And for two:

Object { done: "true", queryLocator: null, records: Array[2], size: "2" }

You have to turn single object to array with one element:

result = sforce.connection.query...
records = result.getArray("records");

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

Apex:

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

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

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);
}
opp.Samples_Sent__c = strProductNames;

sforce.connection.update([opp]);
window.location.reload();
4
  • by the way '\n' is also a character, try to subtract 2 Aug 14, 2016 at 17:50
  • I'm afraid being a noob, I am not understanding what you mean. The code above was put together with a lot of help from the discussion forums. Aug 14, 2016 at 19:46
  • @Stavros, please find updated answer. It contains all suggested changes. Aug 14, 2016 at 20:24
  • thank you so much for your help. This worked perfectly. Now I will go back and breakdown what you did with the syntax to better understand why it worked. Aug 14, 2016 at 22:21

You must log in to answer this question.

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