function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
VictorBVVictorBV 

Error testing

Hi all,

 

I am testing some of my triggers and I realized that real data from salesforce can be used in testing.

Can I somehow avoid that?

I want to avoid using real data because some of my tests fail because uniques key, and I want just to test Insert data, without having to check first which key is not being used.

 

How can I do it?

 

Thanks in advance

Best Answer chosen by Admin (Salesforce Developers) 
Jeremy.NottinghJeremy.Nottingh

The option also exists to take existing Salesforce data and modify it to suit your testing purposes. This includes deleting it. Would this work for you?

 

 

Account oldAcc = [select Account_Code__c from Account limit 1];
String OldAccCode = oldAcc.Account_Code__c;

delete oldAcc;

Account NewTestAcc = new Account (Account_Code__c = OldAccCode);

test.StartTest();
   insert newTestAcc;
test.StopTest();

 

Jeremy

 

 

 

All Answers

MaxiMizeMaxiMize

Have you considered entering test data through your test case?  That would allow you to generate the test data you need, without cluttering your org.  It would also allow you to be sure you don't overlap any unique ID's.

 

Just a thought...

 

An example of this would be:

 

 

User u1 = new User(alias = '1test123', email='1test1234@test.com', emailencodingkey='UTF-8', lastname='1Testing',
        languagelocalekey='en_US', localesidkey='en_US', profileid=p.Id, country='United States', timezonesidkey='America/New_York',
        username='1test12345@test.com');
        insert u1;

 In the above, I've created a test user that will later be used in a RunAs scenario.  Can also do the same for Account, or Contracts, or... Well, you get the idea.

 

 

 

VictorBVVictorBV

Thanks MaxiMize but I already knew that,and the problem I am talking about is related to that.

 

I explain my situation and maybe someone can help me.

I have created a new field in Account object, this field is called AccountCode and is unique.

After some time using salesforce, we already have quite a lot of Accounts stored in salesforce database and we wanted to create a trigger to do something when a new Account is inserted.

Well, I developed that trigger and in order to test it I need a test class and here is the problem. In order to make the test I have to create several Accounts and when creating I am having great problems since if I use for those objects an AccountCode which is already in used, then test fails. I have been trying different AccountCodes and finally it works, but I could not test bulk insertion.

 

So I want salesforce to not take into account real data when testing so I can use any AccountCode.

Is it that possible?

 

Thanks in advance

Jeremy.NottinghJeremy.Nottingh

The option also exists to take existing Salesforce data and modify it to suit your testing purposes. This includes deleting it. Would this work for you?

 

 

Account oldAcc = [select Account_Code__c from Account limit 1];
String OldAccCode = oldAcc.Account_Code__c;

delete oldAcc;

Account NewTestAcc = new Account (Account_Code__c = OldAccCode);

test.StartTest();
   insert newTestAcc;
test.StopTest();

 

Jeremy

 

 

 

This was selected as the best answer
VictorBVVictorBV

Thanks Jeremy, that could be one solution.

 

Before inserting any test data, delete the possible Salesforce data.

 

But I thought there would be a general way to do it, I am afraid there is not.