0

I have something like this but I want more records of this object to be created to be used in my test class

public static Lead createLead(Account new Account)
{
    Lead newLead = new Lead();
    newLead.Account__c = newLead.Id;
    insert newLead;
    return newLead;
}
1

You can pass the number of records that needs to be created as a parameter to your method and can use that parameter and use for loop to create multiple records.

public static List<Legitimation__c> createLegitimation(Account newAccount, Integer numberOfRecords){
    List<Legitimation__c> listLegitimation = new List<Legitimation__c>();
    for(Integer i=0;i<numberOfRecords;i++){
        Legitimation__c newLegitimation = new Legitimation__c();
        newLegitimation.Account__c = newAccount.Id;
        listLegitimation.add(newLegitimation);
    }
    if(!listLegitimation.isEmpty()){
        insert listLegitimation;
     }
    return listLegitimation
}

Instead of inserting values in the test data factory itself, it would be better if you could return the list back so that you will still have an option to update the fields back in your test method without needing to use the update statement.

So let's say if you want to set another custom field's value in Legitimation__c instance which is needed for some of your testMethods and not all, in that case, you will call your createLegitimation method which will return the inserted instance, then you will have to set value of another field and then perform update operation on that instance as it is already inserted to database.

Below method just returns the instance without performing the insert operation, in this case, you can set the values of other fields which are needed and can directly perform the insert operation.

Note: Below method does not insert the instances to database, it only returns the instance of it, you will have to call this below method, make any other required changes to that instance and then perform insert operation in your testMethod

public static Legitimation__c createLegitimation(Account newAccount, Integer numberOfRecords){
    List<Legitimation__c> listLegitimation = new List<Legitimation__c>();
    for(Integer i=0;i<numberOfRecords;i++){
        Legitimation__c newLegitimation = new Legitimation__c();
        newLegitimation.Account__c = newAccount.Id;
        listLegitimation.add(newLegitimation);
    }
    return listLegitimation
}
|improve this answer|||||
  • Thanks but can you eloborate on this? what do you mean by without needing to use update statement – user3920709 Apr 26 '19 at 7:55
  • @user3920709 Updated the answer elaborating the same. – Vijay Ganji Apr 26 '19 at 8:15
  • Hi @user3920709, if this or any answer has solved your question please consider accepting it by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – Vijay Ganji May 2 '19 at 13:03
  • Done, thanks..I am new to the form, appreciate it – user3920709 May 2 '19 at 13:20
  • Yup yup I know.. it was pleasure helping you ! Just that its a good practice to close the question by accepting the answer if it helped. Although there is no obligation to do so. – Vijay Ganji May 2 '19 at 13:29
1

Vijay's answer with some different coding conventions:

private static Legitimation__c[] insertLegitimations(Id parentAccountId, Integer n) {

    Legitimation__c[] legitimations = new Legitimation__c[] {};
    for(Integer i = 0; i < n; i++){
        legitimations.add(new Legitimation__c(
            Account__c = parentAccountId
        ));
    }

    insert legitimations;

    return legitimations
}

If your code involves multiple objects, generally better to move this sort of code into a builder/fixture class that can create all the related objects.

|improve this answer|||||
  • Adding an assert after insert would be perfect. – Raul Apr 26 '19 at 7:40
  • Hi @Raul, I don't get why - can you explain more? Any DML error will throw an exception. – Keith C Apr 26 '19 at 8:15
  • A lot of time dml fails silently and we keep looking for a cause, adding an assert like this in a centralized place reduces hassle. – Raul Apr 26 '19 at 8:21
  • Hi @Raul, Not a problem I have experienced but fair point about centralizing assertions. – Keith C Apr 26 '19 at 10:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.