Happy Business Starts Here


Code to create a renewal quote for subscription

Does anyone have an example of how to create a renewal quote for an existing subscription?   This is something we'd want to do so when a subscription is made, we can generate a renewal opportunity in salesforce and attach a quote to it ready.



Re: Code to create a renewal quote for subscription

I will - give me a bit of time.

Maggie Longshore

Re: Code to create a renewal quote for subscription

Thanks Smiley Happy  I made a start then realised I had to get the Billing and Sold To contacts as Salesforce IDs, - but the Zuora Billing Accounts don't refer to those - which makes me wonder how ZQuotes UI works it out?  Does it look them up by email? what if the email has changed... !


Re: Code to create a renewal quote for subscription

The contacts are setup in the new subscription, The Salesforce contact Ids are not used later.

We manage all bill to and sold to contact details in Zuora.

They are not modified in the renewal process.

Maggie Longshore

Re: Code to create a renewal quote for subscription

This won't compile but should give you guidance on what to put in the quote.  We also use the default value plugin which I call to set other fields that we have as role based such as templates and taxcompanycode.


		quote = new zqu__Quote__c(					                  
					Name = opportunity.Name,                                      
					OwnerId = opportunity.OwnerId,                
					RecordtypeId  = typeHelper.typeForQuote(RecordTypeUtil.QUOTE_RECORD_TYPE_RENEWAL), 
					zqu__Account__c = subscription.Zuora__Account__c,
					zqu__Amendment_Name__c  = opportunity.Name,
					zqu__AutoRenew__c = false,
					zqu__BillingMethod__c = invoiceMethod,
					CurrencyIsoCode = subscription.Zuora__CustomerAccount__r.Zuora__Currency__c,
					zqu__Currency__c  = subscription.Zuora__CustomerAccount__r.Zuora__Currency__c,
					zqu__ExistSubscriptionID__c  = subscription.Zuora__Zuora_Id__c,
					zqu__Hidden_Subscription_Name__c  = subscription.Name,
					zqu__InitialTermPeriodType__c = 'Month',
					zqu__InitialTerm__c = integer.valueof(initialTerm),
					zqu__PaymentMethod__c = paymentMethod,
					zqu__RenewalSetting__c = 'RENEW_WITH_SPECIFIC_TERM',
					zqu__RenewalTermPeriodType__c = 'Month',
					zqu__RenewalTerm__c = integer.valueof(renewalTerm),
					zqu__StartDate__c = subscription.Zuora__SubscriptionEndDate__c,
					zqu__SubscriptionTermEndDate__c = subscription.Zuora__SubscriptionEndDate__c,
					zqu__SubscriptionTermStartDate__c = subscription.Zuora__SubscriptionStartDate__c,
					zqu__SubscriptionType__c  =  'Renew Subscription',
					zqu__SubscriptionVersion__c = subscription.Zuora__Version__c,
					zqu__Subscription_Term_Type__c = 'Termed',
					zqu__ValidUntil__c = subscription.Zuora__SubscriptionEndDate__c,
					zqu__ZuoraAccountId__c = subscription.Zuora__CustomerAccount__r.Zuora__Zuora_Id__c

I insert the opportunity and add the OpportunityId to the quote as an update after the quote is inserted. This is because of callouts to Zuora when renew is called. You can't make a callout between SF DML statements.


This adds the original products from the subscription and other Zuora data to to the quote.



insert opportunity;

quote.zqu__Opportunity__c = opportunity.Id;
DefaultValueCreateQuoteComponent dvcqc = new DefaultValueCreateQuoteComponent(subscription.Zuora__CustomerAccount__r);
dvcqc.populateDefaultFieldValue(quote, null);
upsert quote;
Maggie Longshore

Re: Code to create a renewal quote for subscription

That's supurb - thank you.  Got it making a quote.  


I now want to set Opportunity value based on the total. I assume previewQuote will work for that, but it's another callout (shame it can't it in one!) so I'm going to have @future it I think or something.


Re: Code to create a renewal quote for subscription

We do have custom metrics we calculate for every quote or product change that uses a future method. This is not run on the initial quote creation - but if a sales rep makes a change after all is created.


For the initial creation I have a chain of batch jobs that are run to create every renewal -

  1. creates opp and quote
  2. update metrics
  3. transitions billing periods if needed - like quarterly to annual
  4. update metrics (repeated)
  5. uplift prices
  6. update metrics (repeated)
Maggie Longshore

Re: Code to create a renewal quote for subscription


I got this working, at least an initial prototype.  I've hooked up a trigger to create an opportunity that fires a queable job to create the quote (with the renewQuote callout) which fires another queable to update the metrics and set the value on the opportunity.  I'll write it up and get it documented when it's all working and tested.

Thanks so much for your help Maggie.

Honor Student

Re: Code to create a renewal quote for subscription

Hello Nigel,


I've been reading this thread with interest and wanted to know how you ultimately got on with creating this. 


We are looking to go down the same path, so any advice and examples you maybe be able to provide me with would be very much appreciated.