You need to sign in to do that
Don't have an account?
Travis Wright
Need help with test class
Need help with a test class for the following Class. I am new to development and need to get this class above 75%. I didn't write this class so I am kinda stuck. Any help would be great.
Class
trigger CreateAssetonClosedWon on Opportunity (after insert, after update) {
Map<String, AssetRollup__c> rollupMap = AssetRollup__c.getAll();
for(Opportunity o: trigger.new){
if(o.isWon == true && o.HasOpportunityLineItem == true){
String opptyId = o.Id;
OpportunityLineItem[] OLI = [Select UnitPrice, Quantity, PricebookEntry.Product2Id,
TotalPrice,
PricebookEntry.Product2.Name, Description, Converted_to_Asset__c,
PricebookEntry.Product2.Create_Asset__c, PricebookEntry.Product2.Sales_Order_Group__c
From OpportunityLineItem
where OpportunityId = :opptyId
and Converted_to_Asset__c = false
and PricebookEntry.Product2.Create_Asset__c = true];
Asset[] ast = new Asset[]{};
Asset hotline;
Asset a = new Asset();
for(OpportunityLineItem ol: OLI){
a = new Asset();
a.AccountId = o.AccountId;
a.Product2Id = ol.PricebookEntry.Product2Id;
a.Quantity__c = ol.Quantity;
a.Price = ol.TotalPrice;
a.PurchaseDate = o.CloseDate;
a.Renewal_Date__c = o.CloseDate.Month() + '/' + o.CloseDate.Day();
a.Status = 'Purchased';
a.Description = ol.Description;
a.Name = ol.PricebookEntry.Product2.Name;
// TODO:extend this to support matching on sales order group from AssetRollup__c and build a list of rollups
if(ol.PricebookEntry.Product2.Sales_Order_Group__c=='International Hotline'){
if(hotline == null) {
hotline = a;
AssetRollup__c rollup = rollupMap.get(ol.PricebookEntry.Product2.Sales_Order_Group__c);
if (rollup != null){
hotline.Name = rollup.Asset_Name__c;
hotline.Product2Id = rollup.ProductId__c;
}
}
else {
hotline.Quantity__c += a.Quantity__c;
hotline.Price += ol.TotalPrice;
}
}
else{
ast.add(a);
}
ol.Converted_to_Asset__c = true;
}
if(hotline != null) {
ast.add(hotline);
}
update OLI;
insert ast;
}
}
}
Test Class
@isTest
Public class testReassignCNO {
static testMethod void testPardotScore() {
Account a = new Account();
a.name = 'TNW (for Competitor Profile)';
a.OwnerId = '005A0000000gWDt';
insert a;
Contact q = new Contact();
q.FirstName = 'Test';
q.LastName = 'User 1';
q.pi__score__c = 20;
q.accountId = a.Id;
q.OwnerId = '005A0000000gWDt';
q.No_Longer_Employed__c = true;
insert q;
Contact c = new Contact();
c.FirstName = 'Test2';
c.LastName = 'User 2';
c.pi__score__c = 20;
c.accountId = a.Id;
c.OwnerId = '005F0000003kNza';
insert c;
a.assign__c = True;
update a;
}
}
Class
trigger CreateAssetonClosedWon on Opportunity (after insert, after update) {
Map<String, AssetRollup__c> rollupMap = AssetRollup__c.getAll();
for(Opportunity o: trigger.new){
if(o.isWon == true && o.HasOpportunityLineItem == true){
String opptyId = o.Id;
OpportunityLineItem[] OLI = [Select UnitPrice, Quantity, PricebookEntry.Product2Id,
TotalPrice,
PricebookEntry.Product2.Name, Description, Converted_to_Asset__c,
PricebookEntry.Product2.Create_Asset__c, PricebookEntry.Product2.Sales_Order_Group__c
From OpportunityLineItem
where OpportunityId = :opptyId
and Converted_to_Asset__c = false
and PricebookEntry.Product2.Create_Asset__c = true];
Asset[] ast = new Asset[]{};
Asset hotline;
Asset a = new Asset();
for(OpportunityLineItem ol: OLI){
a = new Asset();
a.AccountId = o.AccountId;
a.Product2Id = ol.PricebookEntry.Product2Id;
a.Quantity__c = ol.Quantity;
a.Price = ol.TotalPrice;
a.PurchaseDate = o.CloseDate;
a.Renewal_Date__c = o.CloseDate.Month() + '/' + o.CloseDate.Day();
a.Status = 'Purchased';
a.Description = ol.Description;
a.Name = ol.PricebookEntry.Product2.Name;
// TODO:extend this to support matching on sales order group from AssetRollup__c and build a list of rollups
if(ol.PricebookEntry.Product2.Sales_Order_Group__c=='International Hotline'){
if(hotline == null) {
hotline = a;
AssetRollup__c rollup = rollupMap.get(ol.PricebookEntry.Product2.Sales_Order_Group__c);
if (rollup != null){
hotline.Name = rollup.Asset_Name__c;
hotline.Product2Id = rollup.ProductId__c;
}
}
else {
hotline.Quantity__c += a.Quantity__c;
hotline.Price += ol.TotalPrice;
}
}
else{
ast.add(a);
}
ol.Converted_to_Asset__c = true;
}
if(hotline != null) {
ast.add(hotline);
}
update OLI;
insert ast;
}
}
}
Test Class
@isTest
Public class testReassignCNO {
static testMethod void testPardotScore() {
Account a = new Account();
a.name = 'TNW (for Competitor Profile)';
a.OwnerId = '005A0000000gWDt';
insert a;
Contact q = new Contact();
q.FirstName = 'Test';
q.LastName = 'User 1';
q.pi__score__c = 20;
q.accountId = a.Id;
q.OwnerId = '005A0000000gWDt';
q.No_Longer_Employed__c = true;
insert q;
Contact c = new Contact();
c.FirstName = 'Test2';
c.LastName = 'User 2';
c.pi__score__c = 20;
c.accountId = a.Id;
c.OwnerId = '005F0000003kNza';
insert c;
a.assign__c = True;
update a;
}
}
If you haven't already, please review the following link: https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
ALso, if you search the forums, there are literally hundreds of these kinds of posts which could also help.
In short, at minium you'll want to:
-
Create an opportunity record in your unit test (this will fire your trigger and execute the code to some degree).
-
Create any other dependent data like Opportunity Line Item records.
-
Provide system asserts in your unit test to verify that your class is working as expected.
Good luck,All Answers
If you haven't already, please review the following link: https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
ALso, if you search the forums, there are literally hundreds of these kinds of posts which could also help.
In short, at minium you'll want to:
-
Create an opportunity record in your unit test (this will fire your trigger and execute the code to some degree).
-
Create any other dependent data like Opportunity Line Item records.
-
Provide system asserts in your unit test to verify that your class is working as expected.
Good luck,@isTest
private class testCreateAssetonClosedWon {
static testMethod void testCreateAssetonClosedWon(){
Account a = [select Id from Account limit 1];
PricebookEntry pbID = [select Id from PricebookEntry WHERE IsActive = true limit 1];
Opportunity o = new Opportunity();
OpportunityLineItem ol = new OpportunityLineItem();
o.AccountId = a.Id;
o.Name = 'test';
o.StageName = '1. Initial Contact';
o.Type = 'New';
o.LeadSource = 'AM Activity';
o.CloseDate = date.today();
insert o;
ol.OpportunityId = o.Id;
ol.Quantity = 1;
ol.UnitPrice = 2.00;
ol.PricebookEntryId = pbId.Id;
insert ol;
delete o;
}
}
To provide test class the best practise is to load the sample data or the custom setting as a static resource and link it to your Test Class. This will help you get the converage of those lines: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_load_data.htm
List<AssetRollup__c> ls = Test.loadData(AssetRollup__c.sObjectType, '<StaticResourceName>');