Creating Record-Triggered Flow for Price Calculation
This section guides you to create your own flow and leverage the pricing engine for price calculations. The pricing engine is available as an apex action in the managed package. You can use this record-triggered flow to populate the pricing from SFS mobile app in the connected mode.
Perform the following steps to create a record-triggered flow that can be used to calculate the price for Product Consumed, Work Order Line Item, or Expense line items. The steps are explained for the Product Consumed line item as an example.
1. Log into your Salesforce org.
2. Create the apex class in the org as documented in the following code box. Include an InvocableMethod and a few InvocableVariables in the apex class that can be invoked from flows.
global with sharing class CalculatePrice {
@invocableMethod(label='Custom Calculate Price')
global static List<SVMXA360.CalculatePriceApexAction.PriceCalRequest> calculatePrice(List<SVMXA360.CalculatePriceApexAction.PriceCalRequest> lstInputParams) {
system.debug(LoggingLevel.INFO, 'calculatePrice() - enter.');
List<SVMXA360.CalculatePriceApexAction.PriceCalRequest> lstResponse = new List<SVMXA360.CalculatePriceApexAction.PriceCalRequest>();
try {
if (lstInputParams == null || lstInputParams.isEmpty()) {
//throw new SvmxSystem.SvmxNestedException(ErrorMessage.MISSING_REQUIRED_PARAMETER);
}
List<SObject> lstAllRecords = new List<SObject>();
for (SVMXA360.CalculatePriceApexAction.PriceCalRequest eachReq : SVMXA360.CalculatePriceApexAction.calculatePrice(lstInputParams)) {
if (eachReq.lstProductConsumedRecords != null && !eachReq.lstProductConsumedRecords.isEmpty()) {
lstAllRecords.addAll(eachReq.lstProductConsumedRecords);
for (SObject eachRecord : eachReq.lstProductConsumedRecords) {
SVMXA360.CalculatePriceApexAction.PriceCalRequest response = new SVMXA360.CalculatePriceApexAction.PriceCalRequest();
response.lstProductConsumedRecords = new List<SObject>();
response.lstProductConsumedRecords.add(eachRecord);
lstResponse.add(response);
}
}
if (eachReq.lstWOLIRecords != null && !eachReq.lstWOLIRecords.isEmpty()) {
lstAllRecords.addAll(eachReq.lstWOLIRecords);
for (SObject eachRecord : eachReq.lstWOLIRecords) {
SVMXA360.CalculatePriceApexAction.PriceCalRequest response = new SVMXA360.CalculatePriceApexAction.PriceCalRequest();
response.lstWOLIRecords = new List<SObject>();
response.lstWOLIRecords.add(eachRecord);
lstResponse.add(response);
}
}
if (eachReq.lstExpenseRecords != null && !eachReq.lstExpenseRecords.isEmpty()) {
lstAllRecords.addAll(eachReq.lstExpenseRecords);
for (SObject eachRecord : eachReq.lstExpenseRecords) {
SVMXA360.CalculatePriceApexAction.PriceCalRequest response = new SVMXA360.CalculatePriceApexAction.PriceCalRequest();
response.lstExpenseRecords = new List<SObject>();
response.lstExpenseRecords.add(eachRecord);
lstResponse.add(response);
}
}
}
if (lstInputParams[0].isUpdateRecords) {
Database.update(lstAllRecords);
}
} catch (DmlException ex) {
system.debug(LoggingLevel.INFO, 'calculatePrice(). Exception in updating source records: ' + ex);
throw ex;
}
catch (Exception ex) {
system.debug(LoggingLevel.INFO, 'calculatePrice(). Exception in price calculation: ' + ex);
throw ex;
}
system.debug(LoggingLevel.INFO, 'calculatePrice() - exit. lstResponse = ' + lstResponse);
return lstResponse;
}
}
|
Do not use the packaged apex action for price calculation. Create the apex class as per the above code.
|
3. Navigate to Setup > Process Automation > Flows.
4. Click New Flow.
5. Select Record-Triggered Flow.
6. Select any layout. The Flow Builder screen is displayed.
7. Click Choose Object, then select the object name as Product Consumed.
8. Provide any filtering conditions for the flow to trigger. For example, The Work Order Id field should not be blank or null.
9. Click Edit to edit the trigger and select A record is created or updated.
10. Click Done. The flow is updated with the configuration.
11. From the Element section on the left pane, drag and drop the Assignment element.
a. Provide the details such as Label, API Name, and Description for the Assignment element.
b. Create a resource of type Variable and Data Type as Record with Allow Multiple Values as True (Collection).
c. Assign the $Record global variable.
d. Click Done.
12. From the Element section on the left pane, drag and drop the Action element.
13. Search for the apex class created in step-2 and select it.
14. Provide the details such as Label, API Name, and Description.
15. Under the Select Objects section, select the objects for which you want to provide the apex action as input to calculate the price.
16. Under the Set Input Values section, provide the following parameters as the inputs to the apex action.
a. Under List of Product Consumed Records, provide the source created in step-11.2.
b. Disable List of Expense Records and Work Order Line Item Records.
c. For the remaining attributes, enable or disable as per your requirement.
d. Click Done.
17. Connect the Start element to Assignment, and the Assignment element to Action.
18. Click Save.
19. Provide Flow Label, Flow API Name, and Description, and click Save.
Related Topic