1

Hi i am a newbie to salesforce. I am trying to learn a easy way to do data sweep on some text fields and replace them with another value in production environment. As far as i know data sweep can be done using a data loader. export the data and import it again. If i want to do it using apex code or any other way instead of using the data loader, is there any other alternative available. I am searching for this information to learn and if some could point me in the right direction or give guidance. it will be helpful.

Thanks in Advance

4
  • 1
    Database.Batchable interface and Apex Mar 14, 2016 at 20:59
  • Hi Jesse, Since i am learning. i have never had access to a real production environment. I have my developers org. So if i understand you right. i can write an apex class and execute it once anonymously from the developer console of a prod environment. i am sorry if i am making wrong statements.
    – Dino
    Mar 14, 2016 at 21:05
  • 1
    Nope, you are exactly right, Adrian has a good starter example below. There is a module on Trailhead I would suggest doing if you are kinda new to the platform. developer.salesforce.com/trailhead/asynchronous_apex/… Mar 14, 2016 at 21:06
  • Thank you Jesse. i will look into that trail-head challenge. thanks for your help.
    – Dino
    Mar 14, 2016 at 21:10

2 Answers 2

0

It sounds like you are looking for Batch Apex. Read up on Using Batch Apex. This touches on a lot of topics and is too broad to provide a comprehensive answer to here. The basic structure is something like:

Service Layer

public with sharing class MyService
{
    public static void setSomeTextField(List<MyObject__c> records)
    {
        for (MyObject__c record : records) // do something
        try
        {
            update records;
        }
        catch (DmlException dmx)
        {
            // determine appropriate error handling scheme
        }
    }
}

Batch Implementation

public with sharing class MyBatch implements Database.Batchable<SObject>
{
    public Database.QueryLocator start(Database.BatchableContext bc)
    {
        return Database.getQueryLocator([
            SELECT ... FROM MyObject__c WHERE ...
        ]);
    }
    public void execute(Database.BatchableContext bc, List<SObject> scope)
    {
        MyService.setSomeTextField((List<MyObject__c>)scope);
    }
    public void finish(Database.BatchableContext bc)
    {
        // optional additional logic here
    }
}
3
  • Thank you Adrian. I will try doing it through the database.batchable. Like jesse said i will also complete the trail-head challenge to get hands on exercise.
    – Dino
    Mar 14, 2016 at 21:11
  • @Dino Yes, you can learn a lot from trailhead. It's a great resource!
    – Adrian Larson
    Mar 14, 2016 at 21:11
  • Yes. I have just started using thee trailhead and i am still in the beginner level. I guess i have to catch up soon to the intermediate and advanced levels.
    – Dino
    Mar 14, 2016 at 21:13
2

One technique (to avoid governor limits) is to run code like this via excute anonymous:

Contact[] contacts = [select Id from Contact where Field__c = 'old value' limit 5000];
for (Contact c : contacts) {
    c.Field__c = 'new value';
}
update contacts;

and keep repeating until all the data is processed. Works fine for say 100,000 records (just run 20 times) but a batchable is the way to go for large numbers of records.

Not the answer you're looking for? Browse other questions tagged .