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 

Testing User creation on Insert and Update

Hello,

 

I have to develope a trigger that creates a User when a contact is inserted or when a contact is updated (and insertion or update match certain criteria).

I've already done, but I have no idea how to test it.

 

I explain the problem.

 

In order to create that trigger I had to use a @future method, which executes asynchornous(due to DML on Setup sObjects limitation).

In order to test that trigger I had to insert several contacts and check if result is right according to it was expected, but if I want user to be created as the time I checked the result, those sentences must be between Test.startTest() and Test.stopTest(). No problem so far, I could check insert.

 

The problem is checking update.

In order to update data, I need data to be inserted so I have to put insert between startTest and stopTest.

But in order to check User creation on contact update, I need to put updates sentences between startTest and stopTest.....but, and here is the problem, in a test only one startTest can be used.

 

How can I solve it?

 

Thans very much.

 

Best Answer chosen by Admin (Salesforce Developers) 
d3developerd3developer

Victor,

 

if that is the only problem why not use an existing user when testing the update scenario? As JN says, put that in a separate testMethod.

All Answers

Jeremy.NottinghJeremy.Nottingh

First:

You can have several testmethods for one Trigger, for separate cases. For you, maybe you want to have a testmethod for insert, and one for update. I'm not sure, but maybe this is your solution.

 

Second:

On the update testmethod, you don't need to insert the record inside your start/stoptest area.Do your inserting, then test.starttest(), and do your updating.

 

Does that make sense?

Jeremy

VictorBVVictorBV

Thanks Jeremy for your answer

 

About First:

- Yes I can, but that does not solve the problem. How can I test update if I dont have any data to update? So first... in the method I have to insert.

 

About Second:

- Insert must be inside start/stop since it will create a user than it will be modified with update. As user creation is asynchronous so it has to be in start/stop to be sure that has been performed at the time data is going to be updated.

 

Maybe I haven't understood your point of view.

d3developerd3developer

Victor,

 

if that is the only problem why not use an existing user when testing the update scenario? As JN says, put that in a separate testMethod.

This was selected as the best answer
VictorBVVictorBV

Thanks d3developer,

 

That makes testing depending on existing users, which is not the best. But I guess it is the only one solution.

 

Thanks again.