Happy Business Starts Here

How to Create Amendment Quote for Update Product?

Highlighted
Zuora Staff

How to Create Amendment Quote for Update Product?

Problem

How to create an amendment quote for update products using the apex program?

Solution

public class QuoteAmendmentUpdateProduct {
    
     public static void createAmendmentQuoteWithUpdateProduct(String AccountId, String ContactId, String ZuoraAccountId, String ZuoraSubscriptionId, String OppID){
        
        Datetime now = System.now();
        
        // Create Amendment Quote with mandatory fields (Get ZuoraAccountId, SubscriptionId from existing quote )
        zqu__Quote__c quoteSObject = new zqu__Quote__c();
        quoteSObject.zqu__SubscriptionType__c = 'Amend Subscription'; // Type should be Amend Subscription
        quoteSObject.zqu__ZuoraAccountId__c = ZuoraAccountId;
        quoteSObject.zqu__ExistSubscriptionId__c = ZuoraSubscriptionId;
        quoteSObject.zqu__Opportunity__c = OppID;
        //quoteSObject.zqu__ValidUntil__c = Date.newinstance(now.year(),now.month(),now.day());
        quoteSObject.zqu__BillToContact__c = ContactId;
        quoteSObject.zqu__SoldToContact__c = ContactId;
        quoteSObject.zqu__StartDate__c = Date.newinstance(now.year(),now.month(),now.day());
        quoteSObject.zqu__PaymentMethod__c = 'Credit Card';
        quoteSObject.zqu__PaymentTerm__c = 'Net 30';
        quoteSObject.zqu__Currency__c = 'USD';
        quoteSObject.zqu__InitialTerm__c = 12;
        quoteSObject.zqu__RenewalTerm__c = 12;
        quoteSObject.zqu__RenewalSetting__c = 'RENEW_WITH_SPECIFIC_TERM';
        quoteSObject.zqu__Amendment_Name__c = 'Update Product';
        quoteSObject.zqu__SubscriptionTermStartDate__c = Date.Today();

  //Insert Quote
  insert quoteSObject;
        
// Invoke future method since getChargeGroups will execute callout
        getChargeGroups(quoteSObject.Id, AccountId, ZuoraAccountId);
        
     }
	 
	 @future(callout=true)
     public static void getChargeGroups(String QuoteId, String sfdcAccountId, String ZuoraAccountId){
         
        // Load the charge groups with rate plan name "Basic Plan" that need to be updated
        List<zqu.zChargeGroup> chargeGroups = zqu.zQuoteUtil.getChargeGroups(QuoteId);
        List<zqu.zChargeGroup> chargeGroupsToUpdate = new List<zqu.zChargeGroup>();
        List<zqu.zChargeGroup> chargeGroupsToUpdate11 = new List<zqu.zChargeGroup>();
        for (zqu.zChargeGroup chargeGroup : chargeGroups) {
         if ( chargeGroup.ratePlanName == 'AirtelDTH') {
             system.debug('--- IN ----');
           chargeGroupsToUpdate.add(chargeGroup);
         }
        }
        
        List<zqu.zCharge> lstZCharge = new List<zqu.zCharge>();
        // Update the discount amount of all charges in the charge groups and recalculate the charges
        for( zqu.zChargeGroup groupT : chargeGroupsToUpdate ) {
         for ( zqu.zCharge charge : groupT.zCharges ) {
           charge.EFFECTIVE_PRICE = '140';
	        lstZCharge.add(charge);
            //chargeGroupsToUpdate11.add(groupT);
            
            system.debug('--- groupT ---'+groupT);
         }
        }
        
        zqu.zQuoteUtil.calculateChargesOnEffectivePriceChange(lstZCharge);
        // Persist the updates
        chargeGroupsToUpdate = zqu.zQuoteUtil.updateChargeGroups(chargeGroupsToUpdate);
        
		//Using Queueable to send the updated info to Zuora. Again it will make callout I'm using Queueable since we can't invoke future from future method
        SendToZuora updateJob = new SendToZuora(sfdcAccountId, ZuoraAccountId, QuoteId);
        
        ID jobID = System.enqueueJob(updateJob);
     }
}

//Send to Zuora
public class SendToZuora implements Queueable,  Database.AllowsCallouts {
    
    private List<Account> accounts;
    private ID parent;
    
    private String sfdcAccountId;
    private String ZuoraAccountId;
    Private String QuoteId;
    
    public SendToZuora(String sfdcAccountId, String ZuoraAccountId, String QuoteId) {
        this.sfdcAccountId = sfdcAccountId;
        this.ZuoraAccountId = ZuoraAccountId;
        this.QuoteId = QuoteId;
    }
    
    public void execute(QueueableContext context) {
         List<zqu.zQuoteUtil.ZBillingQuoteCollection> quotes = new List<zqu.zQuoteUtil.ZBillingQuoteCollection>();

        zqu.zQuoteUtil.ZBillingQuoteCollection quote = new zqu.zQuoteUtil.ZBillingQuoteCollection();
        quote.sfdcAccountId = sfdcAccountId; // SFDC CRM Account ID
        quote.zAccountId = ZuoraAccountId; // Zuora Billing Account ID
        quote.quoteRequests = new List<zqu.zQuoteUtil.ZBillingQuoteRequest>();
        zqu.zQuoteUtil.ZBillingQuoteRequest req =  new zqu.zQuoteUtil.ZBillingQuoteRequest();
        req.sfdcQuoteId = QuoteId; // SFDC Quote ID
        quote.quoteRequests.add(req);
        
        quotes.add(quote);
        
        List<zqu.zQuoteUtil.zBillingResult> results = zqu.zQuoteUtil.sendToZBilling(quotes);
        for ( zqu.zQuoteUtil.zBillingResult result : results ) {
            System.debug('Result: QuoteId = ' + result.sfdcQuoteId + ', Success = ' + result.success + ', message = ' + result.message );
        
        }  
    }
    
}

 

Thanks and Regards,
Prem Anandh
1 REPLY 1
Highlighted
Zuora Support

Re: How to Create Amendment Quote for Update Product?

Thanks @pramamoorthy for posting this solution to the Zuora community Smiley Happy






If you found my answer helpful, please give me a kudo ↑
Help others find answers faster by accepting my post as a solution √