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
Sombir SheoranSombir Sheoran 

Testing TestClasses, for Trigger on Account, I cant find any records created ? How to verify that Test class was executed successfully and it there any method to verify records are created in database or application ?

Hello Everyone,

I have created 1 trigger on Account which opens a case as soon as we create the account.
So, as soon as I have written both the codes and now testing with testClass, it is not creating a new Account record in my salesforce org. and hence also not opening any case.

Is there anything I am missing here. Support is really appreciated.

1. Trigger on Account to open a case 
trigger CreateTShirtCaseonAccount on Account (after insert) {
    for(Account ListofAllAccount: Trigger.New)
    {
        Case newTshirtCase = new Case();
        newTshirtCase.Subject = 'Congratulations, your account is opened, with free T-Shirt from us';
        newTshirtCase.Priority = 'High';
        newTshirtCase.AccountId = ListofAllAccount.id;
        insert newTshirtCase;
    }
}


2. Test Class to test the trigger

@isTest
public class TestAccountTrigger {
    static testMethod void accountCreator()
    {
        Account newAccount = new Account();
        newAccount.Name = 'Account Created with TestClass : TestAccountTrigger';
        insert newAccount;
    }
}



Regards,
Sombir Sheoran
Best Answer chosen by Sombir Sheoran
D-CoderD-Coder
Test data created in test class never gets commited to data base. It is for testing purpose only and to validate your apex code.

My code is to create a case when Account gets inserted. You can create an Account manually under Accounts Tab ==> New and see case is created for it.


If this answers your query please mark this question as a solved so that it can be filtered out from unsolved questions.

All Answers

D-CoderD-Coder
Hi Sombir Sheoran,

Case required fields are missing in your trigger. Try below trigger.
 
trigger CreateTShirtCaseonAccount on Account (after insert) {
    for(Account ListofAllAccount: Trigger.New)
    {
        Case newTshirtCase = new Case();
        newTshirtCase.Subject = 'Congratulations, your account is opened, with free T-Shirt from us';
        newTshirtCase.Priority = 'High';
        newTshirtCase.AccountId = ListofAllAccount.id;
        //Required Fields below
		newTshirtCase.Status = 'New';
		newTshirtCase.Origin = 'Phone';

        insert newTshirtCase;
    }
}
If this answers your query please mark this question as a solved so that it can be filtered out from unsolved questions.
 
Sombir SheoranSombir Sheoran

Hello, $C0RP!AN K!NG .

I tried to change trigger as to add 2 new required fields as well, however, I can not see any new Account or Case being created.
Is there anything I am missing further.

Appreciate your quick response.

Regards,

Sombir Sheoran

D-CoderD-Coder
Test data created in test class never gets commited to data base. It is for testing purpose only and to validate your apex code.

My code is to create a case when Account gets inserted. You can create an Account manually under Accounts Tab ==> New and see case is created for it.


If this answers your query please mark this question as a solved so that it can be filtered out from unsolved questions.
This was selected as the best answer
Sombir SheoranSombir Sheoran
Hello, $C0RP!AN K!NG,

Thank you for your response.
In that case, how do we know whether our trigger/class was executed successfully or not?

Also, how can we know how much percentage of our class/trigger code has been executed in order to complete at least 75% for taking into Prod.

Regards,
Sombir Sheoran

 
D-CoderD-Coder
Go to test class ==> Click Run Test ==> Once test execution is completed go back to trigger ==> check code covered percentage in "Code Coverage"

You can execute tests from developer console as well.

More details can be found here -
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_unit_tests_running.htm
https://help.salesforce.com/articleView?id=code_test_execution.htm&type=0

If this answers your query please mark this question as a solved so that it can be filtered out from unsolved questions.
Sombir SheoranSombir Sheoran
Hello, $C0RP!AN K!NG,

Glad to see your response that says below and hence marking the question as answered.
Appreciate your inputs here in order to clarify my concerns.
  • Test data created in test class never gets committed to the database. It is for testing purpose only and to validate your Apex code.
  • You can create an Account manually under Accounts Tab ==> New and see the case is created for it.
  • More details can be found here -
    https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_unit_tests_running.htm
    https://help.salesforce.com/articleView?id=code_test_execution.htm&type=0


Regards,

Sombir Sheoran