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
Will Sun 14Will Sun 14 

Apex Class deployment from Sandbox to Production help

Hi developers, I need some helps to deploy apex class from Sandbox to Production.. here is my apex class
 
public class LightningDataTableController {
    @AuraEnabled
    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 
Best Answer chosen by Will Sun 14
AnudeepAnudeep (Salesforce Developers) 
Hi Will, 

Let me know if this code gives you any coverage
 
@isTest
private class LightningDataTableControllerTest
{
    @isTest
    static void TestDataTable()
    {   

    Account account = new Account();
    account.Name = 'test corp';
    insert account;

    //Insert contact for Account

    Contact c = new Contact();
    c.AccountId = account.ID;
    c.FirstName = 'test';
    c.LastName = 'test';
    c.email = 'test@test.com';
    insert c;
    
    LightningDataTableController.fetchData();

    }
}

Anudeep
 

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Will,

You could start by looking at the trailheads and write a test class then call the method in the test class.

Then you can improve the code coverage and deploy the test class to production.

You could make use of following links for reference:

>> https://trailhead.salesforce.com/en/content/learn/modules/apex_testing

>> https://www.sfdcpoint.com/salesforce/test-class-with-example-salesforce/

Apart from these make sure to follow the best practices so that the test class doesn't break in case if there is any changes going forward.

>> https://www.infallibletechie.com/2018/08/salesfore-apex-test-class-best-pracitces.html

I hope these links would help.

Regards,
Anutej
Will Sun 14Will Sun 14
Hi Anuteju, thank you for your reply, I have checked out the reference you have provided, but still not sure what to do, I have tried to create the test class, but still not work... for your expertise, what should I do? 
As these fields are formular fields in the object
Product_Category__c, Product_Name__c,ProductCode,Inventory_Status__c,Inventory_Description__c
AnudeepAnudeep (Salesforce Developers) 
Hi Will, 

Let me know if this code gives you any coverage
 
@isTest
private class LightningDataTableControllerTest
{
    @isTest
    static void TestDataTable()
    {   

    Account account = new Account();
    account.Name = 'test corp';
    insert account;

    //Insert contact for Account

    Contact c = new Contact();
    c.AccountId = account.ID;
    c.FirstName = 'test';
    c.LastName = 'test';
    c.email = 'test@test.com';
    insert c;
    
    LightningDataTableController.fetchData();

    }
}

Anudeep
 
This was selected as the best answer
Will Sun 14Will Sun 14
Hi Anudeep, thank you so much for your help