function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
KCrock88KCrock88 

Help with APEX Test method

I have a trigger that creates a record in a custom object (Service) when an Opportunity is Closed Won and

-Sets the Record Type based on information in the Opportunity

-Pulls members from the Opportunity Team into User Lookup fields in the new record

-Brings in some values from the Opportunity and sets some default values

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 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;
        }
}
}

 

I am having trouble writing the test method...can anybody help?  How do I write a method that checks to see if the Service record was created, and that the fields are being brought over correctly?

sf@143sf@143
@isTest
public class TestClass
{
Testmethod static void Method1()
{
test.startTest();
Opportunity opp = [select Id FROM Opportunity LIMIT 1][0];
update opp;
test.stoptest();
}
}
KCrock88KCrock88

Thanks for the reply....attached is my attempt (sorry if this is very basic, I am not a developer and am teaching myself as I go along..we are waiting on approval for developer support). I am getting an error message, "Illegal Modifier on local variable"  on line 24:

Public void verifyOppInsertAndUpdate;

 Here is the entire class...would you mind taking a look and pointing out where I went wrong?

@isTest
Public class TestServiceCreation {
    
    TestMethod static 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();
      
        //Update Opportunity to Closed Won, to activate Trigger
    Public void verifyOppInsertAndUpdate;
        List<Opportunity> insertedOpportunities = [SELECT StageName FROM Opportunity WHERE ID = :Opp.ID];
       Opp.StageName = 'Closed Won';
        Test.startTest();
        Update Opp;
        Test.StopTest();
        
        //Verify the Service was inserted, and fields were brought over
        
    Public void verifyServiceCreation;    
        List<Services__c> insertedServices;
        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');
    }
}

 

KCrock88KCrock88
Also, when running the test, I get the error:
"System.QueryException: List has no rows for assignment to SObject
Class.TestServiceCreation.verifyOpportunityInsert: line 27, column 1"

This is generated from the following line in the class:
Static TestMethod void verifyOppInsertAndUpdate;
List<Opportunity> insertedOpportunities = [SELECT StageName FROM Opportunity WHERE ID = :Opp.ID];
Opp.StageName = 'Closed Won';
Test.startTest();