Skip to content

Instantly share code, notes, and snippets.

@cba2ry
Forked from sbob909/gist:1855055
Created April 19, 2012 17:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save cba2ry/2422564 to your computer and use it in GitHub Desktop.
Save cba2ry/2422564 to your computer and use it in GitHub Desktop.
Apex Plug-In for Force.com Cloud Flow Designer Workbook with proper calculation for monthly payment
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');
// in addition to to adding the calculation below, to use this in the Visual Workflow Workbook, tutorial #3,
// a third input called "Rate" will need to be defined for the calculate quote step: the screen input field "Interest_Rate".
Double rate = (Double)request.inputParameters.get('Rate');
Double monthlyrate = rate/(12*100);
Double cMonthlyPayment = amount * (monthlyrate/(1 - Math.pow(1 + monthlyrate, -term)));
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),
new Process.PluginDescribeResult.InputParameter('Rate',
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 = 500000;
Double term = 360;
Double rate = 5;
inputParams.put('Amount', amount);
inputParams.put('Term', term);
inputParams.put('Rate', rate);
Process.PluginRequest request = new Process.PluginRequest(inputParams);
Process.PluginResult aresult = Plugin.invoke(request);
Double monthlyAmount = (Double) aresult.outputParameters.get('MonthlyPayment');
System.assertEquals(Math.roundToLong(monthlyAmount),2684);
Process.PluginDescribeResult describe = plugin.describe();
System.assertEquals(describe.InputParameters.size(), 3);
System.assertEquals(describe.InputParameters[0].name, 'Amount');
System.assertEquals(describe.InputParameters[1].name, 'Term');
System.assertEquals(describe.InputParameters[2].name, 'Rate');
System.assertEquals(describe.OutputParameters.size(), 1);
System.assertEquals(describe.OutputParameters[0].name, 'MonthlyPayment');
}
}
@gbreavin
Copy link

This will no longer save as of v28 of the API, as test methods cannot be define in a class.

@MikeEdwards24
Copy link

gbreavin's right, could it be updated?

Copy link

ghost commented Dec 1, 2013

I had to revise the version number of the API to get past this - which can be done on the Version settings tab within the Apex Class settings for the Mortgage Calculator

@smeridew
Copy link

version 27.0 of the API worked for me also

@Miroandme
Copy link

Only version 27.0 of the API works for this Apex Class

@Noctis-
Copy link

Noctis- commented Apr 29, 2014

Any plans on updating this to version 30 ?

(BTW, as a temp solution, if you're stuck on this: You can change the version of your Apex Class: Setup>App Setup>Classes. Navigate to this Apex class and click on version settings tab. Change it to 27 for the above to work.)

@sreynard
Copy link

sreynard commented May 8, 2014

To use a version after 27.0 of the API, please use these two samples (one for the Apex class, and one for a separate test class).

https://gist.github.com/sreynard/155eb3d6f1415b845749
https://gist.github.com/sreynard/33c41d50114f59c213d5

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