You need to sign in to do that
Don't have an account?

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