Creating record in salesforce is easy, creating single record in one click is easy, But what about multiple records for the new upcoming months (ex. invoices , meetings , schedule maintenance )
This example will show how to create X records , related to an object in salesforce , 1 record to the Y months, for example 4 quotably invoices for opportunity once it been closed (you can adjust the use case for any other business need you have)
For the implementation we will use only declarative tools:
- Process Builder : will execute the a flow once an event happen , in our use case opportunity stage change into Close Won
- Flow will create the the records on the require dat
Start Here ..
For start we will create 2 new fields on opportunity object :
1. Invoice period : Pick list with options : Monthly , Quarterly , Biyearly
2. Number Of Invoices : Number
The Flow
Now let’s create a Flow that look like :
1 : Decision (IF condition),
counter < number of record to create
2: Populate fields where the magic is in the formula for new date:
ADDMONTHS(Today(),( {!Var_Number_AddMonths}*{!Var_Number_Counter}))
3: Increase counter
4: Add new Sobject to list
5: Insert Sobjects to database
That it .. we have a flow that create the records.
Why is the formula is everything we need ?
Let’s understand the magic of the formula, for example : create a 4 Quarterly invoices , we will pass Var_Number_AddMonths = 4 , and in the flow :
run#1 : Var_Number_Counter as 0 , we will get today date + 0*4 months
run#2 : Var_Number_Counter as 1 , we will get today date + 1*4 months
run#3 : Var_Number_Counter as 0 , we will get today date + 2*4 months
run#4 : Var_Number_Counter as 0 , we will get today date + 3*4 months
** For more information on ADDMONTHS
a link to the function is list below.
The Process builder
This is a small and easy to create process builder that call the flow (autolaunched flow) , the process will pass some inputs and the most important are the fields that we create on the opportunity object :
Invoice period and Number Of Invoices.
If needed you can also pass a date for the invoices to create according to it.
And here it is in action:
A package containing the example will be post ASAP.