1

I have a Trigger that creates a record in a custom object ("Services__c") when an Opportunity is marked as "Closed Won". Once closed won, a new Service record will be inserted, and:

  • Service Record Type will be set based on information provided in the Opportunity
  • Opportunity Team Members will be pulled into the Service record
  • Some fields in the Service record will be pre-populated

The trigger works perfectly, but I am having trouble writing an APEX class/test method to promote it to Production! Can anyone help point out my issue? I am getting the error: "Constructors Can not be static"

HERE IS THE TRIGGER

trigger ServiceCreation on Opportunity (after update) {

 map<id,Opportunity> oldValueMap = Trigger.oldMap;

FOR(OPPORTUNITY OPP:TRIGGER.NEW){

    Opportunity oldOpp = oldValueMap.get(OPP.id);
    //Check if Opportunity is Won, is a Centinel Integration, and is not marked as a Plan Upgrade
    if (OPP.isWon && !oldOpp.isWon && Opp.Platform__c <>'Cardinal 2ID' && Opp.Type<>'Plan Upgrade'){

         Services__c SVC = new Services__c();
        //Queries Opportunity Team Member Table, returns list of User IDs and Team Member Roles
        for(OpportunityTeamMember teamMember:[select UserID,TeamMemberRole from OpportunityTeamMember where OpportunityId=:OPP.ID]){
           //Sets Service Owner and Client Manager in Service record based on Opportunity Team Members
            if (teamMember.TeamMemberRole=='Activation Manager'){
                SVC.OwnerId = teammember.userId;
            } else if (teamMember.TeamMemberRole=='Client Manager'){
                SVC.Client_Manager__c = teammember.userId;
            }  

        }
        //Queries Service Record Types and Developer Names 
        Map<string,id> serviceRTMap = new Map<string,id>();
            for (RecordType RT:[select id,developerName from RecordType where SobjectType='Services__c']){
                   serviceRTMap.put(rt.developerName,rt.id);
            }
        //Set record type of Service based on Opportunity Information
        if (opp.Product__c=='Consumer Authentication'){
            Svc.RecordTypeId = serviceRTMap.get('Authentication');
        } else if (opp.Product__c == 'Payment Brands' && opp.Payment_Brands__c.contains('PayPal')){
            Svc.RecordTypeId = serviceRTMap.get('PayPal');
        } else {
            Svc.RecordTypeId = serviceRTMap.get('Payment_Brand');
        }
        //Prefill subset of Service fields, upon creation, with data from the Opportunity
        Svc.Name = Opp.Name; 
        Svc.Register_Date__c = Opp.CloseDate;
        Svc.Stage__c = '1-Boarding';
        Svc.Status__c = 'Registered';
        Svc.Related_Opportunity__c = Opp.ID;
        svc.account__c = Opp.AccountID;
        svc.Sales_Rep__c = Opp.OwnerID;
        svc.Platform__c = Opp.Platform__c;
        svc.Product__c = Opp.Product__c;
        svc.Payment_Brand__c = Opp.Payment_Brands__c;
        svc.Partner__c = Opp.Partner__c;
        svc.Monthly_Transactions__c = Opp.Monthly_Transactions__c;
        svc.Monthly_Revenue__c = Opp.Monthly_Revenue__c;
        svc.Set_Up_Fee__c = Opp.Set_up_Fee__c;
        svc.Annualized_Revenue__c = Opp.Amount;
        svc.RBA__c = opp.Rules__c;
        svc.Adapter__c = Opp.Adapter__c;
        svc.Projected_Activation_Date__c = Opp.Projected_Activation_Date__c;
        Insert SVC;
    }
}
}

HERE IS THE CLASS

Public class TestServiceCreation {
Static testMethod void verifyOpportunityInsert() {
    //data preparation
    Opportunity Opp = new Opportunity(
        Name = 'Consumer Authentication Test',
        StageName = 'Closed Won',
        CloseDate = system.today(),
        Type = 'New Business',
        Platform__c = 'Cardinal Centinel',
        Product__c = 'Consumer Authentication',
        Payment_Brands__c = 'Verified by Visa',
        Rules__c = 'Yes',
        Monthly_Transactions__c = 150,
        Monthly_Revenue__c = 250,
        Set_up_Fee__c = 1500);
   //Insert Opportunity
    test.startTest();
    insert Opp;
    Test.stopTest();

    //Verify the Opportunity was inserted, and update the Stage to Closed Won
 Static testMethod void verifyOppInsertAndUpdate 
    List<Opportunity> insertedOpportunities = [SELECT Name, StageName, Type, Platform__c, Product__c, Payment_Brands__c, Rules__c, Monthly_Transactions__c, Monthly_Revenue__c, Set_Up_Fee__c FROM Opportunity WHERE Name = 'Consumer Authentication Test'];
   Opp.StageName = 'Closed Won';
    Test.startTest();
    Update Opp;
    Test.StopTest();

    //Verify the Service was inserted, and fields were brought over
Static testMethod void verifyServiceCreation    
    List<Services__c> insertedServices__c;
    Services__c SVC = [SELECT Name, Account__c, Related_Opportunity__c,Status__c, Stage__c, Platform__c, Product__c, Payment_Brand__c, Register_Date__c, RBA__c, Monthly_Transactions__c, Monthly_Revenue__c, Set_Up_Fee__c FROM Services__c];
    System.assertEquals(SVC.Name, 'Consumer Authentication');
    System.assertEquals(SVC.Account__c, Opp.AccountId);
    System.assertEquals(SVC.Related_Opportunity__c, Opp.ID);
    System.assertEquals(SVC.Register_Date__c, Opp.CloseDate);
    System.assertEquals(SVC.Status__c, 'Registered');
    System.assertEquals(SVC.Stage__c, '1-Boarding');
    System.assertEquals(SVC.Platform__c, 'Cardinal Centinel');
    System.assertEquals(SVC.Product__c, 'Consumer Authentication');
    System.assertEquals(SVC.Payment_Brand__c, 'Verified by Visa');
    System.assertEquals(SVC.Monthly_Transactions__c, 1500);
    System.assertEquals(SVC.Monthly_Revenue__c, 250);
    System.assertEquals(SVC.Set_Up_Fee__c, 1500);
    System.assertEquals(SVC.RBA__c, 'Yes');
}

}

1
  • You don't have a return type on your method test() thus the compiler thinks it a constructor instead (and is why its complaining about the static keyword). Dec 4, 2013 at 8:51

2 Answers 2

3

See the example below for how the test method should look. You can use either format for test methods.

@isTest
private class TestServiceCreation {

    @isTest
    static void test1() {
        // Test Logic
    }

    static testMethod void test2() {
        // More test logic
    }

}

There are some other issues with your trigger and class:

The insert record; and update record; lines in your test class will cause a compilation error. It should be insert opp; instead.

Your trigger has several SOQL queries and an Insert inside of for loops which means you code will not handle large batches of records. You need to bulkify your trigger.

0

Try deleting "Public" before "static" on the test method.

Also, I would recommend encapsulating the logic in separate methods more like you will see described in this blog: http://jessealtman.com/2013/09/proper-unit-test-structure-in-apex/

Hope this helps!

4
  • Thanks...I used the article and updated the class in the above post...I am now getting an error saying "Only top-level class variables can be static". I am also getting Errors when testing: "System.QueryException: List has no rows for assignment to SObject". Can you look at the new class @snugsfbay @BarCotter? ANY more help would be appreciated...sorry if this is basic, but I am not a developer and had to teach myself how to write this, and it needs deployed to production this week!
    – KCrock88
    Dec 4, 2013 at 13:24
  • Thats very cool that you have taught yourself to write this. However, I would be concerned about not having the right level of support if/when this breaks in production at a critical time.
    – Phil Rymek
    Dec 4, 2013 at 13:35
  • We are in process of getting developer support, though will not have in next 2 days (unfortunately...been waiting for approval on that for awhile, which is why I had to take this into my own hands)
    – KCrock88
    Dec 4, 2013 at 13:43
  • @KCrock88 I suggest that you create a new Question to answer the "System.QueryException: List has no rows for assignment to SObject" error. As for the test not compiling, you are missing the opening and closing brackets in the test method name static testMethod void verifyServiceCreation should be static testMethod void verifyServiceCreation() {
    – BarCotter
    Dec 4, 2013 at 19:24

You must log in to answer this question.

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