1

I have the following Apex Class that I created in a developer instance. When I execute anonymous window it creates random dummy data. But when I add it to a Sandbox refresh or creation, it seems to not trigger. Additionally, in my Orgs Sandbox tied to Production, when I execute anonymous window it also doesnt create records which Im not sure why if it worked in my developer instance. Any guidance is much appreciated.

public class GenerateSampleDataBatch implements Database.Batchable<sObject>, SandboxPostCopy {

public void runApexClass(SandboxContext context) {
    Database.executeBatch(this);
}

public Database.QueryLocator start(Database.BatchableContext bc) {
    return Database.getQueryLocator([SELECT Id FROM Account LIMIT 1]);
}

public void execute(Database.BatchableContext bc, List<sObject> scope) {
    List<Account> accountsToInsert = new List<Account>();
    List<Contact> contactsToInsert = new List<Contact>();
    
    String[] accountNames = new String[] {
        'Devbug','Oyoba','Realblab','Skyvu','Quamba','Mybuzz','Skinix','Buzzbean',
        'Blogtag','Wikibox','Quire','Blognation','Cogidoo','Ntag','Jayo','Tagchat',
        'Skyble','Jaloo','Zoovu','Brainbox','Pixonyx','Leenti','Geba','Skalith',
        'Browsebug','Skipfire','Digitube','Babbleset','Skiptube','Aimbu','Eayo',
        'Dablist','Fivespan','Mydo','Centizu','Voonte','Zoonoodle','Centimia',
        'Eamia','InnoZ','Oba','Rhycero','Voonix','Dablist TX','Topicshots','Babbleblab','Voonder'
    };
    
    String[] contactNames = new String[] {
        'Trevion Forman', 'Danika Tejeda', 'Kenyon Burk', 'Isai Valencia', 'Truman Raymond',
        'Molly Deloach', 'Izaiah Herrick', 'Kristal Pryor', 'Jarvis Bledsoe', 'Christianna Escamilla',
        'Gerald Fields', 'Lincoln Holcomb', 'Brant Platt', 'Josue Green', 'Karina Zhu',
        'Kaeli Pagan', 'Jamya Schilling', 'Hallie Griffiths', 'Mara Swisher', 'Hugh Wilcox'
    };
    
    String[] emailDomains = new String[] {
        '@gmail.com', '@swantide.com', '@hotmail.com', '@Aol.com', '@Outlook.com', '@Teams.com', '@mail.com', '@icloud.com'
    };

    for (Integer i = 0; i < 100; i++) {
        Integer randomIndex = Math.mod(Math.abs(Crypto.getRandomInteger()), accountNames.size());
        String randomName = accountNames[randomIndex];
        
        Account acc = new Account();
        acc.Name = randomName;
        acc.Industry = 'Technology';
        acc.Phone = '123-456-7890';
        accountsToInsert.add(acc);
    }
    
    insert accountsToInsert;
    
    for (Account acc : accountsToInsert) {
        for (Integer j = 0; j < 5; j++) {
            Integer randomNameIndex = Math.mod(Math.abs(Crypto.getRandomInteger()), contactNames.size());
            String randomContactName = contactNames[randomNameIndex];
            String[] nameParts = randomContactName.split(' ');
            
            Integer randomDomainIndex = Math.mod(Math.abs(Crypto.getRandomInteger()), emailDomains.size());
            String randomDomain = emailDomains[randomDomainIndex];
            
            Integer randomNum = Math.mod(Math.abs(Crypto.getRandomInteger()), 10000);
            
            Contact con = new Contact();
            con.FirstName = nameParts[0];
            con.LastName = nameParts[1];
            con.Email = nameParts[0].toLowerCase() + randomNum + randomDomain;
            con.AccountId = acc.Id;
            contactsToInsert.add(con);
        }
    }
    
    insert contactsToInsert;
}

public void finish(Database.BatchableContext bc) {
    System.debug('Batch execution finished.');
}

}

2
  • 2
    why would there be any Accounts in the sandbox (depending on sandbox type)? Then the batchable start will return no records. IN fact, I can't see any reason why you. are using batchable at all
    – cropredy
    Sep 20, 2023 at 23:44
  • Got it, I was wondering about that where it states public Database.QueryLocator start(Database.BatchableContext bc) { return Database.getQueryLocator([SELECT Id FROM Account LIMIT 1]); } What would be your recommendation? I just got this idea from Salesforce Ben ;( apologies for the noobness
    – andrew
    Sep 21, 2023 at 0:00

1 Answer 1

3

Nothing is inserted because the sandbox has no Accounts and the start() method returns 0 rows so the execute() never runs

I would just get rid of the whole batchable altogether as you're not creating that many objects (100 accounts, 500 contacts, 5 per). If you run into CPU limits (lots of Flows), you could shift to chained queueables or have the start() iterate over a list of Strings rather than a list of database objects

public class GenerateSampleDataBatch implements Database.Batchable<sObject>, SandboxPostCopy {

  public void runApexClass(SandboxContext context) {

  List<Account> accountsToInsert = new List<Account>();
  List<Contact> contactsToInsert = new List<Contact>();

  String[] accountNames = new String[] {'Devbug',...};    
  String[] contactNames = new String[] {'Trevion Forman', ...};
  String[] emailDomains = new String[] {'@gmail.com', ...};

  for (Integer i = 0; i < 100; i++) {
    Integer randomIndex = Math.mod(Math.abs(Crypto.getRandomInteger()), accountNames.size());
    String randomName = accountNames[randomIndex];
        
    accountsToInsert.add(new Account(
        Name = random,Name,
        Industry = 'Technology',
        Phone = '123-456-7890' 
        ));
  }
  insert accountsToInsert;

  for (Account acc : accountsToInsert) {
    for (Integer j = 0; j < 5; j++) {
        Integer randomNameIndex = Math.mod(Math.abs(Crypto.getRandomInteger()), contactNames.size());
        String randomContactName = contactNames[randomNameIndex];
        String[] nameParts = randomContactName.split(' ');
        
        Integer randomDomainIndex = Math.mod(Math.abs(Crypto.getRandomInteger()), emailDomains.size());
        String randomDomain = emailDomains[randomDomainIndex];
        
        Integer randomNum = Math.mod(Math.abs(Crypto.getRandomInteger()), 10000);

        contactsToInsert.add(new Contact(
           AccountId = acc.Id,
           Email = nameParts[0].toLowerCase() + randomNum + randomDomain,
           FirstName = nameParts[0],
           LastName = nameParts[1], 
           ));
    }
  }

  insert contactsToInsert;
  }
}

You must log in to answer this question.

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