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
Dave The RaveDave The Rave 

Change field value en-mass using DML

Hi All,

Here is the change I am looking to make:

If fieldA (SortType_C) from CustomObject_C =  BLANK then change value of SortType_C to "Other"

I am a new administrator and learning fast, I managed to access the developer console but I am new to DML.

Can anyone help me with the code?

Thanks,

Dave The Rave
Best Answer chosen by Dave The Rave
AMIT AGRAWAL 5AMIT AGRAWAL 5
Run this code ...

List<RealEstateObject__c> recList = [SELECT Id, SortRealEstate__c FROM RealEstateObject__c WHERE SortRealEstate__c = NULL ];
for (RealEstateObject__c rec : recList) {
rec.SortRealEstate__c = 'Other';
}
update recList;

All Answers

Harpreet On CloudHarpreet On Cloud
Ideally you should use Workflow / Process Builder to handle it without code. But if you want to write code, write code in Trigger (before event)
 
for (CustomObject__c rec : Trigger.new) {
    if (rec.Sort_Type__c == null) rec.Sort_Type__c = 'Other';
}

 
Dave The RaveDave The Rave
Hi Harpreet, thanks for your quick answer.

The reason I am not using workflow or process builder is because they need a trigger like an update to a record or creation of a record to start a process or field change. So I understand (once I have put the real field and object names in the code) I can just past the code into the developer console, click "execute" and it should run the code? (maybe a stupid question, but I am in a learning mode now)
AMIT AGRAWAL 5AMIT AGRAWAL 5
Hi Dave,

I believe that the requirement is to update SortType_C to "Other", everytime this field is left blank. If you go by the developer console that will be a one-time solution which means it will work only when you run the code and this will not be an automated process. 

If you use a workflow rule for this, you do not have to write a trigger or any code for this and it will work each time a record is created or updated. 

Thanks,
Amit 
Harpreet On CloudHarpreet On Cloud
The code I mentioned above should be written as part of a trigger. It should be on "before insert" and "before update" events.

So whenever a record of CustomObject__c is being saved, if the Sort_Type__c field is not set on the record, the above code will set the Sort_Type__c field to "Other".

The trigger code would handle all the records that would be created or updated from the point, trigger is deployed. However for existing records, you can use the following code to run from Execute Anonymous window in Developer Console.
 
List<CustomObject__c> recList = [SELECT Id, Sort_Type__c FROM CustomObject__c WHERE Sort_Type__c = null];
for (CustomObject__c rec : recList) {
    rec.Sort_Type__c = 'Other';
}
update recList;
Dave The RaveDave The Rave
OK great, I will give it a try in the sandbox version
Dave The RaveDave The Rave
Unfortunately, I have got  an error messageError in Execution
Please tell me if the image is not clear, here is the code I entered:

List<RealEstateObject__c> recList = [SELECT Id, Sort_Type__c FROM RealEstateObject__c WHERE SortRealEstate__c = null];

for (RealEstateObject__c rec : recList) {

    rec.SortRealEstate__c = 'Other';

}

update recList;
AMIT AGRAWAL 5AMIT AGRAWAL 5
Run this code ...

List<RealEstateObject__c> recList = [SELECT Id, SortRealEstate__c FROM RealEstateObject__c WHERE SortRealEstate__c = NULL ];
for (RealEstateObject__c rec : recList) {
rec.SortRealEstate__c = 'Other';
}
update recList;
This was selected as the best answer
Dave The RaveDave The Rave
Maybe it is not the code, but my lack of knowledge, let's get back to basics, I did not create a new file. Under the menu "New" what should I choose?

Menu Options
AMIT AGRAWAL 5AMIT AGRAWAL 5
Hi Dave,

Choose 'Open Execute Anonymous Window' option under 'Debug'. Paste the code in opened window and click on run.

User-added image
Dave The RaveDave The Rave
Thanks Amit and Harpreet, the code is working.
Dave The RaveDave The Rave
Where can I learn more of this code?
Harpreet On CloudHarpreet On Cloud
Trailhead should be a good place to start. :-)