Salesforce Auto generate renewal Opportunity with Line Items (i.e. Opportunity Products)






First,  let's take a look at the business scenario of this. 


Each Account(customer) have a set of opportunities and its Line Items which also know as Products of that Opportunity.
Once the salesperson enters those opportunity and product details in the Salesforce, those are likely to be renewed (quarterly, yearly...etc) by the customer if they are planning to go ahead with the same vendor in the next year as well.
Therefore, we need to again enter the same data into Salesforce which is almost similar to the previous details which are a time-consuming tedious task.   
Our objective is to automate this renewal scenario.

What are the available options

  1. Writing a class which is invoked by a Visualforce page to create the new opportunity and line items (by acquiring data through SOQL in the Apex class).
  2. Writing a trigger to fire on the opportunity once the particular conditions are met.
  3. Configuring a process builder follows by a flow to create the opportunity and line items.
From the above three methods, we are going to use the 3rd one. Why?
Because it's just the configurations, no need a particular knowledge on coding. Also, the configuration of the Lightning environment is improved.  

Solution 

Before Getting started.


Please make sure to do this in your Sandbox environment and do not go live until you properly test the solution according to your scenario.
I'm doing this configuration on the Summer 19 release.

To get an idea following is the final diagram of the flow, that we are going to configure.


Getting started

  • Go to flows by typing flows on the Setup  (clicking on your name -> Setup) quick find text box.
  • Click on New Flow.
  • Inside flow builder, from the left side menu -> select manage tab.
  • Now create new text variables as VarT_AccountId, VarT_OldOpportunityIdVarT_NewOpportunityIdVarT_PricebookId andVarT_OldOpportunityName to store the Account Id, old Opportunity Id, new Opportunity Id, existing Opportunity Price Book Id and old Opportunity Name respectively.
Toolbox Manager




Adding text variable resource

Get all the line items for the opportunity
  • Then, as the first element of the flow, we need to get the line items of the opportunity which is going to be renewed. 
  • Drag and drop Get Records data element from the toolbox.
  • Enter the name To get all Line Items from existing opportunity.
  • Select Object Opportunity Product
  • For criteria select OpportunityId {!VarT_OldOpportunityId}, as shown in the following screenshots.

  • In here, create a record collection variable of type Opportunity Product.


  • Select the fields to store the values.







Create a renewal opportunity

  • For this, we will use the Create Records element. Drag-and-drop Create Records element (Enter the name Create new opportunity).
  • Then map the fields according to the renewal opportunity as in the following screenshots.



  • Map the fields



  • Store the opportunnity ID




Decision Element
  • Now we will use the Decision element to check the SObject Collection Variable {!SOC_OLI} size. If the SObject Collection Variable is not equal to null, then we will go ahead else do nothing. You can take help from the following  screenshot to create a  Decision Element






Loop Element

  • The next step is to extract the opportunity line items from SObject Collection Variable {!SOC_OLI} one by one. For this, we will use Loop ElementTo do this Drag and drop a Loop Element ( Give the name Extract 1 by 1) onto the canvas and loop through SObject Collection Variable {!SOC_OLI} and save the current value in an SObject Variable {!SObjectV_OLI}  as shown in the following screenshot.



  • Now Drag and drop an Assignment Element ( Give the name Assign data into new a SObject Variable) onto the canvas and map the fields according to the following screenshot


  • The next step is to add all SObject Variables into an SObject Collection Variable, So at the end, we will create records in OpportunityLineItem. Drag and Drop Assignment Element (Give the name All in one) on the window and assign the value from SObject Variable {!NEWOLI} to SObject Collection Variable {!SOCNewOLI}, as shown in the following screenshot





  • The final task is to create records in the OpportunityLineItem object. For this, we will use the Create Records Element. Drag and drop a Create Records ( Give the name Create New OLI) onto the canvas and map the field according to the following screenshot


  • Save your flow with name Auto-generate renewal Opportunity WIth OLI and close the canvas. Don’t forget to Activate the Flow.
  • Your final flow should look like the initial flow diagram at the top of this article.

Launch Flow from process builder.

  • On the setup quick find text box type process builder and click new and fill the details as below and click save.
  • The process starts when the record is changed.



  • In here, our object is Opportunity and when a record is created or edited the process should execute.



  • Now we need to Define Criteria for this Action Group
  • Opportunity stage is closed won AND close date is not null.
  • Refer the below screenshot



  • My requirement is to immediately create the renewal opportunity once after the criterias are met.
  • Now Add Action inside immediate actions and select the type as flow.
  • Give the action name and selected the flow we have created previously.
  • Configure flow variables with the values as below.




  • Now, save the process. 
  • It'll look like the following screenshot.



  • Now activate the process.
Note
  • Don't change the API name of the flow when you are modifying it. Just update the version.
  • Otherwise, you may need to change the flow name in the process.




Comments

  1. Hi Chanuka, I amfollowing this to create my flow. Can you tell me step by step how you created the "record collection variable" - {!SOC_OLI}.I don't see it here!

    Also, VarT_AccountId, VarT_OldOpportunityId, VarT_NewOpportunityId, VarT_PricebookId andVarT_OldOpportunityName should all be text variables meaning the "Data Type" should be"Text"?Thanks

    ReplyDelete
  2. Once you drag and drop the Create Record element, In the variable selection option there's an option call "+ New Resource" . From there you could create the variable {!SOC_OLI} OR you could use the manager tab to initially create the resource.
    Yes, Those are text.

    ReplyDelete
  3. Very much appreciate the post, but I'm running into the same issue as Shunn. I am trying to create a new resource, but everytime I create, it doesn't take, so I'm prompted to create it again, but then SFDC tells me that the Name is already taken. Not sure what's happening. I was able to follow the process without line items, so thanks for that!! But I really do need the line items as well.

    ReplyDelete
  4. Pretty sure it's easier to just write a trigger.

    ReplyDelete

Post a Comment

Popular posts from this blog

How to fix SoapUI freeze in Mac OS

Salesforce Create multiple child records based on a number field in the parent using flow