1

i am new to Salesforce and i am trying to write a test class for code coverage to a trigger. Trigger's functionality is working fine, but everytime i try to run Test class the test coverage is showing as 0%. even i am not sure what i am writing is correct or not. can someone please help me on this.

Trigger

trigger ExecutorTrigger on Changes(after update){
    Changes ChangesObj = Trigger.New[0];
    List<Executor__c> ExecutorObj = new List<Executor__c>();
    List<Executor__c> ExecutorData = [select Id from Executor__c where ChangesExec_Id__c=:ChangesObj.id order by Name desc];
    if(ExecutorData.size()==0) {
        Executor__c Executor = new  Executor__c(ChangesExec_Id__c=ChangesObj.Id,ct.Start__c = System.now().date());
        ExecutorObj.add(Executor);
        insert ExecutorObj;
    } else {       
            Executor__c Executor = new  Executor__c(ChangesExec_Id__c=ChangesObj.Id);
            ExecutorObj.add(Executor);
            insert ExecutorObj;        
        }
      }
    }
}

Test Class

@isTest
private class ExecutorTest {
static testMethod void ExecutorTest() {
    Changes cNew =  [Select Id from Changes limit 1];
    Executor ct =  new Executor();
    ct.ChangesExec_Id__c= cNew.Id;
    ct.Start_Date__c = System.now().date();
    ct.End_Date__c = System.now().date();
    insert ct;
    Executor ctNew = [select Id from Executor where Id = :ct.Id];
    update ctNew;
}
}

Thanks, ravi.

3 Answers 3

1

I'm going to be honest, there are a lot of things wrong with the code you have posted.

Your trigger seems to be only dealing with the first object being updated, when in reality there could be more than one. Are you happy with the rest of them not being processed in the same way as the first?

As Karthik says in another answer, your test method is also never updating a Changes (should that be Changes__c?) object so the trigger would never get fired.

But, I'm going to bet that when you run the test, you're getting an error message along the lines of System.QueryException: List has no rows for assignment to SObject because from the code you have posted, you haven't set up your test data, so the SOQL query for the Changes object won't return anything.

Admittedly, that's making an assumption on my part that you're using an API version greater than 24.0, but it's just good practice to set up test data first.

I highly recommend you read the following resources before continuing:

Testing Best Practices

An Introduction to Apex Code Test Methods

What are Apex Unit Tests?

1

There is no standard object named(Changes) ,so your trigger never runs ,if changes is a custom object then use right api name for it.

Refer link for standard object :-https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_list.htm .

trigger ExecutorTrigger on Changes(after update){
    Changes ChangesObj = Trigger.New[0];
    List<Executor__c> ExecutorObj = new List<Executor__c>();
    List<Executor__c> ExecutorData = [select Id from Executor__c where ChangesExec_Id__c=:ChangesObj.id order by Name desc];
    if(ExecutorData.size()==0) {
        Executor__c Executor = new  Executor__c(ChangesExec_Id__c=ChangesObj.Id,ct.Start__c = System.now().date());
        ExecutorObj.add(Executor);
        insert ExecutorObj;
    } else {       
            Executor__c Executor = new  Executor__c(ChangesExec_Id__c=ChangesObj.Id);
            ExecutorObj.add(Executor);
            insert ExecutorObj;        
        }
      }
    }
}
0

It seems you have 2 objects, Changes and Executor. And the trigger you have written is on the Changes object for the "after update" event. So, you would need to update a "Changes" object record in order to get that trigger executed. However, in your test class you don't seem to update the Changes record. Instead you are updating Executor record.

Try to insert a Changes record and update it. Your trigger should get fired.

Your test class would look like

@isTest
private class ExecutorTest {
static testMethod void ExecutorTest() {
    Changes cNew =  new Changes();
    /*
      Assign the mandatory values for the cNew record here like
      cNew.name = 'Sample Record';
    */
    insert cNew;
    // Do some changes on the cNew record, so that you can update the record.
    cNew.name = 'Sample Record Modified';
    update cNew; // For this statement your trigger should get executed.
}
}

Make sure that you reference the custom objects by appending __c to their names. For example Changes__c and Executor__c as those are custom objects.

You must log in to answer this question.

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