Skip to content
The Chat Archive
gpt-3.5-turbo-0301· OpenAI WildChat-4.8M (AI2)

write a test class for below mention class trigger SubscriptionProductChargeTrigger on…

  1. User

    write a test class for below mention class

    trigger SubscriptionProductChargeTrigger on Zuora__SubscriptionProductCharge__c (after insert, after update) {

    List<Zuora__SubscriptionProductCharge__c> lstSub = new List<Zuora__SubscriptionProductCharge__c>();
    if(!AccountTriggerHelper.isRecursive){
        List<id> ids=new List<id>();
        for(Zuora__SubscriptionProductCharge__c sp: Trigger.new){
            ids.add(sp.Zuora__Subscription__c) ;
        }
        List<String> skuList=new List<String>();
        for(Zuora__SubscriptionProductCharge__c sp: Trigger.new){
            skuList.add(sp.Zuora__ProductSKU_Product__c);
        }
        
        List<Zuora__Product__c > productList=[SELECT id, Zuora__SKU__c   FROM Zuora__Product__c  WHERE Zuora__SKU__c  in:skuList];
        Map<String,id> skuToProductIdmap=new Map<String,id>();
        for(Zuora__Product__c  prod:productList){
            skuToProductIdmap.put(prod.Zuora__SKU__c , prod.Id);
        }
        
        for(Zuora__SubscriptionProductCharge__c sp: Trigger.new){
            if(((Trigger.isInsert && sp.DateofChurn__c != null && sp.ReasonforChurn__c != null) || 
                (Trigger.isupdate &&  sp.DateofChurn__c != null && sp.DateofChurn__c != Trigger.oldMap.get(sp.Id).DateofChurn__c
                 && sp.ReasonforChurn__c != null && sp.ReasonforChurn__c != Trigger.oldMap.get(sp.Id).ReasonforChurn__c))){
                     Zuora__SubscriptionProductCharge__c newSP = new Zuora__SubscriptionProductCharge__c();
                     newSP.Name = sp.Name;
                     newSP.Zuora__Type__c = 'Churned Product';
                     newSP.Zuora__TotalContractValue__c = sp.Zuora__TotalContractValue__c;
                     newSP.Zuora__BillingPeriod__c = sp.Zuora__BillingPeriod__c;
                     newSP.DateofChurn__c = sp.DateofChurn__c;
                     newSP.ReasonforChurn__c = sp.ReasonforChurn__c;
                     newSP.Zuora__EffectiveStartDate__c = sp.Zuora__EffectiveStartDate__c;
                     newSP.Zuora__EffectiveEndDate__c = sp.Zuora__EffectiveEndDate__c;
                     newSP.Zuora__Account__c = sp.Zuora__Account__c;
                     newSP.Zuora__Subscription__c = sp.Zuora__Subscription__c;
                     newSP.Zuora__Product__c = skuToProductIdmap.get(sp.Zuora__ProductSKU_Product__c);
                     newSP.currencyISOCode = sp.currencyISOCode;
                     newSP.Original_End_Date__c = Trigger.oldMap.get(sp.Id).Zuora__EffectiveEndDate__c;
                     newSP.Zuora__Price__c = sp.Zuora__Price__c;
                     newSP.Zuora__RatePlanName__c = sp.Zuora__RatePlanName__c;
                     newSP.Zuora__Model__c = sp.Zuora__Model__c;
                     newSP.ChurnNotes__c = sp.ChurnNotes__c;
                     newSP.Zuora__ExtendedAmount__c = sp.Zuora__ExtendedAmount__c;
                     newSP.Competitor__c = sp.Competitor__c;
                     lstSub.add(newSP);
                    system.debug(Logginglevel.error,'Original Zuora__Product__c value: '+ sp.Zuora__Product__c);
                     System.debug(Logginglevel.error,'New Zuora__Product__c value: ' + newSP.Zuora__Product__c);
                 }
        }
        AccountTriggerHelper.isRecursive = true;
        insert lstSub;
    }
    

    }

  2. Assistant

    @IsTest public class SubscriptionProductChargeTriggerTest {

    static testMethod void testTrigger() {
        //create test data
        Account acct = new Account(Name='Test Account');
        insert acct;
        
        Contact con = new Contact(FirstName='Test', LastName='Contact', AccountId=acct.Id);
        insert con;
        
        Zuora__Product__c zuoraProd = new Zuora__Product__c(Zuora__SKU__c='TestSKU');
        insert zuoraProd;
        
        Zuora__Subscription__c zuoraSub = new Zuora__Subscription__c(
            Name='Test Subscription',
            Zuora__Account__c=acct.Id,
            Zuora__TermType__c='TERMED',
            Zuora__InitialTermPeriodType__c='Month',
            Zuora__InitialTerm__c=12,
            Zuora__RenewalTermPeriodType__c='Month',
            Zuora__RenewalTerm__c=12,
            Zuora__Status__c='Active',
            Zuora__TermStartDate__c=System.today(),
            Zuora__TermEndDate__c=System.today().addMonths(12)
        );
        insert zuoraSub;
        
        List<Zuora__SubscriptionProductCharge__c> spList = new List<Zuora__SubscriptionProductCharge__c>{
            new Zuora__SubscriptionProductCharge__c(
                Name='Test Subscription Charge',
                Zuora__Type__c='Product - Charge',
                Zuora__TotalContractValue__c=1000,
                Zuora__BillingPeriod__c='Quarterly',
                DateofChurn__c=null,
                ReasonforChurn__c=null,
                Zuora__EffectiveStartDate__c=System.today(),
                Zuora__EffectiveEndDate__c=System.today().addMonths(3),
                Zuora__Account__c=acct.Id,
                Zuora__Subscription__c=zuoraSub.Id,
                Zuora__ProductSKU_Product__c='TestSKU',
                currencyISOCode='USD',
                Zuora__RatePlanName__c='Test Rate Plan',
                Zuora__Model__c='Test Model',
                ChurnNotes__c='Test Notes',
                Zuora__ExtendedAmount__c=1000,
                Competitor__c='Test Competitor'
            )
        };
        insert spList;
        
        //test trigger
        Test.startTest();
        SubscriptionProductChargeTrigger.triggerHandler(Test.getTriggeredDMLRecords(triggerSize));
        Test.stopTest();
        
        //assertions
        List<Zuora__SubscriptionProductCharge__c> churnedList = [
            SELECT Id, Name, Zuora__Type__c, Zuora__TotalContractValue__c, Zuora__BillingPeriod__c,
                DateofChurn__c, ReasonforChurn__c, Zuora__EffectiveStartDate__c, Zuora__EffectiveEndDate__c,
                Zuora__Account__c, Zuora__Subscription__c, Zuora__Product__c, currencyISOCode,
                Original_End_Date__c, Zuora__Price__c, Zuora__RatePlanName__c, Zuora__Model__c,
                ChurnNotes__c, Zuora__ExtendedAmount__c, Competitor__c
            FROM Zuora__SubscriptionProductCharge__c
            WHERE Zuora__Type__c = 'Churned Product'
        ];
        System.assertEquals(0, churnedList.size(), 'Should not have created any churned product charges');
        
        //update subscription product charge with churn information
        spList[0].DateofChurn__c = System.today();
        spList[0].ReasonforChurn__c = 'Test Churn Reason';
        update spList;
        
        //test trigger again
        Test.startTest();
        SubscriptionProductChargeTrigger.triggerHandler(Test.getTriggeredDMLRecords(triggerSize));
        Test.stopTest();
        
        //assertions
        churnedList = [
            SELECT Id, Name, Zuora__Type__c, Zuora__TotalContractValue__c, Zuora__BillingPeriod__c,
                DateofChurn__c, ReasonforChurn__c, Zuora__EffectiveStartDate__c, Zuora__EffectiveEndDate__c,
                Zuora__Account__c, Zuora__Subscription__c, Zuora__Product__c, currencyISOCode,
                Original_End_Date__c, Zuora__Price__c, Zuora__RatePlanName__c, Zuora__Model__c,
                ChurnNotes__c, Zuora__ExtendedAmount__c, Competitor__c
            FROM Zuora__SubscriptionProductCharge__c
            WHERE Zuora__Type__c = 'Churned Product'
        ];
        System.assertEquals(1, churnedList.size(), 'Should have created one churned product charge');
        Zuora__SubscriptionProductCharge__c churnedCharge = churnedList[0];
        System.assertEquals(spList[0].Name, churnedCharge.Name, 'Name should match');
        System.assertEquals('Churned Product', churnedCharge.Zuora__Type__c, 'Type should be Churned Product');
        System.assertEquals(spList[0].Zuora__TotalContractValue__c, churnedCharge.Zuora__TotalContractValue__c, 'Total Contract Value should match');
        System.assertEquals(spList[0].Zuora__BillingPeriod__c, churnedCharge.Zuora__BillingPeriod__c, 'Billing Period should match');
        System.assertEquals(spList[0].DateofChurn__c, churnedCharge.DateofChurn__c, 'Date of Churn should match');
        System.assertEquals(spList[0].ReasonforChurn__c, churnedCharge.ReasonforChurn__c, 'Reason for Churn should match');
        System.assertEquals(spList[0].Zuora__EffectiveStartDate__c, churnedCharge.Zuora__EffectiveStartDate__c, 'Effective Start Date should match');
        System.assertEquals(spList[0].Zuora__EffectiveEndDate__c, churnedCharge.Zuora__EffectiveEndDate__c, 'Effective End Date should match');
        System.assertEquals(spList[0].Zuora__Account__c, churnedCharge.Zuora__Account__c, 'Account should match');
        System.assertEquals(spList[0].Zuora__Subscription__c, churnedCharge.Zuora__Subscription__c, 'Subscription should match');
        System.assertEquals(zuoraProd.Id, churnedCharge.Zuora__Product__c, 'Product should match');
        System.assertEquals(spList[0].currencyISOCode, churnedCharge.currencyISOCode, 'Currency ISO Code should match');
        System.assertEquals(spList[0].Zuora__Price__c, churnedCharge.Zuora__Price__c, 'Price should match');
        System.assertEquals(spList[0].Zuora__RatePlanName__c, churnedCharge.Zuora__RatePlanName__c, 'Rate Plan Name should match');
        System.assertEquals(spList[0].Zuora__Model__c, churnedCharge.Zuora__Model__c, 'Model should match');
        System.assertEquals(spList[0].ChurnNotes__c, churnedCharge.ChurnNotes__c, 'Churn Notes should match');
        System.assertEquals(spList[0].Zuora__ExtendedAmount__c, churnedCharge.Zuora__ExtendedAmount__c, 'Extended Amount should match');
        System.assertEquals(spList[0].Competitor__c, churnedCharge.Competitor__c, 'Competitor should match');
    }
    

    }