0

I am new to SFDC Dev and wrote my first trigger, and was hoping to get assistance on my test class.

My trigger is based off of this reference below.

https://developer.salesforce.com/forums/?id=906F00000008yuFIAQ

Here is my code:

trigger FireFighterticketcreatorTrigger on Help_Desk_Ticket__c (after update) {
    List<Firefighter__c> ftix = new List<Firefighter__c>(); 
    for (Help_Desk_Ticket__c hd:trigger.new) {
        if (hd.OwnerId == '0054D000000RlEB') {
            Firefighter__c ff = new Firefighter__c ();
            ff.Email_Address__c = hd.Email_Address__c;
            ff.EmailFromName__c = hd.EmailFromName__c;
            ff.OwnerId            = '0054D000000RlEB';
            ff.Description__c   = hd.Description__c;
            ff.Internal_Notes__c= hd.Internal_Notes__c;

            ftix.add(ff);
        }
    } 

    try {
        insert ftix;
    } catch (system.Dmlexception e){
        system.debug(e);
    }
}

Here is my test class:

@isTest(SeeAllData=true)
private class TestFireFighterTickets_createFFticket {

    static testmethod void testFireFighter() {
        Test.startTest();
        Firefighter__c ftix = new Firefighter__c (
            Email_Address__c = '[email protected]',
            Title__c = 'Test',
            EmailFromName__c = 'Wil Deynes'
        );
        insert ftix;
    Test.stopTest();
    }
}

Any help is greatly appreciated!

3
  • 1
    You should consider a different approach to your condition check based on record id. This code is only ever going to execute for a single user, quite pointless right? Is there any other criteria based on user that would fit into your business requirement?
    – TSmith
    Commented Mar 27, 2017 at 21:58
  • Unless you need do to coding, I'd suggest you just move this to a Process Builder, which is competent enough for the task you're trying to perform.
    – sfdcfox
    Commented Mar 27, 2017 at 23:01
  • If you have to use a Owner ID, you may want to broaden that to a Profile and manage it through custom settings so you don't have to change code each time the person wins the lottery
    – EricSSH
    Commented Mar 28, 2017 at 3:07

2 Answers 2

1

There are some points to note within your current code and also your attempted test method.

FireFighterticketcreatorTrigger

if (hd.OwnerId == '0054D000000RlEB') {...}

Performing a conditional statement on a single id is useless on the force.com platform. Always remember to consider a bulkification approach when at the design stage. Running record creations based on a single id will cause you trouble not only in code but when migrating from one environemnt to another.

try {
    insert ftix;
} catch (system.Dmlexception e){
    system.debug(e);
}

Try catch on a DML of this nature is some what of an overkill. Checking the list size before the DML would be sufficient.

if (ftix.size() > 0) insert ftix; 

TestFireFighterTickets_createFFticket

Test cases should be desinged first before any class/trigger logic is created, TDD. These cases allow us to define our expected outcome or what we could call the business acceptance criterias.

In your case we should be expecting a single Firefighter__c record when any Help_Desk_Ticket__c has been updated and has a specific owner (please review this requirement and consider a different approach based on business).

Once we have set up our test data to trigger the creation of Firefighter__c records, we can use the System class to assert our expectancies.

Possible test class:

@isTest(SeeAllData=true)

private class TestFireFighterTickets_createFFticket {  
    static testmethod void testFireFighter() {
        Test.startTest();
        Help_Desk_Ticket__c hd = [
            select id,
              EmailFromName__c
            from Help_Desk_Ticket__c
            where OwnerId = '0054D000000RlEB'
            limit 1];
        System.assert(hd != null);

        hd.EmailFromName__c = 'This email should match new FF record.';
        update hd;
        Test.stopTest();

        List<Firefighter__c> ffs = [
            select id
            from Firefighter__c
            where EmailFromName__c = :hd.EmailFromName__c];
        System.assertEquals(1, ffs.size());
    }
}

After updating the queried record belong to the specific OwnerId, we do an update to trigger out FireFigther creation. Once the update has complete, we query the FireFigther table to find a record matching based on EmailFromName__c.

2
  • if (ftix.size() > 0) is useless, as the system automatically does this for you as of a few releases ago. All this code does is waste CPU time (I actually bothered to benchmark it). The More You Know.
    – sfdcfox
    Commented Mar 27, 2017 at 22:45
  • Thanks @sfdcfox, Found some benchmarking for DMLs on an empty list.
    – TSmith
    Commented Mar 28, 2017 at 8:10
-1

As most of your code inside trigger is valid only if user with Id '0054D000000RlEB' is the owner, you need to use system.runas() to replicate inserting a help_desk_ticket

Code:

private class TestFireFighterTickets_createFFticket {
    static testmethod void testFireFighter()
    {
        user u=[select id from user where id='0054D000000RlEB' limit 1];
        System.runAs(u) {

            Test.startTest();
            Help_Desk_Ticket__c hdt = new hdt (
            //required fields and values
            );
            insert hdt;
            Test.stopTest();
        }
    }
}
3
  • I just noticed that you are trying to insert a fireFighter record in your test class. You should rather insert an help desk ticket, which automatically adds a fire Fighter record. Commented Mar 27, 2017 at 22:02
  • -1 Never hard code an ID into a test class. Use a profile name instead to get a user Id or create a new user with the profile you want so the test cannot fail if the user isn't found. Inserting a help desk ticket will require an owner which you'll also want to insert separately.
    – crmprogdev
    Commented Mar 29, 2017 at 12:31
  • @crmprogdev thanks for your comments. 1. I was just giving a sample test code for the class he provided. i should have told him that his code standards are not Commented Mar 29, 2017 at 21:30

You must log in to answer this question.

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