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
Marcela SanchezMarcela Sanchez 

custom button to update checkbox?

I'm trying to create a button on the object Opportunity Product, The button is a List Button and I want it to update the checkbox of the selected items.

{!REQUIRESCRIPT("/soap/ajax/37.0/connection.js")} 
var opp = new sforce.SObject('OpportunityLineItem'); 
opp.id = "{!OpportunityLineItem.Id}"; 
opp.Use_Calculated_Price__c = 1; 
result = sforce.connection.update([opp]); 
location.reload(true);

It doesn't show any mistake, but only refreshes the opportunity page.
I tryed with diferent values like '1', true, True, TRUE.

Thanks.
Best Answer chosen by Marcela Sanchez
Anirudh SinghAnirudh Singh
Hi Marcela,

The above code is not working as the code is only handling one record, therefore, it is not able to update multiple records selected.

Please use the below code, it will work fine:
{!REQUIRESCRIPT("/soap/ajax/37.0/connection.js")}

var selectedProductRecordIds={!GETRECORDIDS($ObjectType.OpportunityLineItem)};
var productRecordsToUpdate=[];

if(selectedProductRecordIds.length<1)
{
    alert("Please select at least one record.");
}
else
{
    for(var i=0; i<selectedProductRecordIds.length; i++)
    {
        var selectedProduct=new sforce.SObject("OpportunityLineItem");
        selectedProduct.Id=selectedProductRecordIds[i];
        selectedProduct.Use_Calculated_Price__c=true;
        productRecordsToUpdate.push(selectedProduct);
    }
    
    var updateResult=sforce.connection.update(productRecordsToUpdate);
    location.reload(true);
}

Please let me know if this helps.
If yes, please mark the Question as Solved as a best practice, so that others can also benefit from the answer.

Thanks and Regards,
Anirudh Singh

All Answers

Anirudh SinghAnirudh Singh
Hi Marcela,

The above code is not working as the code is only handling one record, therefore, it is not able to update multiple records selected.

Please use the below code, it will work fine:
{!REQUIRESCRIPT("/soap/ajax/37.0/connection.js")}

var selectedProductRecordIds={!GETRECORDIDS($ObjectType.OpportunityLineItem)};
var productRecordsToUpdate=[];

if(selectedProductRecordIds.length<1)
{
    alert("Please select at least one record.");
}
else
{
    for(var i=0; i<selectedProductRecordIds.length; i++)
    {
        var selectedProduct=new sforce.SObject("OpportunityLineItem");
        selectedProduct.Id=selectedProductRecordIds[i];
        selectedProduct.Use_Calculated_Price__c=true;
        productRecordsToUpdate.push(selectedProduct);
    }
    
    var updateResult=sforce.connection.update(productRecordsToUpdate);
    location.reload(true);
}

Please let me know if this helps.
If yes, please mark the Question as Solved as a best practice, so that others can also benefit from the answer.

Thanks and Regards,
Anirudh Singh
This was selected as the best answer
Marcela SanchezMarcela Sanchez
Yeah I found that fuction  (GETRECORDIDS) doing research yestesday. I did something similar...
 
{!REQUIRESCRIPT("/soap/ajax/37.0/connection.js")} 

var selectedOppIds = {!GETRECORDIDS($ObjectType.OpportunityLineItem)};
var OppForUpdate = [];

if (selectedOppIds[0] == null) {
    alert('You must select at least one record');
} 
else {
    for (var i = 0; i < selectedOppIds.length; i++) {
       var opp = new sforce.SObject("OpportunityLineItem");
       opp.Id = selectedOppIds[i];
       opp.Use_Calculated_Price__c = 1;
       OppForUpdate.push(opp);
    }
}
var saveResult = sforce.connection.update(OppForUpdate);
location.reload(true);

Anyway Thanks you! I forgot to update this question :)