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
Ankit Garg 117Ankit 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;
            }
        }
    }
 }
ravi soniravi soni
hy,
try following test class with 100% code coverage.
@isTest
public class OppCloneTriggerTest {
    @isTest
    public static void test_unit(){
        Test.startTest();
       Account acc = new Account(Name = 'Test Account');
        insert acc;
       
        banner__c banner1 = new banner__c();
        banner1.Name = 'test';
        insert banner1;
        
        Opportunity opp = new Opportunity();
        opp.Name = 'Test Opportunity';
        opp.AccountId = acc.Id;
        opp.StageName = 'Closed Won';
        opp.CloseDate = system.today() + 1;
        insert opp;
        Opportunity cloneOpp = opp.clone();
        
        string sourceId = cloneOpp.getCloneSourceId();
        system.debug('sourceId : ' + sourceId);
        banner1.Opportunity__c = sourceId;
        update banner1;
        insert cloneOpp;
        Test.stopTest();     
       system.assertEquals(opp.StageName, 'Closed Won');
    }
    
}

let me know if it helps you and marking it as best answer.
Thank you
Ankit Garg 117Ankit Garg 117
Thanks Veer for sending this through. The code coverage I got is 58%. Have I missed anything? We do have record type, will this make a difference?

User-added image