How to Execute the Javascript Functionality in Salesforce Custom Button:
{!REQUIRESCRIPT(‘/soap/ajax/28.0/connection.js’)}
var opportunityRecord= {!GETRECORDIDS($ObjectType.OpportunityLineItem)};
if (opportunityRecord[0] == null) {
alert(“Please select at least one product to traffic.”) }
else {
var oppLineItemsIds = “”;
//fetch opportunity Line items Ids
for(var rowNum in opportunityRecord){
oppLineItemsIds += “‘”+ opportunityRecord[rowNum] + “‘,”;
}
//remove last comma
oppLineItemsIds = oppLineItemsIds.slice(0, oppLineItemsIds.length – 1);
//Enclose the ids in round brackets
if(oppLineItemsIds.length > 1){
oppLineItemsIds = “(” + oppLineItemsIds + “)”;
}
var result = sforce.connection.query(“SELECT id,Trafficked__c FROM OpportunityLineItem WHERE ID in “+oppLineItemsIds);
var records = result.getArray(“records”);
for(var i=0;i<records.length;i++)
{
if(records[i].Trafficked__c == ‘true’){
records[i].Trafficked__c = false;
}
else{
records[i].Trafficked__c = true;
}
}
var resultoli = sforce.connection.update(records);
if(resultoli[0].success==’true’){
window.location.reload(); }
}