I am new to salesforce and i am writing a test class for the trigger. Can any one tell me how to write a test class for the trigger below

trigger CloseParentCommunication on BuyerSellerCommunication__c (before insert,after insert,before update,after update) {

    List<ID> parentCommId = New List<ID>();

    for(seit__BuyerSellerCommunication__c communication:Trigger.new)

        if(communication.seit__Status__c=='Closed' && communication.seit__Parent_Communication__c!=Null)

    List<seit__BuyerSellerCommunication__c> parentCommList = [SELECT id,name,status__c FROM seit__BuyerSellerCommunication__c WHERE id in :parentCommId];
    for(integer i = 0 ; i < parentCommList .size(); i++){
        parentCommList[i].status__c = 'Closed';
        update parentCommList[i];

    update parentCommList;

2 Answers 2


So I don't want to write test class for your trigger but I can guide you

private class Test_CloseParentCommunication  {
    static testMethod void testCloseParentCommunication() {
       // first insert your seit__BuyerSellerCommunication__c records here.. with all required values like name, status__c etc

       // now create seit__BuyerSellerCommunication__c  records with required fields. Perform insert DML that will fire the trigger
      // If all the condition satisfies in trigger 
      // like  seit__BuyerSellerCommunication__c.seit__Status__c  = 'closed'
      // then update parent record ie.e seit__BuyerSellerCommunication__c  status update 
       // put system.assert to check parent status changed or not
      // perform update operation and update status field  
      // after update put system.assert to check parent status changed or not

The basic premise is that you need to create & manipulate test data in order to have your trigger run, then assert that the logic has acted correctly.

Here is an excellent article on testing trigger logic.

When deploying your Apex development from a sandbox to a production environment (Apex trigger, Apex class, etc.), Salesforce requires 75% code coverage, and the execution of that code must not incur any development errors. This article will show how to develop an Apex test class to cover your Apex development, and ensure the code performs correctly. Specifically, this article will show how to:

  1. Develop an Apex trigger that will run itself during the creation and modification of a Case record.

  2. Create a test class to test the trigger code.

  3. Test a web service call and simulate its response.

So in your scenario you will need to insert / update some BuyerSellerCommunication__c records in order to match your trigger criteria, then requery the dataset to ensure the trigger has run & set the Status__c field accordingly.

Also, as you're new to Salesforce it would be a good idea to read up on the basics and implement a nice framework from the get-go. Here is my preferred approach.

You must log in to answer this question.

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