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

Written by 

I am Sakthivel Madesh, a certified Salesforce Advanced Developer & Administrator working on Salesforce Technology since 2011. I am currently working in Techforce Services as Sr. Salesforce Developer. I have worked on multiple technologies Like PHP, Java, Salesforce, I am very passionate about Salesforce and to earn 15x Salesforce Certification. I love Trailhead for learning Salesforce Skill and Become a Trailhead 5x Ranger too. I Love to Learn & Share my Salesforce knowledge to Salesforce Learners/Beginner using My Blog and Salesforce Community.

Leave a Reply

Your email address will not be published. Required fields are marked *

*