Skip to content

Instantly share code, notes, and snippets.

@sbob909
Created February 17, 2012 19:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save sbob909/1855055 to your computer and use it in GitHub Desktop.
Save sbob909/1855055 to your computer and use it in GitHub Desktop.
Apex Plug-In for Force.com Cloud Flow Designer Workbook
global class MortgageCalculator implements Process.Plugin
{
global Process.PluginResult invoke(Process.PluginRequest request)
{
Double amount = (Double)request.inputParameters.get('Amount');
Double term = (Double)request.inputParameters.get('Term');
// Magic here
Double cMonthlyPayment;
cMonthlyPayment=2750;
Map<String, Object> result = new Map<String, Object>();
result.put('MonthlyPayment', cMonthlyPayment);
return new Process.PluginResult(result);
}
global Process.PluginDescribeResult describe()
{
Process.PluginDescribeResult result = new Process.PluginDescribeResult();
result.description='This plug-in generates a monthly payment quote given the term and amount.';
result.tag='Mortgage Quote';
result.inputParameters = new List<Process.PluginDescribeResult.InputParameter> {
new Process.PluginDescribeResult.InputParameter('Amount',
Process.PluginDescribeResult.ParameterType.DOUBLE, true),
new Process.PluginDescribeResult.InputParameter('Term',
Process.PluginDescribeResult.ParameterType.DOUBLE, true)
};
result.outputParameters = new List<Process.PluginDescribeResult.OutputParameter> {
new Process.PluginDescribeResult.OutputParameter('MonthlyPayment',
Process.PluginDescribeResult.ParameterType.DOUBLE)
};
return result;
}
public static testmethod void testAll()
{
MortgageCalculator plugin = new MortgageCalculator();
Map<String,Object> inputParams = new Map<String,Object>();
Double amount = 100000;
Double term = 25;
inputParams.put('Amount', amount);
inputParams.put('Term', term);
Process.PluginRequest request = new Process.PluginRequest(inputParams);
Process.PluginResult aresult = Plugin.invoke(request);
Double monthlyAmount = (Double) aresult.outputParameters.get('MonthlyPayment');
System.assertEquals(monthlyAmount,2750);
Process.PluginDescribeResult describe = plugin.describe();
System.assertEquals(describe.InputParameters.size(), 2);
System.assertEquals(describe.InputParameters[0].name, 'Amount');
System.assertEquals(describe.InputParameters[1].name, 'Term');
System.assertEquals(describe.OutputParameters.size(), 1);
System.assertEquals(describe.OutputParameters[0].name, 'MonthlyPayment');
}
}
@sbob909
Copy link
Author

sbob909 commented Feb 17, 2012

Tutorial #3 of the Cloud Flow Designer workbook (see http://wiki.developerforce.com/) uses this Apex class as a plug-in for a flow.

@egui
Copy link

egui commented Mar 27, 2012

There's no magic here :)
It's returning the same Monthly Payment whatever the term and amout...

@escully
Copy link

escully commented Apr 28, 2012

If you add a third input parameter called 'interest' then the following will work to change the amount in the appropriate way (monthly payments increase as interest rate increases and term decreases but total payment decreases as term decreases) albeit not fully mathematically correctly.
Double monthlyPayment;
monthlyPayment=amount_(1+interest/100_term/12)/term;
You will need to make a few other changes as needed to account for the new input parameter and to change the output to the new calculated variable.

@sbob909
Copy link
Author

sbob909 commented May 7, 2012

See https://gist.github.com/2422564 for the latest gist that supports the workbook. Thanks Chris!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment