You need to sign in to do that
Don't have an account?

How can I create a Test Class for my Apex Class ?
Hi,
After several days of work and a lot of help I managed to create an Apex Class and an Opportunity Trigger to create a quote when an opportunity is created and a quote line item if the opportunity has an opportunity line item.
But now I must create a Test Class to manage to import it in my production and I have no idea how I can do that.
public class CreateQuote {
@future
public static void createQuote(List<Id> oppIds) {
List<Quote> lstQ = new List<Quote>();
//Fetching all the OLIs belonging to the Opportunities of Trigger.new
List<OpportunityLineItem> olis =[select id, OpportunityId, quantity, PriceBookEntry.Product2Id, UnitPrice, PricebookentryId from OpportunityLineItem where OpportunityId in :oppIds];
List<Opportunity> opps =[select id, name, CloseDate, Type, Pricebook2Id from Opportunity where Id in :oppIds];
//Preparing a Map of each Opportunity and their corrresponding LineItems ( Id of the opportunity => List of the OLIs that belong to the opportunity)
Map<Id,List<OpportunityLineItem>> mapOppIdOli = new Map<Id,List<OpportunityLineItem>>();
for (OpportunityLineItem oli : olis) {
if (mapOppIdOli.containsKey(oli.OpportunityId)) {
mapOppIdOli.get(oli.OpportunityId).add(oli);
} else {
List<OpportunityLineItem> lstOlis = new List<OpportunityLineItem>();
lstOlis.add(oli);
mapOppIdOli.put(oli.OpportunityId, lstOlis);
}
}
//Preparing the new quotes - one for each Opportunity
//List<Opportunity> opps =[select id, name, CloseDate, Pricebook2Id from Opportunity where Id in :oppIds];
for (Opportunity o : opps) {
if (o.Type == 'Event') {
Quote q = new Quote();
//q.name = 'Credit Note ' + o.name;
q.name = o.name;
//q.Montant_1_re_ch_ance__c = 1000;
q.Date_de_Facture__c = o.CloseDate;
q.Date_1_re_ch_ance__c = o.CloseDate;
q.opportunityId = o.id;
q.Pricebook2Id= o.Pricebook2Id;
lstQ.add(q);
}
}
//Inserting the new quotes
insert lstQ;
//Preparing the new QLIs - one for each OLI
List<QuoteLineItem> lstQLI = new List<QuoteLineItem>();
for (Quote q : lstQ) {
List<OpportunityLineItem> lstOlis = mapOppIdOli.get(q.OpportunityId);
if (lstOlis != null) {
for (OpportunityLineItem oli : lstOlis) {
QuoteLineItem qli = new QuoteLineItem();
qli.quoteId = q.Id;
qli.UnitPrice = oli.UnitPrice;
qli.Product2Id = oli.PriceBookEntry.Product2Id;
qli.Quantity = oli.Quantity;
qli.PriceBookentryid = oli.PriceBookentryId;
lstQLI.add(qli);
}
}
}
//Inserting the new QLIs
insert lstQLI;
}
}
trigger Facturation on Opportunity (after insert) {
List<Id> oppIds = new List<Id>();
for (Opportunity o : Trigger.new)
oppIds.add(o.id);
CreateQuote.createQuote(oppIds);
}
After several days of work and a lot of help I managed to create an Apex Class and an Opportunity Trigger to create a quote when an opportunity is created and a quote line item if the opportunity has an opportunity line item.
But now I must create a Test Class to manage to import it in my production and I have no idea how I can do that.
public class CreateQuote {
@future
public static void createQuote(List<Id> oppIds) {
List<Quote> lstQ = new List<Quote>();
//Fetching all the OLIs belonging to the Opportunities of Trigger.new
List<OpportunityLineItem> olis =[select id, OpportunityId, quantity, PriceBookEntry.Product2Id, UnitPrice, PricebookentryId from OpportunityLineItem where OpportunityId in :oppIds];
List<Opportunity> opps =[select id, name, CloseDate, Type, Pricebook2Id from Opportunity where Id in :oppIds];
//Preparing a Map of each Opportunity and their corrresponding LineItems ( Id of the opportunity => List of the OLIs that belong to the opportunity)
Map<Id,List<OpportunityLineItem>> mapOppIdOli = new Map<Id,List<OpportunityLineItem>>();
for (OpportunityLineItem oli : olis) {
if (mapOppIdOli.containsKey(oli.OpportunityId)) {
mapOppIdOli.get(oli.OpportunityId).add(oli);
} else {
List<OpportunityLineItem> lstOlis = new List<OpportunityLineItem>();
lstOlis.add(oli);
mapOppIdOli.put(oli.OpportunityId, lstOlis);
}
}
//Preparing the new quotes - one for each Opportunity
//List<Opportunity> opps =[select id, name, CloseDate, Pricebook2Id from Opportunity where Id in :oppIds];
for (Opportunity o : opps) {
if (o.Type == 'Event') {
Quote q = new Quote();
//q.name = 'Credit Note ' + o.name;
q.name = o.name;
//q.Montant_1_re_ch_ance__c = 1000;
q.Date_de_Facture__c = o.CloseDate;
q.Date_1_re_ch_ance__c = o.CloseDate;
q.opportunityId = o.id;
q.Pricebook2Id= o.Pricebook2Id;
lstQ.add(q);
}
}
//Inserting the new quotes
insert lstQ;
//Preparing the new QLIs - one for each OLI
List<QuoteLineItem> lstQLI = new List<QuoteLineItem>();
for (Quote q : lstQ) {
List<OpportunityLineItem> lstOlis = mapOppIdOli.get(q.OpportunityId);
if (lstOlis != null) {
for (OpportunityLineItem oli : lstOlis) {
QuoteLineItem qli = new QuoteLineItem();
qli.quoteId = q.Id;
qli.UnitPrice = oli.UnitPrice;
qli.Product2Id = oli.PriceBookEntry.Product2Id;
qli.Quantity = oli.Quantity;
qli.PriceBookentryid = oli.PriceBookentryId;
lstQLI.add(qli);
}
}
}
//Inserting the new QLIs
insert lstQLI;
}
}
trigger Facturation on Opportunity (after insert) {
List<Id> oppIds = new List<Id>();
for (Opportunity o : Trigger.new)
oppIds.add(o.id);
CreateQuote.createQuote(oppIds);
}
public class CreateQuoteTest {
static testMethod void insertNewOpportunity() {
Test.startTest();
Product2 testProduct = new Product2(
Name = 'Happy Funtime Ball'
);
insert testProduct;
Opportunity testOpportunity = new Opportunity(
Name = 'Test Opportunity Triggers',
CloseDate = DATEVALUE('2005-11-15'),
StageName = 'Sourcing Demand'
);
insert testOpportunity;
OpportunityLineItem testOLI = new OpportunityLineItem(
OpportunityId = testOpportunity.Id,
Product2 = testProduct.Id,
Name = testOpportunity.Name,
Account_manager_gescom__c = testOpportunity.Account_manager_gescom__c,
CloseDate = testOpportunity.CloseDate,
AccountId = testOpportunity.AccountId,
Stage = testOpportunity.Stage
);
insert testOLI;
Test.stopTest();
}
}
I tried this but there is a problem with the date. I tried many syntaxes but it didn't work.
I am still trying to make Raj V's one work.
@isTest
public class CreateQuoteTest {
static testMethod void insertNewOpportunity() {
Test.startTest();
Product2 testProduct = new Product2(
Name = 'Happy Funtime Ball'
);
insert testProduct;
PricebookEntry pbEntry = new PricebookEntry(
Pricebook2Id = Test.getStandardPricebookId(),
Product2Id = testProduct.Id,
UnitPrice = 100.00,
IsActive = true
);
insert pbEntry;
Opportunity testOpportunity = new Opportunity(
Name = 'Test Opportunity Triggers',
CloseDate = date.today().addDays(5),
StageName = 'Sourcing Demand',
Type = 'Event',
Account_manager_gescom__c = 'Farida Raji'
);
insert testOpportunity;
OpportunityLineItem testOLI = new OpportunityLineItem(
OpportunityId = testOpportunity.Id,
Product2Id = testProduct.Id,
PricebookEntryId = pbEntry.Id,
Quantity = 5,
TotalPrice = 5 * pbEntry.UnitPrice
// Any other data you need
);
insert testOLI;
Test.stopTest();
}
}