Happy Business Starts Here

Highlighted
Advanced Tutor

"List index out of bounds" when using zamend()

Hi,

 

I'm trying to use zamend() method on Apex in order to manually create a suspension on a Subscription, but when I run the following code the zamend() method raises the exception in the subject.

 

public static void suspend(Zuora__Subscription__c subscription, Date suspendDate, Date resumeDate, Boolean extendTerms) {
	final List<Zuora.zObject> amendments = new List<Zuora.zObject>();

	if(extendTerms) {
		amendments.add(createTermsAndConditionsAmendment(subscription, suspendDate, resumeDate));
	}

	amendments.add(createSuspendSubscriptionAmendment(subscription, suspendDate));
	amendments.add(createResumeSubscriptionAmendment(subscription, suspendDate, resumeDate));

	requestAmendmentsCreation(amendments);
}

public static void requestAmendmentsCreation(List<Zuora.zObject> amendments) {
	final Zuora.zApi zApiInstance = new Zuora.zApi();
	zApiInstance.zlogin();

	Zuora.zApi.AmendRequest amendRequest = new Zuora.zApi.AmendRequest();
	amendRequest.amendments = amendments;

	Zuora.zApi.AmendOptions amendmentOption = new Zuora.zApi.AmendOptions();
	amendmentOption.GenerateInvoice = false;
	amendmentOption.ProcessPayments = false;

	amendRequest.amendOptions = amendmentOption;

	Zuora.zApi.AmendRequest[] amendRequests = new Zuora.zApi.AmendRequest[]{ amendRequest };
	Zuora.zApi.AmendResult result = zApiInstance.zamend(amendRequests);
	System.debug(LoggingLevel.ERROR, '_requestAmendmentsCreation_ = ' + generateErrorMessage(result));
}

public static Zuora.zObject createSuspendSubscriptionAmendment(Zuora__Subscription__c subscription, Date suspendDate) {
	return createAmendment(subscription, 'SFDC Auto-generated Suspend Subscription Amendment', 'SuspendSubscription', suspendDate, null);
}

public static Zuora.zObject createSuspendSubscriptionAmendment(Zuora__Subscription__c subscription, String amendName, Date suspendDate) {
	return createAmendment(subscription, amendName, 'SuspendSubscription', suspendDate, null);
}

public static Zuora.zObject createResumeSubscriptionAmendment(Zuora__Subscription__c subscription, Date suspendDate, Date resumeDate) {
	return createAmendment(subscription, 'SFDC Auto-generated Resume Subscription Amendment', 'ResumeSubscription', suspendDate, resumeDate);
}

public static Zuora.zObject createResumeSubscriptionAmendment(Zuora__Subscription__c subscription, String amendName, Date suspendDate, Date resumeDate) {
	return createAmendment(subscription, amendName, 'ResumeSubscription', suspendDate, resumeDate);
}

public static Zuora.zObject createTermsAndConditionsAmendment(Zuora__Subscription__c subscription, Date suspendDate, Date resumeDate) {
	return createAmendment(subscription, 'SFDC Auto-generated Extends Subscription Term Amendment', 'TermsAndConditions', suspendDate, resumeDate);
}

public static Zuora.zObject createTermsAndConditionsAmendment(Zuora__Subscription__c subscription, String amendName, Date suspendDate, Date resumeDate) {
	return createAmendment(subscription, amendName, 'TermsAndConditions', suspendDate, resumeDate);
}

public static Zuora.zObject createAmendment(Zuora__Subscription__c subscription, String amendName, String amendType, Date startDate, Date endDate) {
	//final Long initialTerm = Long.valueOf(subscription.Zuora__InitialTerm__c.replaceAll('\\D+', ''));
	final Boolean isEvergreen = subscription.Zuora__TermSettingType__c == 'EVERGREEN';

	Zuora.zObject amendment = new Zuora.zObject('Amendment');
	amendment.setValue('ContractEffectiveDate', startDate);
	amendment.setValue('CustomerAcceptanceDate', startDate);
	amendment.setValue('EffectiveDate', startDate);
	amendment.setValue('ServiceActivationDate', startDate);
	amendment.setValue('SubscriptionId', subscription.Zuora__External_Id__c);
	amendment.setValue('RenewalSetting', (isEvergreen ? 'RENEW_TO_EVERGREEN' : 'RENEW_WITH_SPECIFIC_TERM'));
	amendment.setValue('Status', 'Completed');
	amendment.setValue('Type', amendType);

	switch on amendType {
		when 'SuspendSubscription' {
			amendment.setValue('CurrentTermPeriodType', 'Month');
			amendment.setValue('Name', amendName);
			amendment.setValue('RenewalTermPeriodType', 'Month');
			amendment.setValue('SuspendDate', startDate);
		}
		when 'ResumeSubscription' {
			final Long currentTerm = Long.valueOf(subscription.Zuora__CurrentTerm__c.replaceAll('\\D+', ''));

			amendment.setValue('CurrentTerm', currentTerm);
			amendment.setValue('CurrentTermPeriodType', 'Month');
			amendment.setValue('Name', amendName);
			amendment.setValue('RenewalTermPeriodType', 'Month');
			amendment.setValue('TermStartDate', subscription.Zuora__TermStartDate__c);
			amendment.setValue('ResumeDate', endDate);
		}
		when 'TermsAndConditions' {
			final Long initialTerm = Long.valueOf(subscription.Zuora__InitialTerm__c.replaceAll('\\D+', ''));
			final Long renewalTerm = Long.valueOf(subscription.Zuora__RenewalTerm__c.replaceAll('\\D+', ''));
			final Long currentTerm = endDate.daysBetween(subscription.Zuora__TermStartDate__c);

			amendment.setValue('AutoRenew', subscription.Zuora__AutoRenew__c);
			amendment.setValue('CurrentTerm', currentTerm);
			amendment.setValue('CurrentTermPeriodType', 'Day');
			amendment.setValue('Name', amendName);
			//amendment.setValue('InitialTerm', initialTerm);
			//amendment.setValue('InitialTermPeriodType', subscription.Zuora__InitialTermPeriodType__c);
			amendment.setValue('RenewalTerm', renewalTerm);
			amendment.setValue('RenewalTermPeriodType', subscription.Zuora__RenewalTermPeriodType__c);
			amendment.setValue('TermStartDate', subscription.Zuora__TermStartDate__c);
			amendment.setValue('TermType', subscription.Zuora__TermSettingType__c);
		}
		when else {
			throw new DomusException.UnsupportedOperationException('Unsupported amendment type: "' + amendType + '". Needs implementation yet.');
		}
	}

	return amendment;
}

 

Stack Trace:

 

stack trace.PNG

 

What can be the cause?

2 REPLIES 2
Zuora Support

Re: "List index out of bounds" when using zamend()

Hi @nmartop,
Thanks for posting this code on the community!
I am going to test this further at my end and get back to you. Can you please send me your Org ID in private message & grant access to it?






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

Zuora Support

Re: "List index out of bounds" when using zamend()

Thanks @nmartop for all the patience with us while working on this issue!

 

Solution --> "List index out of bounds: 0 in Class.Zuora.SoapHandler.handleResponseRemoteException: line 62, column 1" error usually occurs if the date is being passed in a wrong format. Correct date format is YYYY-MM-DD i.e 2019-02-25 for instance. 

 

The problem was due to the date fields (e.g. ContractEffectiveDate, CustomerAcceptanceDate, RenewalTerm, etc.) which were populated with the variables of Date type instead of formatting them early as strings with the format 'YYYY-MM-DD'.






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