0

I'm trying to cover my Apex Trigger below using a Test Class:

trigger createPubAppovalCase on Opportunity (after update) {
    List<Case> listcase = new List<Case>();
    for(Opportunity o:trigger.new) {
       Opportunity oldopp = trigger.OldMap.get (o.id);
       if (o.Request_PubApproval__c && !oldopp.Request_PubApproval__c && o.Account.Account_Region__c <> 'North America' && o.Account.Account_Region__c <> '' && o.Account.Account_Region__c <> null)
   {
        String userName = UserInfo.getUserName();
        User activeUser = [Select Email From User where Username = : userName limit 1];
        String userEmail = activeUser.Email;
       List<Contact> oppOwnerAsContact = [select Id from Contact where email =: userEmail];      
       //If the above list is empty, a new Contact needs to be created before the Case is inserted.
       if(oppOwnerAsContact.size() == 0) {
           Contact newContact = new Contact(firstName = UserInfo.getFirstName(),
           lastName = UserInfo.getLastName(),
           Account = [select id from Account where Name =: 'LiveRail' Limit 1],
           email = userEmail);
           insert newContact;
           oppOwnerAsContact.add(newContact);
       }      
          //here Opportunity__c is lookup to Opportunity record for which new record is created
          listCase.add(new Case(Opportunity__c = o.id,
          RecordTypeID = [select id from RecordType where DeveloperName = :'Pub_Approval' Limit 1].id,
          OwnerID = [select id from Group where DeveloperName = :'EMEA_Pub_Approval_Queue' Limit 1].id,
          ContactID = oppOwnerAsContact[0].id,
          Subject = 'Pub Approval for "'+o.Name+'"',
          Origin = 'Pub Approval Workflow',
          //Priority = 'Medium',
          Status = 'New',
          Description = 'Full Details on the opportunity.',
          site_URLs__c = o.Site_URLs__c,
          Sample_video_page_URL__c = o.Sample_video_page_URL__c,
          Company_website__c = o.Company_website__c));
    }
}
if(listcase.size()>0) {
   insert listcase;
}

Below is the test Class, which for some reason isn't covering the above trigger, and the case isn't being created (no rows for assignment error). I'm stumped!

@isTest (SeeAllData=true)
private class testPubApprovalTrigger {

static testMethod void testPubApprovalRequest() {

Account acc = new Account(Name = 'testestsklzjgfldsa',
Account_Region__c = 'EMEA',
Account_Country__c = 'United Kingdom',
Type = 'Publisher');
insert acc;
acc = [select id, Name from Account where name = 'testestsklzjgfldsa' Limit 1];

User own = [select id, Name from User where name = 'Tim Milazzo' Limit 1];

Opportunity opp = new Opportunity(name='pubpubpubApprovalTest',
    closeDate=date.today(),
    recordType = [select id from RecordType where DeveloperName = 'LRC_Supply' AND SobjectType = 'Opportunity'],
    site_URLs__c = 'site.com',
    sample_Video_Page_URL__c = 'site.com/video',
    company_website__c = 'site.com',
    stageName = 'Pitching',
    Account = acc,
    Owner = own,
    LeadSource = 'apex is so fun');
insert opp;

opp = [select id, Request_PubApproval__c, Name, ownerID from Opportunity where name =: 'pubpubpubApprovalTest' Limit 1];
opp.Request_PubApproval__c = TRUE;
update opp;

// Find the new case and test some assertions
Case appCase = [select id, sample_Video_Page_URL__c from Case where Opportunity__c =: opp.id Limit 1];
system.assert(appCase.sample_Video_Page_URL__c == opp.sample_Video_Page_URL__c);

}

}

If this is easy for anyone to catch what's going wrong, it would be very much appreciated!

9
  • 1
    Hi Tim! Welcome to Salesforce.StackExchange. I've formatted the code for your trigger so it displays properly (select it, then hit the {} for preformatted text) or space each line over 4 spaces. Have you tried adding debug statements to see where your code isn't working?
    – crmprogdev
    Commented Jul 7, 2014 at 14:31
  • you query to get the opportunity should be like this, opp = [select id, Request_PubApproval__c, Name, ownerID from Opportunity where name = 'pubpubpubApprovalTest' Limit 1]
    – Bforce
    Commented Jul 7, 2014 at 14:34
  • crmprogdev, I had accidentally approved the suggested edit. Haved rolled back to your edit. Commented Jul 7, 2014 at 14:34
  • Ahh, that explains it! I edited my comment when I saw it revert. Good to know so many people are on the job! :)
    – crmprogdev
    Commented Jul 7, 2014 at 14:48
  • Hi crmprogdev - through the Developer Console's test coverage, it looks like the trigger isn't covered past the first if() statement, so one of those conditions is not true for the test. That's where I'm stumped, as "Request_PubApproval__c" should be newly true, and the region in the test isn't one that's excluded.
    – TimMilazzo
    Commented Jul 7, 2014 at 14:53

1 Answer 1

1

OK, here are the issues:

Lookup fields not in scope of Trigger.new

    if (o.Request_PubApproval__c && !oldopp.Request_PubApproval__c && 
        o.Account.Account_Region__c <> 'North America' && 
        o.Account.Account_Region__c <> '' && 
        o.Account.Account_Region__c <> null)

The field o.account.account_region__c is not in scope for trigger.new. Only the Opportunity fields are in scope for Trigger.new. You'll need to query to get the Account fields via the lookup relationships - be sure to bulkify the lookup query

Your final assert will fail because you've wiped out the value you are testing against:

    opp = [select id, Request_PubApproval__c, Name, ownerID 
        from Opportunity where name =: 'pubpubpubApprovalTest' Limit 1];
    opp.Request_PubApproval__c = TRUE;
    update opp;

    // Find the new case and test some assertions
    Case appCase = [select id, sample_Video_Page_URL__c 
                   from Case where Opportunity__c =:opp.id Limit 1];
    system.assert(appCase.sample_Video_Page_URL__c == opp.sample_Video_Page_URL__c);

When you query for opp, you are not selecting field sample_video_page_url__c and hence it is not available (value is null) to compare against the Case

This would have been clearer to you if you had written the assertion as

system.assertEquals(opp.sample_Video_Page_URL__c,appCase.sample_Video_Page_URL__c);

as SFDC would tell you that the expected value was null and the actual value was site.com/video

You must log in to answer this question.

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