You need to sign in to do that
Don't have an account?
KCrock88
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?
public class TestClass
{
Testmethod static void Method1()
{
test.startTest();
Opportunity opp = [select Id FROM Opportunity LIMIT 1][0];
update opp;
test.stoptest();
}
}
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:
Here is the entire class...would you mind taking a look and pointing out where I went wrong?
"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();