Activity Forums Salesforce® Discussions In what condition we apply the soql query in test class?

  • Ravi

    Member
    July 28, 2016 at 8:02 am

    Hi Mohit,
    First scenario where we use SOQL query is to retrive some organization data from salesforce database. In this case you have to make your test class all data visible using (SeeAllData = true). But using this is not recommended since your yout test class may not work in other org.

    // All test methods in this class can access all data.
    @isTest(SeeAllData=true)
    public class TestDataAccessClass {
    // This test accesses an existing account.
    static testmethod void myTestMethod1() {
    // Query an existing account in the organization.
    Account a = [SELECT Id, Name FROM Account WHERE Name='Acme' LIMIT 1];
    System.assert(a != null);
    }

    Other scenario where we use SOQL query is to verify the inserted/updated record.

    public class TestDataAccessClass {

    // This test creates and accesses a new test account.
    static testmethod void myTestMethod1() {
    // Create a test account .
    Account testAccount = new Account();
    testAccount.Name = 'Acme Test';
    insert testAccount;
    // Query the test account that was inserted.
    Account testAccount2 = [SELECT Id, Name FROM Account
    WHERE Name='Acme Test' LIMIT 1];
    System.assert(testAccount2 != null);
    }

    }

    • This reply was modified 8 years, 4 months ago by  Ravi.

Log In to reply.

Popular Salesforce Blogs

Salesforce Marketing Cloud Integration with Google Analytics 360

Redefining the future of Marketing – Salesforce Marketing Cloud Integration with Google Analytics 360

Blog in

Know your customers better and make smarter, insight-based marketing decisions through Salesforce’s massive customer relationship management platform twinned with Google Analytics 360’s market-leading digital analytics solution…

Popular Salesforce Videos

Salesforce Integration Tutorial | Integrate Salesforce with Apps

Video in

This "𝐒𝐚𝐥𝐞𝐬𝐟𝐨𝐫𝐜𝐞 𝐈𝐧𝐭𝐞𝐠𝐫𝐚𝐭𝐢𝐨𝐧 𝐓𝐮𝐭𝐨𝐫𝐢𝐚𝐥" video by Edureka will help you understand what is Salesforce integration in detail. It will also address the various Salesforce integration…