You need to sign in to do that
Don't have an account?
Stavros McGillicuddy
Code to query and concatenate Opportunity Line items only works if more than one line item exists
At the click of a custom button on an Opportunity object, this is supposed to:
- Capture all Opportunity line items
- Concatenate all line items except ones that have DISCOUNT in the name into a Samples_Sent__c field
- Delete all Opportunity line items
{!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();
This is what finally worked for me
All Answers
This is what finally worked for me