function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sml9099sml9099 

Add Functionality in Custom button executing javascript

Hi All,

I have created a custom button executing javascript on Opprtunity product related list of opportunity. This button just mass updates the checkbox (Trafficked__c) to true on selected opportunity line items of opportunity. Business users just have to select the number of line items and click the button and the checkbox will be set to TRUE. I want with the second click on the button , the ckeckbox should be set to FALSE.  Below is the javascript which sets the cjeckbos to TRUE. Kindly help.

{!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++)
{

records[i].Trafficked__c = true;

}

var resultoli = sforce.connection.update(records);
if(resultoli[0].success=='true'){
window.location.reload(); }
}
Best Answer chosen by sml9099
Shweta_AgarwalShweta_Agarwal
Hi
I have just added a condition which will check if Trafficked__c is TRUE or false. according to that it will update the checkbox.

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

All Answers

Shweta_AgarwalShweta_Agarwal
Hi
I have just added a condition which will check if Trafficked__c is TRUE or false. according to that it will update the checkbox.

{!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(); }
}
This was selected as the best answer
sml9099sml9099
Thanks a lot for your help. It worked