CPQ – Orphaned Amendments – Solution!

There came an issue with amendment Opportunities/Quotes being orphaned after a renewal was closed won and Contracted! Below is a diagram showing the issue.

The process is as follows:

  1. Everything is fine (most important step)
  2. Create new Opportunity
    1. New Quote
  3. Close win Opportunity
  4. Click Contracted = True on Opportunity (or your favorite automation)
    1. Contract is auto-created
    2. Subscriptions are auto-created
  5. Click Renewal Forecast/Quoted on Contract (or your favorite automation)
    1. Renewals are auto-created
  6. Then we begin an Amendment on the Contract
    1. Click Amend and Amend on the Contract
    2. Save the Quote
  7. Then we go back to the Renewal Opportunity and close win it! BEFORE the Amendment Opportunity is completed…
  8. Click Contracted = True on the Renewal Opportunity (or your favorite automation)
    1. New Contract is auto-created
    2. Subscriptions are auto-created
  9. This is where the problem lies. Now, we have an in-flight Amendment on the old Contract that we do not wanna have to recreate on the new Contract.

Here is the proposed solution process. Once the system creates the new Contract and Subscriptions, another process is kicked off that reassigns the in-flight amendment Opportunity/Quote/Quote Lines to the new Contract/Subscriptions.

Custom Setting and Fieldset Design

The idea for Settings and Fieldset comes from the notion that this solution should not need maintenance after it is implemented.  Therefore we give Settings and a Metadata Type to allow for adjustments to the process.

Custom Setting Definition

Custom SettingPurpose
On/OffThe On/Off setting allows for control over if the automation runs or not, in the event any of the risks are being experienced.
Amendment Opp Stage(s)The Amendment Opp Stage(s) setting will determine which amendments to reassign to the new Contract.
Make sure to insert a record into this custom setting after creating it!

Fieldset Definition

ObjectFieldset NamePurpose
Quote LineReassigned Amendment FieldsThe Reassigned Amendment Fields fieldset on the Quote Line object will control which fields to copy from renewal Subscription -> Quote Line to existing amendment Quote Line.
Make sure to add fields to this fieldset after creating it!

Process Logic Design

The logic of this new process will be implemented in a future method.  This is to mitigate any potential heap size or timeout limit issues as much as possible.

  1. Trigger Mechanism: Flow Scheduled Action
    1. Flow: Contract :: On After Create
      1. Criteria: No Criteria
        1. Schedule: 0 Days After CreatedDate
        2. Action: Call Apex
          1. Action Name: ReassignAmendments
          2. Apex Class: ContractTriggerHelper
          3. Set Apex Variables: contractIds = Field Reference: [Contract].Id
    2. If the On/Off Setting is TRUE and the Contract -> Opportunity -> Renewed Contract != NULL,
      1. Query for necessary fields from the passed in contracts.
      2. Query for Opportunities that have:
        1. Account = Account on Contract that fired this process
        2. Amended Contract = this Contract -> Opportunity -> Renewed Contract
        3. Is not closed with Stage = Amendment Opp Stage(s) setting
      3. Query for Primary Quotes that have Opportunity in the above list of Opportunity IDs.
      4. Query for Subscriptions of the new (renewal) Contract (that fired this process).
      5. Query for fields to update on reassigned amendment quote lines.
      6. Query for quote lines of the above renewal subscriptions.
      7. Query for amendment Quote Lines that have Quote = above list of Quote IDs.
      8. Loop through these contracts.
        1. Loop through amendment Opportunities.
          1. If this Opportunity -> amended Contract == this Contract -> Opportunity -> renewed Contract
            1. Set this amendment Opportunity -> Amended Contract = New (renewal) Contract
            2. Loop through amendment Quotes.
              1. Set this Quote -> Master Contract = New (renewal) Contract (that fired this process)
              2. Loop through new (renewal) Subscriptions (of the Contract that fired this process).
                1. Loop through amendment Quote Lines
                  1. If this renewal Subscription -> Quote Line -> Renewed Subscription == this amendment Quote Line -> Upgraded Subscription,
                    1. Set this amendment Quote Line -> Upgraded Subscription = this new renewal Subscription.
                    2. Loop through Reassigned Amendment Fields fieldset’s fields.
                      1. Set this amendment Quote Line -> this fieldset field = this new renewal Subscription -> Quote Line -> this fieldset field value.
      9. Save Amendment Quote Lines
      10. Save Amendment Quotes
      11. Save Amendment Opps

And now you’re at the end! Of this post. Which means you get a prize. This is on the AppExchange. Go install it. 🙂


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.