You need to sign in to do that
Don't have an account?
Ankit Garg 117
Help with the Test Class while cloning the Opp
Hi,
I have the code which will copy the ''BannerAd'' which is a custom object and shown as a related list on the opportunity, whenever the opportunity is cloned. This is working in the sandbox but I need help with the test class so I can push this to production. Below is the code -
In order to make it work. I had to define two new Apex classes in the developer console
OppCloneTrigger - a trigger that will file after and Opportunity is saved in the system. Defined like:
trigger OppCloneTrigger on Opportunity (after insert) {
// Create a variable for adding all the closed opps.
List<Opportunity>closedOpp = new list<Opportunity>();
// Run through the list and generated closed opporunities.
for(Opportunity op : Trigger.new) {
// Only add opportunities that are cloned and either won or closed.
// (op.StageName != Trigger.oldMap.get(op.Id).StageName) - no need to check for this since it as an insert.
if(op.isClone()) {
closedOpp.add(op);
}
}
// Clone the banners
OppCloneClass.copyBannerAds(closedOpp);
}
and the OppCloneClass - a class that defines a method used to close the banner from original opportunity to the cloned one. Defined:
public class OppCloneClass {
public static void CopyBannerAds(List<Opportunity> opporunities) {
// For every opportunity delivered
for(Opportunity op : opporunities)
{
// Only run this for a cloned opportunity.
if (op.isClone()) {
// Get the id of the source oppotunity.
ID sourceId = op.getCloneSourceId();
// Get the banners from the opportunity that the clone originated from.
// Need to manually specify the fields retrieved so that clone copy all the fields.
List<banner__c> banners=[SELECT
Id,
IsDeleted,
Name,
CreatedDate,
CreatedById,
LastModifiedDate,
LastModifiedById,
SystemModstamp,
LastActivityDate,
LastViewedDate,
LastReferencedDate,
Opportunity__c,
Practice_Address__c,
Location__c,
Inventory_Number__c,
Account_or_HCP_Name__c,
HS_Contract_GST__c,
Competition_Banner__c,
Tier__c,
Discount__c,
Monthly_Banner_Fee__c,
Banner_Discount_in__c,
Banner_Price_2__c,
Group_Purchase_B__c,
HS_Total_Price_incl_GST__c,
Listing_End_Date__c,
HS_Inventory__c,
HS_Discount_Price__c,
Banner_Region__c,
HS_full_Price__c,
HS_Specialty__c,
HS_Contract_Duration__c,
FROM banner__c WHERE Opportunity__c = :sourceId];
// Create new banner items by cloning all the ones related to the source opportunity.
List<banner__c> newBanners = new List<banner__c>();
for(banner__c banner : banners) {
banner__c banclone = banner.clone();
banclone.Opportunity__c=op.id;
newBanners.add(banclone);
}
// Commit new banners into the system.
insert newBanners;
}
}
}
}
I have the code which will copy the ''BannerAd'' which is a custom object and shown as a related list on the opportunity, whenever the opportunity is cloned. This is working in the sandbox but I need help with the test class so I can push this to production. Below is the code -
In order to make it work. I had to define two new Apex classes in the developer console
OppCloneTrigger - a trigger that will file after and Opportunity is saved in the system. Defined like:
trigger OppCloneTrigger on Opportunity (after insert) {
// Create a variable for adding all the closed opps.
List<Opportunity>closedOpp = new list<Opportunity>();
// Run through the list and generated closed opporunities.
for(Opportunity op : Trigger.new) {
// Only add opportunities that are cloned and either won or closed.
// (op.StageName != Trigger.oldMap.get(op.Id).StageName) - no need to check for this since it as an insert.
if(op.isClone()) {
closedOpp.add(op);
}
}
// Clone the banners
OppCloneClass.copyBannerAds(closedOpp);
}
and the OppCloneClass - a class that defines a method used to close the banner from original opportunity to the cloned one. Defined:
public class OppCloneClass {
public static void CopyBannerAds(List<Opportunity> opporunities) {
// For every opportunity delivered
for(Opportunity op : opporunities)
{
// Only run this for a cloned opportunity.
if (op.isClone()) {
// Get the id of the source oppotunity.
ID sourceId = op.getCloneSourceId();
// Get the banners from the opportunity that the clone originated from.
// Need to manually specify the fields retrieved so that clone copy all the fields.
List<banner__c> banners=[SELECT
Id,
IsDeleted,
Name,
CreatedDate,
CreatedById,
LastModifiedDate,
LastModifiedById,
SystemModstamp,
LastActivityDate,
LastViewedDate,
LastReferencedDate,
Opportunity__c,
Practice_Address__c,
Location__c,
Inventory_Number__c,
Account_or_HCP_Name__c,
HS_Contract_GST__c,
Competition_Banner__c,
Tier__c,
Discount__c,
Monthly_Banner_Fee__c,
Banner_Discount_in__c,
Banner_Price_2__c,
Group_Purchase_B__c,
HS_Total_Price_incl_GST__c,
Listing_End_Date__c,
HS_Inventory__c,
HS_Discount_Price__c,
Banner_Region__c,
HS_full_Price__c,
HS_Specialty__c,
HS_Contract_Duration__c,
FROM banner__c WHERE Opportunity__c = :sourceId];
// Create new banner items by cloning all the ones related to the source opportunity.
List<banner__c> newBanners = new List<banner__c>();
for(banner__c banner : banners) {
banner__c banclone = banner.clone();
banclone.Opportunity__c=op.id;
newBanners.add(banclone);
}
// Commit new banners into the system.
insert newBanners;
}
}
}
}
try following test class with 100% code coverage.
let me know if it helps you and marking it as best answer.
Thank you