Codementor Events

How apex code helps us generate test data in Salesforce

Published Dec 21, 2018
How apex code helps us generate test data in Salesforce

We can definitely say that automation of everyday tasks helps Salesforce Users to save time and focus more of their efforts towards resolving customers’ issues. One of such examples of automation I’d like to share.

Some days ago my client asked me to generate some thousands of lead records with different data. I understood that I couldn't do it manually. So I wrote some code.

public static void createLeads() {

    List<String> firstNameList = new List<String>{

        'AARON','ABDUL','ABE','ABEL','ABRAHAM','ABRAM'

    };

    List<String> lastNameList = new List<String>{

        'JOHNSON','WILLIAMS','BROWN','JONES'

    };

    List<String> companyList = new List<String>{

        'Quantum ','BackRub ','Apple','Tokyo Tsushin Kogyo'

    };

    List<String> statusList = new List<String>{

        'Open - Not Contacted ','Working - Contacted'

    };

    List<Lead> leadList = new List<Lead>();

    for (Integer i = 0; i < 20; i++) {

        Lead newLead = new Lead();

        newLead.LastName = lastNameList.get(getRandomNumber(lastNameList.size()));

        newLead.FirstName = firstNameList.get(getRandomNumber(firstNameList.size()));

        newLead.Company = companyList.get(getRandomNumber(companyList.size()));

        newLead.Status = statusList.get(getRandomNumber(statusList.size()));

        newLead.Email = newLead.LastName + '.' + newLead.FirstName +'@gmail.com';

 

        leadList.add(newLead);

    }

    Database.insert(leadList, false);

}

 

public static Integer getRandomNumber(Integer size) {

    Double result = math.random() * size;

    return result.intValue();

}

createLeads();


And executed this code in developer console.

As a result, we get different lead records with different data.

612c0baec7409ed2269887a53607833a.png

Hope my experience will be useful for you. Thanks for reading.

P.S. What are your ideas about the task?

Discover and read more posts from Stanislau Yarashchuk
get started