Skip to main content The Trailblazer Community will be unavailable from 2/1/2025 to 2/2/2025. Please plan your activities accordingly.

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

 
5 answers
  1. Aug 14, 2016, 10:19 PM
    THank you for your help Kevin.

    This is what finally worked for me

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

     

     
Loading
0/9000