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
Raza Syed 26Raza Syed 26 

SOQL to update a custom field in account object

I have around 4000 records, and I have to update a custom field (e.g Rating) in Account object, where condition is that if value in the field is 10 update/change to "A" and if the value in a field is 20 change to "B" in all the records.

what is the best way i.e SOQL or data (Data Loader), I am new to SOQL

Thank you very much 
James LoghryJames Loghry
You have (at least) two good options to tackle this:
  1. Export the Accounts through Dataloader.  Manipulate the Ratings you need to in Excel.  Re-import the Accounts through Dataloader.
  2. Run the following code in Execute Anoymous (in the Developer Console):
List<Account> accts = [Select Rating__c From Account Where Rating__c = '10' or Rating__c = '20'];
for(Account acct : accts){
    if(acct.Rating__c == '10'){
        acct.Rating__c = 'A';
    }else if(acct.Rating__c == '20'){
        acct.Rating__c = 'B';
    }
}
update accts;

Granted, you'll likely have to update the fields and values I used in the example above.  (For instance, I'm not sure if the 10 and 20 are numerical or string values in your use case)
NaveenReddyNaveenReddy
Hi Raza,

  Please try below code in developer console.
 
List<Account> newAccnts= new List<Account>();
List<Account> accnts=[Select rating,customFieldX__c From Account];
for(Account acc:accnts)
{
  if(acc.customFieldX__c==10)
     {
       acc.Rating='A';
     newAccnts.add(acc);
    }
else if(acc.customFieldX__c==20)
 {
acc.Rating='B';
     newAccnts.add(acc);
} 
}

Update newAccnts;

Please let me know if you need any more help.

Mark it as solved if it solves your question.

Thanks,
Naveen
Raza Syed 26Raza Syed 26
Hi James,
Thanks for providing code.
10, 20 are datatype: Number
I dont have any experience in SOQL/SQL. I cut and paste your query in Developer console and try excute,
Error message: "Invalid Query , the query has to start with 'FIND' or 'SELECT'"

I would really appreciate your help.
 
Johnny KhoueiryJohnny Khoueiry
I believe that if you would have 4000 records, the apex developer console will immeditely fail since it cannot resolve up to 4000 records.