+ Start a Discussion
RahulRahul 

Hello friends, Iam cloning a cpq quote and quote line item but the pricings for the cloned quote and line items are increasing slightly. Do anyone have idea why this happens? Please find the quote below. In debug logs its inserting correctly

public class OpportunityHandler_cpq{

public integer enddate=0;

public integer startdate=0;


    Public void CpqQuoteInsert( list<opportunity> listopp){
        
        set<id> oppids = new set<id>();
      
        for(opportunity op :listopp ) {
      
            if(op.SBQQ__Renewal__c == True &&  op.SBQQ__RenewedContract__c != null ){

              oppids.add(op.id);
              system.debug('First op id'+op.id);
            }
        }
        system.debug('oppids::'+oppids);  
        List<Opportunity> oppls = [select id,Renewal_Contract_Term__c,SBQQ__RenewedContract__c, formulapreviousopp__c, SBQQ__RenewedContract__r.SBQQ__Opportunity__c from opportunity where id IN : oppids];
        system.debug('oppls::'+oppls);  
        Map<Id, Id> oldVSnewOpp = new Map<Id, Id>();
   
        for(Opportunity opp: oppls) {
            system.debug('opp.SBQQ__RenewedContract__c::'+opp.SBQQ__RenewedContract__c);
            system.debug('opp.SBQQ__RenewedContract__r.SBQQ__Opportunity__c::'+opp.SBQQ__RenewedContract__r.SBQQ__Opportunity__c);
            system.debug('opp.formulapreviousopp__::'+opp.formulapreviousopp__c);
            
            if(opp.SBQQ__RenewedContract__c != null && opp.SBQQ__RenewedContract__r.SBQQ__Opportunity__c != null)
                oldVSnewOpp.put(opp.SBQQ__RenewedContract__r.SBQQ__Opportunity__c, opp.Id);
     
        }
        system.debug('oldVSnewOpp:'+oldVSnewOpp);
        List<Opportunity> oldOppls = [select id, name,
                                                (Select ACV_Auto_Renew_Increment_Amount__c, ACV__c, Account_Name__c, Auto_Renew__c, Contract_End_Date__c, CreatedById, CreatedDate, Document_Template__c, 
Id, IsDeleted, LastActivityDate, LastModifiedById, LastModifiedDate, LastReferencedDate, LastViewedDate, Name, Number_of_Licences__c, 
Opp_Closed__c, Original_Quote_id__c, OwnerId, Payment__c, Primary_Contact_First_Name__c, Product_Family__c, SBQQ__Account__c, 
SBQQ__AdditionalDiscountAmount__c, SBQQ__AverageCustomerDiscount__c, SBQQ__AveragePartnerDiscount__c, SBQQ__BillingCity__c, SBQQ__BillingCountry__c, 
SBQQ__BillingFrequency__c, SBQQ__BillingName__c, SBQQ__BillingPostalCode__c, SBQQ__BillingState__c, SBQQ__BillingStreet__c, SBQQ__ContractingMethod__c,
 SBQQ__CustomerAmount__c, SBQQ__CustomerDiscount__c, SBQQ__DaysQuoteOpen__c, SBQQ__DefaultTemplate__c, SBQQ__DeliveryMethod__c, SBQQ__DistributorDiscount__c,
 SBQQ__Distributor__c, SBQQ__DocumentStatus__c, SBQQ__EmailTemplateId__c, SBQQ__EndDate__c, SBQQ__ExpirationDate__c, SBQQ__FirstSegmentTermEndDate__c,
 SBQQ__GenerateContractedPrice__c, SBQQ__Introduction__c, SBQQ__Key__c, SBQQ__LineItemCount__c, SBQQ__LineItemsGrouped__c, SBQQ__LineItemsPrinted__c, 
 SBQQ__ListAmount__c, SBQQ__MarkupRate__c, SBQQ__MasterContract__c, SBQQ__NetAmount__c, SBQQ__Notes__c, SBQQ__Opportunity2__c, SBQQ__Ordered__c, 
 SBQQ__OriginalQuote__c, SBQQ__PartnerDiscount__c, SBQQ__Partner__c, SBQQ__PaymentTerms__c, SBQQ__PriceBook__c, SBQQ__PricebookId__c,
 SBQQ__PrimaryContact__c, SBQQ__Primary__c, SBQQ__QuoteProcessId__c, SBQQ__QuoteTemplateId__c, SBQQ__RegularAmount__c, SBQQ__SalesRep__c, 
 SBQQ__ShippingCity__c, SBQQ__ShippingCountry__c, SBQQ__ShippingName__c, SBQQ__ShippingPostalCode__c, SBQQ__ShippingState__c, SBQQ__ShippingStreet__c, 
 SBQQ__Source__c, SBQQ__StartDate__c, SBQQ__Status__c, SBQQ__SubscriptionTerm__c, SBQQ__TargetCustomerAmount__c, SBQQ__TotalCustomerDiscountAmount__c,
 SBQQ__Type__c, SBQQ__WatermarkShown__c, SystemModstamp
                                                from SBQQ__Quotes2__r where SBQQ__Primary__c =  true) 
                                    from opportunity where id IN :oldVSnewOpp.keySet()];
         system.debug('oldOppls:'+oldOppls);
     
        List<SBQQ__Quote__c>  quotels = new  List<SBQQ__Quote__c>();
        for(Opportunity oldOpp :oldOppls) {
            if(oldOpp.SBQQ__Quotes2__r.size() >0 ) {
                system.debug('oldOpp.SBQQ__Quotes2__r::'+oldOpp.SBQQ__Quotes2__r);
                system.debug('oldVSnewOpp.containsKey(oldOpp.id)::'+oldVSnewOpp.containsKey(oldOpp.id));
                if(oldVSnewOpp.containsKey(oldOpp.id)) {
                    system.debug('here!!!!!!');
                    SBQQ__Quote__c  quote1 =  oldOpp.SBQQ__Quotes2__r[0].clone();
                    quote1.Original_Quote_id__c=oldOpp.SBQQ__Quotes2__r[0].id;
                    
                    integer startdate = Integer.valueOf(String.valueOf(Math.roundToLong(oldOpp.SBQQ__Quotes2__r[0].SBQQ__SubscriptionTerm__c)));
                    
                    quote1.SBQQ__StartDate__c = oldOpp.SBQQ__Quotes2__r[0].SBQQ__StartDate__c.addmonths(startdate);
                    quote1.SBQQ__EndDate__c = (quote1.SBQQ__StartDate__c).addmonths(startdate);
                    
                 //  Integer enddate= Integer.valueOf(String.valueOf(Math.roundToLong(oldOpp.SBQQ__Quotes2__r[0].SBQQ__SubscriptionTerm__c)));
                    
              //   quote1.SBQQ__EndDate__c =system.today().addmonths(enddate);

                    system.debug('quote1.Original_Quote_id__c::'+quote1.Original_Quote_id__c);
                    quote1.SBQQ__Opportunity2__c = oldVSnewOpp.get(oldOpp.id);
                    system.debug('quote1::'+quote1);
                    quotels.add(quote1);
                } 
         
            }
     
        }
        system.debug('quotels::'+quotels);
        
        if(quotels.size()>0) {
            insert quotels;
        }
        
       
        
         set<id> Quoteid = new set<id>();
         Map<id, id> oldQuoteVSNewQuoteIds = new Map<id, id>();
         for(SBQQ__Quote__c qt : quotels){
            if(qt.Original_Quote_id__c != null){
                oldQuoteVSNewQuoteIds.put(qt.Original_Quote_id__c, qt.id);
            }
        }
        system.debug('oldQuoteVSNewQuoteIds:::'+oldQuoteVSNewQuoteIds);
        
        list<SBQQ__Quote__c> oldquotelist = [Select ACV_Auto_Renew_Increment_Amount__c, ACV__c, Account_Name__c, Auto_Renew__c, Contract_End_Date__c, CreatedById, CreatedDate, Document_Template__c, 
Id, IsDeleted, LastActivityDate, LastModifiedById, LastModifiedDate, LastReferencedDate, LastViewedDate, Name, Number_of_Licences__c, 
Opp_Closed__c, Original_Quote_id__c, OwnerId, Payment__c, Primary_Contact_First_Name__c, Product_Family__c, SBQQ__Account__c, 
SBQQ__AdditionalDiscountAmount__c, SBQQ__AverageCustomerDiscount__c, SBQQ__AveragePartnerDiscount__c, SBQQ__BillingCity__c, SBQQ__BillingCountry__c, 
SBQQ__BillingFrequency__c, SBQQ__BillingName__c, SBQQ__BillingPostalCode__c, SBQQ__BillingState__c, SBQQ__BillingStreet__c, SBQQ__ContractingMethod__c,
 SBQQ__CustomerAmount__c, SBQQ__CustomerDiscount__c, SBQQ__DaysQuoteOpen__c, SBQQ__DefaultTemplate__c, SBQQ__DeliveryMethod__c, SBQQ__DistributorDiscount__c,
 SBQQ__Distributor__c, SBQQ__DocumentStatus__c, SBQQ__EmailTemplateId__c, SBQQ__EndDate__c, SBQQ__ExpirationDate__c, SBQQ__FirstSegmentTermEndDate__c,
 SBQQ__GenerateContractedPrice__c, SBQQ__Introduction__c, SBQQ__Key__c, SBQQ__LineItemCount__c, SBQQ__LineItemsGrouped__c, SBQQ__LineItemsPrinted__c, 
 SBQQ__ListAmount__c, SBQQ__MarkupRate__c, SBQQ__MasterContract__c, SBQQ__NetAmount__c, SBQQ__Notes__c, SBQQ__Opportunity2__c, SBQQ__Ordered__c, 
 SBQQ__OriginalQuote__c, SBQQ__PartnerDiscount__c, SBQQ__Partner__c, SBQQ__PaymentTerms__c, SBQQ__PriceBook__c, SBQQ__PricebookId__c,
 SBQQ__PrimaryContact__c, SBQQ__Primary__c, SBQQ__QuoteProcessId__c, SBQQ__QuoteTemplateId__c, SBQQ__RegularAmount__c, SBQQ__SalesRep__c, 
 SBQQ__ShippingCity__c, SBQQ__ShippingCountry__c, SBQQ__ShippingName__c, SBQQ__ShippingPostalCode__c, SBQQ__ShippingState__c, SBQQ__ShippingStreet__c, 
 SBQQ__Source__c, SBQQ__StartDate__c, SBQQ__Status__c, SBQQ__SubscriptionTerm__c, SBQQ__TargetCustomerAmount__c, SBQQ__TotalCustomerDiscountAmount__c,
 SBQQ__Type__c, SBQQ__WatermarkShown__c, SystemModstamp,
                                              (Select CreatedById, CreatedDate, Id, IsDeleted, LastModifiedById, LastModifiedDate, Name, Opportunity__c, Product_Description__c, Product_Family__c, 
SBQQ__AdditionalDiscountAmount__c, SBQQ__AdditionalDiscount__c, SBQQ__AdditionalQuantity__c, SBQQ__AllowAssetRefund__c, SBQQ__BatchQuantity__c, 
SBQQ__BillingFrequency__c, SBQQ__BillingType__c, SBQQ__BlockPrice__c, SBQQ__Bundle__c, SBQQ__BundledQuantity__c, SBQQ__Bundled__c, SBQQ__CarryoverLine__c,
 SBQQ__ChargeType__c, SBQQ__ComponentCost__c, SBQQ__ComponentDiscountedByPackage__c, SBQQ__ComponentListTotal__c, SBQQ__ComponentSubscriptionScope__c, 
 SBQQ__ComponentTotal__c, SBQQ__ComponentUpliftedByPackage__c, SBQQ__ComponentVisibility__c, SBQQ__CompoundDiscountRate__c, SBQQ__ConfigurationRequired__c,
 SBQQ__ContractedPrice__c, SBQQ__CostEditable__c, SBQQ__Cost__c, SBQQ__CustomerPrice__c, SBQQ__CustomerTotal__c, SBQQ__DefaultSubscriptionTerm__c,
 SBQQ__Description__c, SBQQ__Dimension__c, SBQQ__DiscountScheduleType__c, SBQQ__DiscountSchedule__c, SBQQ__DiscountTier__c, SBQQ__Discount__c, 
 SBQQ__DistributorDiscount__c, SBQQ__DynamicOptionId__c, SBQQ__EffectiveEndDate__c, SBQQ__EffectiveQuantity__c, SBQQ__EffectiveStartDate__c,
 SBQQ__EndDate__c, SBQQ__Existing__c, SBQQ__Favorite__c, SBQQ__GenerateContractedPrice__c, SBQQ__GrossProfit__c, SBQQ__Group__c, SBQQ__Hidden__c,
 SBQQ__Incomplete__c, SBQQ__ListPrice__c, SBQQ__ListTotal__c, SBQQ__MarkupAmount__c, SBQQ__MarkupRate__c, SBQQ__Markup__c, SBQQ__MaximumPrice__c,
 SBQQ__MinimumPrice__c, SBQQ__NetPrice__c, SBQQ__NetTotal__c, SBQQ__NonDiscountable__c, SBQQ__NonPartnerDiscountable__c, SBQQ__Number__c,
 SBQQ__OptionDiscountAmount__c, SBQQ__OptionDiscount__c, SBQQ__OptionLevel__c, SBQQ__OptionType__c, SBQQ__Optional__c, SBQQ__OriginalPrice__c,
 SBQQ__OriginalUnitCost__c, SBQQ__PackageCost__c, SBQQ__PackageListTotal__c, SBQQ__PackageProductCode__c, SBQQ__PackageProductDescription__c, 
 SBQQ__PackageTotal__c, SBQQ__PartnerDiscount__c, SBQQ__PartnerPrice__c, SBQQ__PartnerTotal__c, SBQQ__PreviousSegmentPrice__c, 
 SBQQ__PreviousSegmentUplift__c, SBQQ__PriceEditable__c, SBQQ__PricingMethodEditable__c, SBQQ__PricingMethod__c, 
 SBQQ__PriorQuantity__c, SBQQ__ProductCode__c, SBQQ__ProductFamily__c, SBQQ__ProductName__c, SBQQ__ProductOption__c, SBQQ__Product__c, 
 SBQQ__ProrateMultiplier__c, SBQQ__ProratedListPrice__c, SBQQ__ProratedPrice__c, SBQQ__Quantity__c, SBQQ__Quote__c, SBQQ__RegularPrice__c, 
 SBQQ__RegularTotal__c, SBQQ__Renewal__c, SBQQ__RenewedAsset__c, SBQQ__RenewedSubscription__c, SBQQ__RequiredBy__c, SBQQ__SegmentIndex__c, 
 SBQQ__SegmentKey__c, SBQQ__SegmentLabel__c, SBQQ__Source__c, SBQQ__SpecialPriceDescription__c, SBQQ__SpecialPriceType__c, SBQQ__SpecialPrice__c,
 SBQQ__StartDate__c, SBQQ__SubscribedAssetIds__c, SBQQ__SubscriptionBase__c, SBQQ__SubscriptionCategory__c, SBQQ__SubscriptionPercent__c, 
 SBQQ__SubscriptionPricing__c, SBQQ__SubscriptionScope__c, SBQQ__SubscriptionTargetPrice__c, SBQQ__SubscriptionTerm__c,
 SBQQ__TaxCode__c, SBQQ__Taxable__c, SBQQ__TermDiscountSchedule__c, SBQQ__TermDiscountTier__c, SBQQ__TermDiscount__c, SBQQ__TotalDiscountAmount__c, 
 SBQQ__TotalDiscountRate__c, SBQQ__UnitCost__c, SBQQ__UnproratedNetPrice__c, SBQQ__UpgradedAsset__c, SBQQ__UpgradedQuantity__c, 
 SBQQ__UpgradedSubscription__c, SBQQ__UpliftAmount__c, SBQQ__Uplift__c, SBQQ__VolumeDiscount__c, SystemModstamp  FROM SBQQ__Lineitems__r) FROM SBQQ__Quote__c 
                                                   where id IN :oldQuoteVSNewQuoteIds.keySet()];
                                              
        List<SBQQ__QuoteLine__c> newQlils = new List<SBQQ__QuoteLine__c>();                                           
        for(SBQQ__Quote__c q1 :oldquotelist){
            for(SBQQ__QuoteLine__c qli : q1.SBQQ__Lineitems__r){
                SBQQ__QuoteLine__c newQli = qli.clone();
                newQli.SBQQ__Quote__c = oldQuoteVSNewQuoteIds.get(q1.id);
                newQli.SBQQ__Product__c= qli.SBQQ__Product__c;
                
                system.debug('qli:::'+qli.SBQQ__ProductFamily__c+ '::'+qli);
                system.debug('newQli:::'+newQli.SBQQ__ProductFamily__c+ '::'+newQli);

                newQlils.add(newQli);  
            } 
        }
        
        system.debug('newQlils::'+newQlils.size() + '::' +newQlils);
        if(newQlils.size() > 0) {
            insert newQlils;
        
        }
      /*  set<Id> newIds = new Set<Id>();
        for(SBQQ__QuoteLine__c qliNew1 :newQlils) {
            newIds.add(qliNew1.id);
        }
        
    
    List<SBQQ__QuoteLine__c>  updatedValue = [select id, name, SBQQ__CustomerPrice__c,SBQQ__ProductFamily__c  from SBQQ__QuoteLine__c where id IN :newIds];                                      
    system.debug('updatedValue::'+updatedValue);
    for(SBQQ__QuoteLine__c a1  :updatedValue) {
        system.debug('a1::'+a1);
    
    }
        */
        
      
       
     set<id> newQuoteid = new set<id>();
       
       for(SBQQ__Quote__c qt2 :quotels){
       newQuoteid.add(qt2.id);
       
       }
       
     //  list<string> updateTerm = new list<string>();
        
       list<SBQQ__Quote__c> newQuote =[select SBQQ__Primary__c   from  SBQQ__Quote__c where id=:newQuoteid];
       
        for(SBQQ__Quote__c  newprimary: newQuote){
        
        newprimary.SBQQ__Primary__c  = true;
        
        update newprimary;
       
       }

    } 
        
}
Tad Aalgaard 3Tad Aalgaard 3
There may be some price rules in affect that are changing the prices.

https://help.salesforce.com/articleView?id=cpq_price_rules_intro.htm&type=5

If you haven't done so, I'd suggest completing the Salesforce CPQ Basics trailhead.

https://trailhead.salesforce.com/en/content/learn/modules/sf_cpq