Happy Business Starts Here

How to Create Amendment Quote for Remove Product Using Apex Program?

Highlighted
Zuora Staff

How to Create Amendment Quote for Remove Product Using Apex Program?

Problem

Many of them trying to do amendments via code especially on the trigger of Opportunity. Please see below for the solution to create an amendment quote for Remove products using the apex program?

Solution

public class QuoteAmendmentRemoveProduct {
    
     public static void createAmendmentQuoteWithRemoveProduct(String AccountId, String ContactId, String ZuoraAccountId, String ZuoraSubscriptionId, String OppID){
        
        Datetime now = System.now();
        
        // Create Quote
        zqu__Quote__c quoteSObject = new zqu__Quote__c();
        quoteSObject.zqu__SubscriptionType__c = '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 = 'Remove Product';
        quoteSObject.zqu__SubscriptionTermStartDate__c = Date.Today();
        insert quoteSObject;
        
        // Invoking future method as "zqu.zQuoteUtil.getChargeGroups" will invoke 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> chargeGroupsToDelete = new List<zqu.zChargeGroup>();
        for (zqu.zChargeGroup chargeGroup : chargeGroups) {
         if ( chargeGroup.ratePlanName == 'ZELO Rate Plan') {
           chargeGroupsToDelete.add(chargeGroup);
         }
        }
        
        zqu.zQuoteUtil.deleteChargeGroups(chargeGroupsToDelete);
        
        //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