Rollup summary field is an interesting custom field data type in salesforce. This will work when two objects have Master-Detail relationship but not for lookup relationship. Here I am going to explain a way to have rollup for lookup relationship.
Rollup summary fields for lookup relationship.
Using Flow and Process Builder we can do Rollup summary fields (Sum,Count,Max,Min) on object which is parent of lookup relationship.
For example, Employee with lookup to Company. Now we want to summarize the Employee upto Company and populate No.of employees in company, Maximum salary in company, Minimum salary in company fields.
Lets start to create Flow.
Step 1: Fast lookup to query all employees related to specific company
Drage & Drop - Fast lookup into empty canvas. This is the first element in flow.
Fill the name and unique name.
In Filters and Assignments section, select our child object name as lookup. Now, we will set criteries to query child records. You can set criteries based on your requirements. Here in Field box select the lookup field Company__c.
To lookup all related Employees to specific Company we have to define a variable (VarCompanyId) which will pass by Process Builder.
Give Unique Name, Data Type. Select Input Only for Input/Output Type to access this variable later in Process Builder.
Create one more variable to store the lookup query result.
In step 1 we have passed the company id(parent id) from process builder to query related Employees (child records) and stored the query results.
Step 2: Drage & Drop the Loop element from palette to iterate the records to do summary
To perform the logic inside the loop we need to specify a variable.
Step 3: We need 3 more variables to calculate No.of employees in company, Maximum salary in company, Minimum salary in company.
In the same way we have to create two more variables in the name of MaxSalary and EmpCount to hold Maximum salary and Employee count value.
Step 4: Calculate Employee Count using Assignment Logic. Drag and Drop the assignment element onto the canvas.
Step 5: Calculate Maximum salary
To calculate maximum salary we have to implement the logic using Decision element. Drag and Drop the Decision element onto canvas and fill the details and Its look like the following.
Here, the logic to find maximum salary is, if the employee record (coming in loop) Salary greater that MaxSalary (default value set it as 0) then we assign this value to MaxSalary variable using Assignment element.
Step 6 : Calculate Minimum salary
In the same way as we calculated Maximum salary, we can calculate the Minimum salary of employee in a company.
Drag and drop the Decision element onto canvas. Fill the details like following.
Fill the logic to find minimum salary and assign the minimum salary to MinSalary Variable.
Step 7 : Update Company Record (Parent) with Employee Count, Maximum salary, Minimum Salary values.
Step 8 : Make connections between the elements
We have finished with logics and have to connect the elements to make a flow as in below diagram.
When we connect Decision element with other elements, we have to set routing based on outcome values of Decision element.
Step 9 : Call flow from Process Builder
Now, we have to call this flow and pass Company Id from process builder when on create/update of child record.
We can test the process now by activating the flow and Process :)