I have a screen flow on case record page. The flow starts with this screen -
On clicking the Next button an apex action will run to check all the fields for null values. If there are fields with null values then a message is shown to the user to add values to those fields and a Finish button to restart the flow.
If the Apex Action didn't find any null fields then a txt file is generated and added to the files related list of the case record.
Now everytime this flow is started or after clicking the Finish button on the above screen the flow will check for an existing file and if found the flow will provide the user with the below message. The issue I am having is while on this screen, if the users make a change to any of required fields like setting a field to null, the next step in the flow which is the Apex Action to check for null fields does not seem to pickup those fields with null values and it directly goes to the final screen which says the file has been generated. How do I have the Apex Action to pickup values in realtime.
Hoping I was able to explain the issue well.
UPDATE - Adding the Apex Action code -
public with sharing class CheckForNullFieldsService {
@InvocableMethod(
label='Check for Null Values Action'
description='Method to check for null values in fields.'
)
public static List<Results> checkForNullValues(List<Requests> input) {
List<Results> results = new List<Results>();
for (Requests request : input) {
Results result = new Results();
result.fieldsWithNullValues = '';
result.status = 'Success';
result.errorMessage = '';
try {
String caseId = request.caseRecord.Id;
Case record = [
SELECT
Id,
SuppliedFirstName__c,
SuppliedLastName__c,
FROM Case
WHERE Id = :caseId
];
Map<String, String> fieldsToCheck = new Map<String, String>{
'SuppliedFirstName__c' => 'First Name',
'SuppliedLastName__c' => 'Last Name',
};
// Loop over the fields to check
for (String fieldApiName : fieldsToCheck.keySet()) {
// Check if the field value is null
if (record.get(fieldApiName) == null) {
String fieldName = fieldsToCheck.get(fieldApiName);
result.fieldsWithNullValues += '• ' + fieldName + '\n';
}
}
//Add the error message if any fields were null
if (!String.isBlank(result.fieldsWithNullValues)) {
result.errorMessage =
'The following fields are required and cannot be left blank: \n' +
result.fieldsWithNullValues +
'Please provide valid values.';
result.status = 'Error';
}
} catch (Exception e) {
result.status = 'Error';
result.errorMessage = 'An error occurred: ' + e.getMessage();
}
System.debug('result: ' + result);
results.add(result);
}
return results;
}
public class Requests {
@InvocableVariable(label='SObject API Name' required=true)
public String sobjectApiName;
@InvocableVariable(label='Case Record')
public Case caseRecord;
@InvocableVariable(label='WorkOrderOrder')
public WorkOrder workOrderRecord;
}
public class Results {
@InvocableVariable(label='Fields With Null Values' required=true)
public String fieldsWithNullValues;
@InvocableVariable(label='Status' required=true)
public String status;
@InvocableVariable(label='Error Message' required=true)
public String errorMessage;
}
}
These are the case fields that the flow will check for null values -
After running through the flow once a validation file is generated and then now I am on this screen which is run at the beginning of the flow to check if a file already exists after clicking on the Finish to generate the validation file.
And now while on this screen if I remove the data for one of the fields, for e.g the Phone field as marked in the snip below, clicking on regenerate which will go through the null fields check again does not seem to pickup that field with null value and show it to the user.
Here is the System.debug statement for the Phone field, the flow still uses the old value of that field.
And this is how the Apex action is setup in the flow -
Flow Diagram -