Using Apex Class from Visual Flow

Visual flow it's a very nice and useful tool for creating process with minimum time effort.

However, there are cases where you can implement most of your requirement easily with the flow, except small part of the process which can be done only with code.
For that reason we can connect our flow to apex class, and from there we can do almost anything.

I will show it with simple example:
Visual flow to close case (update the status to closed) instead of the standard Close Case button, and sending email to inform specific person that the case was closed.
The last part can be done with apex class (actually it can also be done with workflow, but for the example I'll use apex code).


1.Creating the class.

This class must implement interface Process.Plugin, and must implement 2 methods:

Process.PluginResult invoke(Process.PluginRequest request)
            This is the function that the flow call from the class.

Process.PluginDescribeResult describe()
            this function describe to the flow which data the plugin need to receive.


global with sharing class cls_FlowSendEmailCase implements Process.Plugin {  

 //The main method to be implemented. The Flow calls this at runtime.   
 global Process.PluginResult invoke(Process.PluginRequest request) {   
  String caseId = (String) request.inputParameters.get('vCaseID');  
  String emailAddress = (String) request.inputParameters.get('vEmailAddress');  

  sendCaseEmail(caseId, emailAddress );  

  //return to Flow   
  Map<String,Object> result = new Map<String,Object>();  
  return new Process.PluginResult(result);   
 }   

 //Returns the describe information for the interface   
 global Process.PluginDescribeResult describe()  {   

  Process.PluginDescribeResult result = new Process.PluginDescribeResult();  
  result.Name = 'CloseCase';  
  result.Tag = 'Name';  

  result.inputParameters =new List<Process.PluginDescribeResult.InputParameter>();  

  result.inputParameters.add(  
   new Process.PluginDescribeResult.InputParameter('vCaseID',  
   Process.PluginDescribeResult.ParameterType.STRING, true));  

  result.inputParameters.add(  
   new Process.PluginDescribeResult.InputParameter('vEmailAddress',  
   Process.PluginDescribeResult.ParameterType.STRING, true));  

  return result;  
 }  

 public void sendCaseEmail(String caseId, String emailAddress) {  
  Case caseRec = [SELECT id, caseNumber FROM Case WHERE id=:caseId];  

  Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();  
  mail.setToAddresses(new String[] {emailAddress});  
  mail.setSubject('Case Closed. ' + caseRec.caseNumber);  
  mail.setPlainTextBody('This case has been closed ');  

  Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });  
 }  
}  



In this example the class received 2 parameters: the case ID and the email address (line 6-7).

In line 9, we call function sendCaseEmail, to send the email.

After creating such class (that implement Process.Plugin), we will see it in the flow designer, and will be able to use it by dragging it from the Palette tab to our flow.

2.Creating the flow.

This is our flow



First It will show kind of information message + option to send email and filling the email address.


Next we will first update the case to closed, and send email depend on the checkbox that user tag.

If the user select the check-box (equal true), then the flow will get to the CloseCase Send Case Email (with the plugin icon) and call the function invoke inside the class.

When we double clicking this square, we will show the variable we defined in the describe method, and we will be able to assign values to them.

The caseId in the class get the case id of the flow (should pass in the URL when calling the flow).
The email address get the email from the user input (stage 1 in the flow).



3.Finally add custom button in Case, that will call this flow.

The button will call URL:
/flow/CloseCase?vCaseID={!Case.Id}&retURL={!Case.Id}

The vCaseID is variable inside the flow (name must be exactly the same).

4.The final result:


Filling the email address and clicking 'Next'

Confirmation message

The email received and the case closed.

No comments:

Post a Comment

Retire of Permission on Profiles

If you are working as a Salesforce admin/developer you've probably heard somewhere that Salesforce is planning to make a significant cha...