I need some helps to deploy apex class from Sandbox to Production.. here is my apex class

public class LightningDataTableController {
public static List<PricebookEntry> fetchData() {
    //Query and return list of Contacts
    id userId = UserInfo.getUserId();
    User u = [select id, contactId from User where id = : userId];
    id getContactId = u.contactId;
    List<Contact> cont = new List<Contact>();
    cont = [SELECT AccountId, Title, Name, Email FROM Contact Where Id =: getContactId];
    String dealerId = '';
    if(cont != null && cont.size() > 0){
        dealerId = cont[0].AccountId;
    List<PricebookEntry> objRecords = [SELECT Product_Category__c, Product_Name__c,ProductCode,UnitPrice,Inventory_Status__c,Inventory_Description__c from PricebookEntry  where isActive =true AND Pricebook2.isStandard = false AND Pricebook2.Dealer__c =: dealerId LIMIT 900];
    return objRecords;

I understand that I need to create a test class, but don't know where to start with and not sure how to pass the production Apex test.. Please help

1 Answer 1


In order to deploy to production you must have a test class that when run covers 75% of your code, i.e. 75% of your code must be executed by this test.

A test class in Apex is another Apex class but marked with @isTest. The typical structure of a test is:

private class LightningDataTableController_Test {
    static void makeData() {
         //Create your test data here. For you this would be inserting Contact, User and PriceBookEntry as well as any data required to set them up

    private static void testPositiveCase() {
        //Test the positive use case for your code

    private static void testNegativeCase() {
        //Test the negative use case for your code

Because of the way your code is structure to use the running user, a recommendation for your positive case would be to retrieve the user record you create and call System.runAs passing in that user. This will run the code as that user for you.

Have a look at this Trailhead module for more information on writing unit tests.

  • After several attempts, still hit the roadblock...
    – Will
    Commented Jun 22, 2020 at 11:33
  • 1
    @Will You haven't given us enough information to help you. You're saying there's a roadblock, but you haven't said what that is. At minimum, we would need the unit test you've written, the code coverage you have (e.g. which lines need coverage, if any), and the errors you're getting. Please read about How to Ask.
    – sfdcfox
    Commented Jun 22, 2020 at 12:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .