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
samsam 

Can I get Test method for this apex method

public static void createExhibitionContractedPrice(Set<Id> contractIdSet, Map<Id,Contract> newContractMap){
        system.debug('===createExhibitionContractedPrice called');
        Map<Id, Account> accMap = new Map<Id, Account>();
        Map<Id,Set<Id>> customerExhiMap = new Map<Id,Set<Id>>();
        List<ST_Exhibition_Contracted_Price__c> exhibitionContractedPriceList = new List<ST_Exhibition_Contracted_Price__c>();
        Set<Id> deactivatePrevCont = new Set<Id>();
        Account acc = new Account();
        
        
        for(SBQQ__Subscription__c subs : [select id, SBQQ__ProductId__c, SBQQ__Contract__r.AccountId, SBQQ__Contract__r.Account.Active_Exhibition_Contract__c,ST_Customer_Product_Category__c, ST_Exhibition__c,ST_Liability_Coverage__c,
                                          ST_Offline_Charge__c, ST_Rate_Amount__c, ST_Rate_UOM__c, SBQQ__Account__c, SBQQ__EndDate__c, SBQQ__StartDate__c, SBQQ__QuoteLine__r.SBQQ__Quote__r.Quote_For__c,
                                          SBQQ__Account__r.Active_Contract__c, SBQQ__Contract__c,Exhibition_Standard_Price__r.Level_1__c,Exhibition_Standard_Price__r.Level_2__c,Exhibition_Standard_Price__r.Level_3__c,
                                          Slab_Level_1_Rate__c,Slab_Level_2_Rate__c,Slab_Level_3_Rate__c,Slab_Level_4_Rate__c,Slab_Level_5_Rate__c,Max_Slab_Rate__c,
                                          Exhibition_Standard_Price__r.Level_4__c,Exhibition_Standard_Price__r.Level_5__c
                                          FROM SBQQ__Subscription__c WHERE SBQQ__Contract__c=:contractIdSet ])
        {
            system.debug('===SBQQ__Subscription__c record : '+subs.Id);
            ST_Exhibition_Contracted_Price__c exhibitionContract = new ST_Exhibition_Contracted_Price__c();
            exhibitionContract.ST_Active__c = true;
            exhibitionContract.ST_Contract__c = subs.SBQQ__Contract__c;
            exhibitionContract.ST_Customer_Account__c = subs.SBQQ__Account__c;
            exhibitionContract.ST_Customer_Product_Category__c = subs.ST_Customer_Product_Category__c;
            exhibitionContract.ST_End_Date__c = subs.SBQQ__EndDate__c;
            exhibitionContract.ST_Exhibition__c = subs.ST_Exhibition__c;
            exhibitionContract.ST_Liability_Coverage__c = subs.ST_Liability_Coverage__c;
            exhibitionContract.ST_Offline_Charge__c = subs.ST_Offline_Charge__c;
            exhibitionContract.ST_Product__c = subs.SBQQ__ProductId__c;
            exhibitionContract.ST_Rate_UOM__c = subs.ST_Rate_UOM__c;
            exhibitionContract.Level_1_Rate__c = subs.Slab_Level_1_Rate__c;
            exhibitionContract.Level_2_Rate__c = subs.Slab_Level_2_Rate__c;
            exhibitionContract.Level_3_Rate__c = subs.Slab_Level_3_Rate__c;
            exhibitionContract.Level_4_Rate__c = subs.Slab_Level_4_Rate__c;
            exhibitionContract.Level_5_Rate__c = subs.Slab_Level_5_Rate__c;
            exhibitionContract.Max_Slab_Rate__c = subs.Max_Slab_Rate__c;
            exhibitionContract.ST_Start_Date__c = subs.SBQQ__StartDate__c;
            exhibitionContract.Level_1__c = subs.Exhibition_Standard_Price__r.Level_1__c;
            exhibitionContract.Level_2__c = subs.Exhibition_Standard_Price__r.Level_2__c;
            exhibitionContract.Level_3__c = subs.Exhibition_Standard_Price__r.Level_3__c;
            exhibitionContract.Level_4__c = subs.Exhibition_Standard_Price__r.Level_4__c;
            exhibitionContract.Level_5__c = subs.Exhibition_Standard_Price__r.Level_5__c;
            exhibitionContractedPriceList.add(exhibitionContract);
            if(customerExhiMap.containsKey(subs.SBQQ__Account__c)){
                customerExhiMap.get(subs.SBQQ__Account__c).add(subs.ST_Exhibition__c);
            }
            else{
                Set<Id> exiId = new Set<Id>{subs.ST_Exhibition__c};
                    customerExhiMap.put(subs.SBQQ__Account__c,exiId);
            }
        }
        
        try{
            if(!exhibitionContractedPriceList.isEmpty()){
                system.debug('===exhibitionContractedPriceList size : '+exhibitionContractedPriceList.size());
                insert exhibitionContractedPriceList;
            }
            
            if(!accMap.isEmpty()){
                system.debug('===acc details : '+accMap.values());
                update accMap.values();
            }            
        }
        catch(Exception ex)
        {
            system.debug('===Error occurred : '+ex.getStackTraceString()+', Error Message : '+ex.getMessage()+', Line Number : '+ex.getLineNumber());            
        }
        if(customerExhiMap.size() > 0 && customerExhiMap != null){
            Set<Id> existingContractIds = getExistingContractIds(customerExhiMap,newContractMap);
            if(existingContractIds.size() > 0 && existingContractIds != null){
                deactivateExhibitionPrice(existingContractIds,'DeactivationDueToNewContract');
                contractExpiration(existingContractIds);
            }
            
        }
        
    }
    
    public static void acrContractedPriceDeactivation(Map<Id,Contract> contractMap, String deactivationEvent){
        system.debug('CONT:::acrContractedPriceDeactivation');
        Set<Id> accIds = new Set<Id>();
        Map<Id,Id> orderContractIdMap = new Map<Id,Id>();
        for(Contract c : contractMap.values()){
            accIds.add(c.AccountId);
            orderContractIdMap.put(c.SBQQ__Order__c,c.Id);
        }
        List<ST_ACR_Contracted_Price__c> acrContractPrices2Update = new List<ST_ACR_Contracted_Price__c>();
        for(ST_ACR_Contracted_Price__c price : [SELECT Id,ST_Order__c,ST_Active__c,ST_Deactivation_Date__c,
                                                ST_Product__c,CreatedDate,ST_Contract__c 
                                                FROM ST_ACR_Contracted_Price__c
                                                WHERE ST_Customer_Account__c IN :accIds
                                                ORDER BY CreatedDate desc 
                                               ])
        {
            
            if(price.ST_Active__c && deactivationEvent == 'DeactivationDueToNewContract'
               && (!contractMap.containsKey(price.ST_Contract__c)) 
               //&& price.ST_Contract__c != orderContractIdMap.get(price.ST_Order__c) //Adel, do we need this ?
              ) 
            {
                price.ST_Active__c = false;
                price.ST_Deactivation_Date__c = system.today();
                acrContractPrices2Update.add(price);
            }
            else if(price.ST_Active__c && deactivationEvent == 'DeactivationDueToExpiration'){
                price.ST_Active__c = false;
                price.ST_Deactivation_Date__c = system.today();
                acrContractPrices2Update.add(price);
            }
            
        }
        if(acrContractPrices2Update.size() >0){
            try {
                update acrContractPrices2Update;
            } catch (DmlException ex) {
                System.debug('The Update on contractedPriceActivation Failed Exception***'+ex.getMessage());
            }
        }
    }
    
CharuDuttCharuDutt
Hii Sam
Try Below Code
@isTest
public class BstockCMtokensavetest {
    @isTest
    public static void unitTest(){
        Account Acc = new Account();
        Acc.Name = 'Test Account';
        insert Acc;
        Account Acc1 = new Account();
        Acc1.Name = 'Test Account 1';
        insert Acc1;
        Contract c = new Contract(
            AccountId = Acc.id
            ,ContractTerm = 12
            ,StartDate = system.today()
            ,OwnerExpirationNotice = '30'
            
        );
        insert c;
        c.AccountId = Acc1.id;
        update c;
        
        SBQQ__Subscription__c oSub = new SBQQ__Subscription__c();
        oSub.SBQQ__Contract__c = c.id;
        //fill All required Fields
        insert oSub;
        
        ST_ACR_Contracted_Price__c cp = new ST_ACR_Contracted_Price__c();
        cp.ST_Customer_Account__c = Acc1.id;
        cp.ST_Contract__c = c.Id;
        ST_Active__c = true;
        //fill All required Fields
        insert cp ;
        
        Map<Id,Contract> newContractMap = new Map<Id,Contract>();
        newContractMap.put(c.Id, c);
        BstockCMtokensave.contractExpiration(new set<Id>{c.Id},newContractMap);
        BstockCMtokensave.contractExpiration(newContractMap,'DeactivationDueToNewContract');
    }
    @isTest
    public static void unitTest2(){
        
        Account Acc = new Account();
        Acc.Name = 'Test Account';
        insert Acc;
        
        Contract c = new Contract(
            AccountId = Acc.id
            ,ContractTerm = 12
            ,StartDate = system.today()
            ,OwnerExpirationNotice = '30'
            
        );
        insert c;
        
        ST_ACR_Contracted_Price__c cp = new ST_ACR_Contracted_Price__c();
        cp.ST_Customer_Account__c = Acc.id;
        cp.ST_Contract__c = c.Id;
        ST_Active__c = true;
        //fill All required Fields
        insert cp ;

        Map<Id,Contract> newContractMap = new Map<Id,Contract>();
        newContractMap.put(c.Id, c);
        BstockCMtokensave.contractExpiration(newContractMap,'DeactivationDueToExpiration');
    }
}
Please Mark It As Best Answer If It Helps
Thank You!
samsam
Not working