2

I need help with updating existing bulk records just for the sake of firing a trigger.

Below is my situation:

We have more than 1 million contacts in our org. Recently we installed a managed package. Two objects in this managed package are used as related lists on Contact. I created a new trigger which on contact insert, it inserts a record in the related list object. This trigger is also fired on after update. The trigger is working for newly created contacts but for the existing old records, we have to click on edit and then hit save to fire the trigger. Is there a way to create an update scenario on all these bulk contact records to fire the trigger so that it populates/updates the related list object?

2 Answers 2

2

You can use the Apex Data Loader (you'll find it in Setup) to export all your records, then run that CSV file back through the Apex Data Loader for an update. This is probably the easiest solution, but it does require installing some software.


The alternative would be to write a batch class, but that requires a deployment to production. The batch class would look like this:

public class UpdateContacts implements Database.Batchable<sObject> {
  public Database.QueryLocator start(Database.BatchableContext context) {
    return Database.getQueryLocator([SELECT Id FROM Contact]);
  }
  public void execute(Database.BatchableContext context, Contact[] scope) {
    update scope;
  }
  public void finish(Database.BatchableContext context) {
  }
}

And the associated unit test would just be:

@isTest class UpdateContactTest {
  @isTest static void test() {
    insert new Contact(LastName='Test');
    Test.startTest();
    Database.executeBatch(new UpdateContacts());
    Test.stopTest();
    // Note: add a System.assert here to verify trigger ran.
  }
}

Finally, after deployment, you would just use the Developer Console to kick off an Execute Anonymous script:

Database.executeBatch(new UpdateContacts());
0

Just create a batch class that queries all the contacts and use an update dml on them. Doesn't sound like it is checking for changed field values so your execute class would look like this:

public static void execute(database.batchableContext bc, List<Contact> cons){
    update cons;
}
1
  • @sfdcfox and SFDC Noob, Thank you for the response. I will try that out.
    – Saha
    Jul 20, 2018 at 0:20

You must log in to answer this question.

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