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
Danica Alexis LlantosDanica Alexis Llantos 

Auto update checkbox to true using Developer Console - Open Execute

Hello all.  I am VERY new to the development side of SF but have been doing admin for over 4 years.  I want to use Apex to edit a checkbox on the product object. for this query (SELECT Id,Name, fw8__QuickBooks_ID__c,Family,fw8_1__QB ) How do I accomplish this, please help.
Danica Alexis LlantosDanica Alexis Llantos
This is the Query ( SELECT Id,Name, fw8__QuickBooks_ID__c,Family,fw8_1__QB_OK__c FROM product2 Where IsActive = true and fw8__QuickBooks_ID__c != null  )
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Danica,

Can you confirm which checkbox field do you want to update?

Thanks,
 
Danica Alexis LlantosDanica Alexis Llantos
Hi Sai,

Thank you for your response, the field I want to update is the QB OK field (checkbox) API Name fw8_1__QB_OK__c under Product Object.
Mohit. LakdeMohit. Lakde
Danica,

If you want to update specific record (replace RECORDID with Specific  SF recordId):
Product2 productRecord = [SELECT Id,Name, fw8__QuickBooks_ID__c,Family,fw8_1__QB_OK__c FROM product2 Where Id = 'RECORDID' AND IsActive = true and fw8__QuickBooks_ID__c != null];
productRecord.fw8_1__QB_OK__c = TRUE;
UPDATE productRecord;


if you want to update all records :
List<Product2> productList = [SELECT Id,Name, fw8__QuickBooks_ID__c,Family,fw8_1__QB_OK__c FROM product2 Where IsActive = true and fw8__QuickBooks_ID__c != null];

List<Product2> productListToUpdate = new List<Product2>();
for(Product2 prod : productList){
prod.fw8_1__QB_OK__c = TRUE;
productListToUpdate.add(prod);
}

if(!productListToUpdate.isEmpty()){
UPDATE productListToUpdate;
}

 
Vineeth ReddyVineeth Reddy
Hi Danica,

Here is the code you need.
 
// query products
List<Product2> products = [SELECT Id, Name, fw8__QuickBooks_ID__c, Family, fw8_1__QB_OK__c FROM Product2 WHERE IsActive = true AND fw8__QuickBooks_ID__c != null];

// loop through the products and update the checkbox to true
for(Product2 product : products)
        product.fw8_1__QB_OK__c = true;

// commit to database
update products;

 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Danica,

You can try the code as below.
 
List<Product2> produlist = [SELECT Id, Name, fw8__QuickBooks_ID__c, Family, fw8_1__QB_OK__c FROM Product2 WHERE IsActive = true AND fw8__QuickBooks_ID__c != null];

List<Product2>tobeupdated= new List<Product2>();
for(Product2 prd : produlist){
       prd.fw8_1__QB_OK__c = true;
tobeupdated.add(prd);
}

if(tobeupdated.size()>0)
update tobeupdated;
Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,