+ Start a Discussion
Travis MalleTravis Malle 

test.loaddata

Hello Community,
 
Looking for some assistance with loading test data via CSV files. I’m attempting to generate test data using the Test.LoadData method. In this circumstance, I’m loading, Users, Accounts and Campaigns. Users and Accounts load fine but when I attempt to load Campaign records, I get an error stating “Validation Errors While Saving Record(s)”. I have turned off all validation rules on the Campaign object and have taken my Campaign CSV data down to bare bones for experimentation purposes (inserting only one record with minimal fields).
 
The Campaign object has two Account lookup field and two user lookup fields. Although I have not been able to validate it, I’m thinking that the foreign id’s I’m using as references are only being considered directly after the insert. For example.
 
Step 1:
Load Users with numerically sequential ID’s i.e. 1,2,3,4,5,6. When loaded, the users are assigned actual 15 charter SF id’s .
 
Step 2:
Load Account data with numerically sequential ID’s i.e. 1,2,3,4,5,6. I also have a user lookup field that I have assigned via the foreign key (numerically sequential ID’s from step 1)
 
Step 3:
Load Campaign data and reference the users and Accounts via the assigned foreign id’s from step 1 & 2.
 
Step 3 will not work and throws a validation error described above. I am unable to get any further information from the logs and feel a bit stuck.
 
Anyone have an idea on the issue here?

Here is my code:
@isTest
public class TestUtilityClass {
    
    public static void CreateTestAccountsAndCampaigns() {
        list<SObject> dataList = new list<SObject>();
        dataList.addAll(Test.loadData(User.sObjectType,'TestData_User_MDO'));  
        dataList.addAll(Test.loadData(Account.sObjectType,'TestData_Accounts_Districts'));
        dataList.addAll(Test.loadData(Campaign.sObjectType,'TestData_Campaign_MD'));
    }        
}
 
@isTest
private class Test_OfficerStats_CampaignUpdate {
    
@testSetup static void setup() {
        id UserId = system.UserInfo.getUserId();
        user CurrentUser = [Select id from user where id = :UserId];
        
        system.runAs(currentUser){
            TestUtilityClass.CreateTestAccountsAndCampaigns();     
        }
        
    }
}
Here are my CSV files
User-added image
User-added image
User-added image

 
Best Answer chosen by Travis Malle
Travis MalleTravis Malle
Well, after much trial an error I was able to get this working. I thought I would pass along the knowledge for the next person who gets stuck on this... It was very frustrating
 
To get all data to load, I had to change the Id’s on each CSV file to a value that was not already used. To make it easy, I just increased by a factor of 10 on each load
 
Example:
User Id’s
10, 20, 30
 
Account Id’s
100, 200, 300
 
Campaign id’s
1000, 2000, 3000
 

All Answers

Saurabh Gupta ManrasSaurabh Gupta Manras

in order for external field to map correctly, the header of the external field should have the following syntax
fieldapiname__r:externalfieldapiname
try uploading data with this structure and it should work

Please choose it as the Best answer, if you find it helpful .
 

Travis MalleTravis Malle
Well, after much trial an error I was able to get this working. I thought I would pass along the knowledge for the next person who gets stuck on this... It was very frustrating
 
To get all data to load, I had to change the Id’s on each CSV file to a value that was not already used. To make it easy, I just increased by a factor of 10 on each load
 
Example:
User Id’s
10, 20, 30
 
Account Id’s
100, 200, 300
 
Campaign id’s
1000, 2000, 3000
 
This was selected as the best answer