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
Tamojita GuhasarkarTamojita Guhasarkar 

Basic Question on Update fields

Hello All , 

I'hv a basic question on Updating fileds using APEX Code - rather I want to clear my concepts . The requirement is very simple. For Account Object , Update all Countries to United States of America where country is like US or USA . Now, I'm able to do it by writing below code

Public Class Acc_Country_Name_Change
{
     
   Public Pagereference Name_update()
   {
     
      List<Account> Act = [SELECT name,BillingCountry FROM Account where BillingCountry in ( 'US','USA')];
 
      for(Account a : act)
          {
      a.BillingCountry ='united states of america';
          } 
  update act;
     
      return null;
 
  }
 
 }

Now , I'hv two questions on this . Please help me to clear my concept. 

1> in the For loop , its like FOR ( sObject : List ) - so why both are not List ? is it because in FOR Loop , only one row get processed at a time ? Or something else?

2> Inside the FOR loop , we are writing a.BillingCountry ='united states of america'  , but in update its like Update act . Why so? 

Can someone please help me to clarify my doubts? I know they are very basic , But I'm kind of confused.

Thanks,
Tanoy
Best Answer chosen by Tamojita Guhasarkar
Akhil AnilAkhil Anil
Hi Tanoy,

Below are the answers to your queries.

1. You got it right. It is just one instance of an sObject because the FOR loop is processing just one record at a time.

2. When you loop through the loop and set the value of your billing country, you are basically setting that value in the individual records present in the act List. So once the FOR loop is completed every record in the act list will have the billingcountry that you have set. Howvever, this value is not yet commited to the database. To do you are doing the DML operation of update act. This will commit all the values into your database.

Hope that helps !

Kindly mark it as an answer if that resolves your query.

All Answers

Akhil AnilAkhil Anil
Hi Tanoy,

Below are the answers to your queries.

1. You got it right. It is just one instance of an sObject because the FOR loop is processing just one record at a time.

2. When you loop through the loop and set the value of your billing country, you are basically setting that value in the individual records present in the act List. So once the FOR loop is completed every record in the act list will have the billingcountry that you have set. Howvever, this value is not yet commited to the database. To do you are doing the DML operation of update act. This will commit all the values into your database.

Hope that helps !

Kindly mark it as an answer if that resolves your query.
This was selected as the best answer
Tamojita GuhasarkarTamojita Guhasarkar
Hi Akhil ,

Thanks for your quick response.It helps :) 
Akhil AnilAkhil Anil
Hi Tamojita,

Can you please mark it as an answer so that we can close this thread ?