0

I am having trouble writing a test class for this trigger:

trigger backToIdea on Idea_Lobby__c (after update) {
for (Idea_Lobby__c yourIdea : Trigger.new) {

if (yourIdea.Approval__c == 'Approved') 
{
        Idea backAgain      = new Idea(); //Creates a new Idea entry
        backAgain.Name__c   = yourIdea.Name__c; //Maps Name from Idea to Idea Lobby
        backAgain.Location__c = yourIdea.Location__c; //Maps Location from Idea to Idea Lobby
        backAgain.Company__c = yourIdea.Company__c; //Maps Company from Idea to Idea Lobby
        backAgain.Title = yourIdea.Name; //Maps Title from Idea to Idea Lobby
        backAgain.Body = yourIdea.Idea_Body__c; //Maps Body from Idea to Idea Lobby
        backAgain.CommunityId = '09ao00000008Hy8'; //Assigns the record to Internal
        backAgain.Approval__c = 'Approved'; //Sets Approval to Approved
        insert backAgain;
}
else if(yourIdea.Approval__c == 'Delete')
{
  Idea_Lobby__c y = new Idea_Lobby__c(); //All this deletes the record
        y.Id = yourIdea.Id;
        delete y;
        }
}
}

I have been playing with different test classes, but haven't found one to generate any code coverage. Here is an example:

@isTest 
public class backToIdeaTest{
    static testMethod void approvedDelete() {

    Idea_Lobby__c change = new Idea_Lobby__c();

    change.Approval__c = 'Approved';
    change.Name = 'Test';

    change.Name__c = 'Submitter';
    change.Location__c = 'Test Location';
    change.Company__c = 'None';


    insert change;
    }
    static testMethod void approvedDeleted() {

    Idea_LObby__c change = new Idea_Lobby__c();

    change.Approval__c = 'Delete';
    change.Name = 'Test';

    change.Name__c = 'Submitter';
    change.Location__c = 'Test Location';
    change.Company__c = 'None';


    insert change;
    }
    }

Any ideas? Any and all help is much appreciated!

Thanks guys!

  • You seem to be inserting, not updating, why would that enter an after update trigger? – George S. Sep 25 '14 at 17:29
  • @highfive After updating the "Approval" field in Ideas, it will insert a copy of the record into a custom object "Idea Lobby". – DevelopHawaii Sep 25 '14 at 18:14
2

First of all, let me point out that you are making DML operation inside of a loop, and that is not recommended. There is a limit of DML operations (150 DLMs I think). So if your trigger is fired by more than 150 records, your trigger will not work at all. Instead of using DML operations inside of the for loop, you can use lists as follows :

trigger backToIdea on Idea_Lobby__c (after update) {

    List<Idea_Lobby__c> lst_idealobbiesTodelete = new List<Idea_Lobby__c>();
    List<Idea> lst_ideasToInsert = new List<Idea>();

    for (Idea_Lobby__c yourIdea : Trigger.new) {

        if (yourIdea.Approval__c == 'Approved') {
            Idea backAgain = new Idea (
                Name__c     = yourIdea.Name__c,
                Location__c = yourIdea.Location__c,
                Company__c  = yourIdea.Company__c,
                Title       = yourIdea.Name,
                Body        = yourIdea.Idea_Body__c,
                CommunityId = '09ao00000008Hy8',
                Approval__c = 'Approved'
            );
            lst_ideasToInsert.add(backAgain);
        }

        else if(yourIdea.Approval__c == 'Delete') {
            lst_idealobbiesTodelete.add(yourIdea);
        }
    }

    // Insert the accepted Idea_Lobby__c records in one shot
    insert lst_ideasToInsert;

    // Delete the rejected Idea_Lobby__c records in one shot
    delete lst_idealobbiesTodelete;
}

Now that your trigger doest not reach any limit in a bulk test case let's take a look at your test. What you need do is pretty simple. You have to create an Idea_Lobby__c record and then update it, using explicitly the update DML keyword. Also, note that you are not using the keyword @isTest in front of the test cases. For that reason you're not getting any test coverage. Here is a example of a valid test case :

@isTest 
public class backToIdeaTest{

    @isTest static testMethod void approvedDelete() {

        Idea_Lobby__c change = new Idea_Lobby__c(
            change.Approval__c = 'Approved',
            change.Name = 'Test'
        );

        insert change;
        change.Name = 'New Name';

        // The following update operation will fire your trigger
        update change;

        // You need to assert your result, this is important, otherwise you're not testing anything!
        Integer expectedNumberOfIdeas = [SELECT COUNT() FROM Idea WHERE Title = :change.Name];
        System.assertEquals (expectedNumberOfIdeas, 1)
    }
}
1

The testmethods can be simplified and somewhat generalized to the following. This tests the bulkification of your trigger; is easy to extend with additional test cases and is very transparent..

It also does asserts to make sure that your code keeps working after time passes and changes are made here or perhaps elsewhere. Code coverage by itself is not best practice.

@istest
private static testIdeaLobby {
  @isTest
  private static void testIdeaLobbyCrud() {
    List<Idea_Lobby__c> ilList = new List<Idea_Lobby__c> {
                                   new Idea_Lobby__c( name='00', name__c ='00IdeaLobbyToApprove'),
                                   new Idea_Lobby__c( name='01', name__c = '01IdeaLobbyToDelete')
                                           };
    insert ilList;
    iList[0].approval__c = 'Approved';
    iList[1].approval__c = 'Delete';
    update ilList;  // tests after update trigger

    System.assertEquals(0, [select count() from Idea_Lobby where id = "ilList[1].id]);  // sb deleted
    List<Idea> ideaList = [select id, name__c /* add other flds to verify */ 
                                      from Idea order by Name__c asc];
    System.assertEquals(1,ideaList.size());   // verify one Idea added
    System.assertEquals(ideaList[0].name__c,ilList[0].name__c);  // verify name copied over
    // other asserts to test other field transformations/copies
  }

}
0

You trigger run on update event. A single line need ti be added. update dml keyword.

@isTest 
public class backToIdeaTest{
    static testMethod void approvedDelete() {

    Idea_Lobby__c change = new Idea_Lobby__c();

    change.Approval__c = 'Approved';
    change.Name = 'Test';

    change.Name__c = 'Submitter';
    change.Location__c = 'Test Location';
    change.Company__c = 'None';


    insert change;
    update change;
    }
    static testMethod void approvedDeleted() {

    Idea_LObby__c change = new Idea_Lobby__c();

    change.Approval__c = 'Delete';
    change.Name = 'Test';

    change.Name__c = 'Submitter';
    change.Location__c = 'Test Location';
    change.Company__c = 'None';


    insert change;
    update change;
    }
    }
  • Thanks!, but it didn't seem to work. I think I'll take out the 2nd half that deletes the record and have my admin manually do it. – DevelopHawaii Sep 25 '14 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.