Pranay
Pranay

Reputation: 13

Record created in test class is not visible in controller

I am creating a test class for a controller and i dont know for what reason a record created for a object is not visible in a method in controller.That method is having a select query which is giving the blank output. If i run the same query in test class , that is giving me the output. Rest of the records for different objects are also visible and working fine.

Is there any specific reason why the record is not available in a method in a controller??

Upvotes: 0

Views: 2487

Answers (1)

K Nandeeswara Raju
K Nandeeswara Raju

Reputation: 51

Check 1:

It might be some access issue. So check the Object access in the Profile level.

If you are a System Admin, then try to create records by running as System Admin user.

User adminUser = [SELECT Id,Name FROM User WHERE Id =: UserInfo.getUserId()];
System.runAs(adminUser){
    //Test Class block
}

Check 2:

If it is happening in a Trigger or Trigger_handler related Test Class. Then please see the below scenario.

Ex: Assume, you are working on Order and OrderItem objects. You have a trigger on Order object (after insert or after update).

To create any OrderItem, you first need the Order to be created. So you will insert an Order first and then OrderItem follows.

Since we have a trigger on Order object, the trigger will start to execute as soon as we insert the Order record. In trigger we may have used the OrderItem records also. But they will not be available in Trigger yet.

Because, we have only created the Order record so far and the trigger started executing immediately and we are yet to create the OrderItem.

So, try the below approach:

Create/Insert an Order.
Create OrderItems by using the above Order id.
Now just do a simple update on Order record and see if it works.

Upvotes: 0

Related Questions