Difference between revisions of "Getting Started with the Java API"
imported>Aeric m (Text replace - 'Settings > ' to 'File:GearIcon.png > Setup > ') |
imported>Aeric |
||
(37 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
===About the Java API=== | |||
The Java APIs are used when writing custom [[Classes]] that can be invoked from Rules. | |||
The examples on the remainder of this page take you through the process of working with Java API, step by step. | |||
{{:Common:Java API Considerations}} | |||
:'' | ''Learn More:'' | ||
:* [[Java Code Samples]] - an initial class template and advanced, ready-to-copy code samples | |||
:* [[Java Debugging Tips]] - for help debugging your application | |||
:* [[Java API]] - for detailed descriptions of the classes, objects and API calls used in these examples | |||
:* [[Localization#Java Programming]] - learn how to take into account localization of data for users | |||
===Add an Account Record=== | ===Add an Account Record=== | ||
Line 26: | Line 21: | ||
====Set up a Parameters Object==== | ====Set up a Parameters Object==== | ||
Data is passed to the platform using a <tt>Parameters</tt> object. The <tt>Parameters</tt> object holds key-value pairs, where each key corresponds to a field name and each value corresponds to a field value in the database. Set the key-value pairs and then pass the <tt>Parameters</tt> object as an argument when the record is added. | Data is passed to the platform using a <tt>Parameters</tt> object. The <tt>Parameters</tt> object holds key-value pairs, where each key corresponds to a field name and each value corresponds to a field value in the database. Set the key-value pairs and then pass the <tt>Parameters</tt> object as an argument when the record is added. (To learn more about the parameters passed to a Java method, see the [[Standard Parameters]].) | ||
To set up a Parameters Object, create an instance of <tt>Parameters</tt> to hold the key-value pairs for the record by calling [[ | To set up a Parameters Object, create an instance of <tt>Parameters</tt> to hold the key-value pairs for the record by calling [[Java API:Utility functions#getParametersInstance|<tt>getParametersInstance</tt>]]. This instance is named <tt>addAccountParams</tt>: | ||
< | :<syntaxhighlight lang="java" enclose="div"> | ||
Parameters addAccountParams = Functions.getParametersInstance(); | |||
</syntaxhighlight> | |||
To see the fields that are defined in an object: | To see the fields that are defined in an object: | ||
#Open a web browser and [[Login]] to the platform | #Open a web browser and [[Login]] to the platform | ||
#Click ''' | #Click '''[[File:GearIcon.png]] > Customization > Objects > {object} > Fields''' | ||
#View the field list | #View the field list | ||
Add the key-value pairs for the database fields to <tt>addAccountParams</tt> by calling <tt>Parameters.add</tt>: | Add the key-value pairs for the database fields to <tt>addAccountParams</tt> by calling <tt>Parameters.add</tt>: | ||
< | :<syntaxhighlight lang="java" enclose="div"> | ||
addAccountParams.add("name","Hello World Account"); | addAccountParams.add("name","Hello World Account"); | ||
addAccountParams.add("number","0000001"); | addAccountParams.add("number","0000001"); | ||
Line 47: | Line 44: | ||
addAccountParams.add("phone", "222-222-2222"); | addAccountParams.add("phone", "222-222-2222"); | ||
addAccountParams.add("website", "www.helloworldaccount.com"); | addAccountParams.add("website", "www.helloworldaccount.com"); | ||
</ | </syntaxhighlight> | ||
====Call addRecord==== | ====Call addRecord==== | ||
To add a new record, call <tt>addRecord</tt> and pass it an object identifier and the <tt>addAccountParams</tt> object. The values you added to <tt>addAccountParams</tt> are written to the account record in the database. | To add a new record, call <tt>addRecord</tt> and pass it an object identifier and the <tt>addAccountParams</tt> object. The values you added to <tt>addAccountParams</tt> are written to the account record in the database. | ||
< | :<syntaxhighlight lang="java" enclose="div"> | ||
Result accountAddResult = Functions.addRecord("ACCOUNT", addAccountParams); | |||
</syntaxhighlight> | |||
Like <tt>addRecord</tt>, many of the Java API record handling calls have an <tt>objectID</tt> parameter the object is specified ("ACCOUNT" in this example). In the <tt>objectID</tt> parameter, specify the [[Object Type Identifier]]. | Like <tt>addRecord</tt>, many of the Java API record handling calls have an <tt>objectID</tt> parameter the object is specified ("ACCOUNT" in this example). In the <tt>objectID</tt> parameter, specify the [[Object Type Identifier]]. | ||
Line 60: | Line 58: | ||
The <tt>addRecord</tt> code (as well as several other Java API calls) returns an instance of the <tt>Result</tt> class. It is possible to check member variables in the <tt>Result</tt> object by calling its methods. For example, the code below makes a Java API <tt>debug</tt> utility call, making a nested call to <tt>Result.getMessage</tt> which returns a string indicating whether the call succeeded. | The <tt>addRecord</tt> code (as well as several other Java API calls) returns an instance of the <tt>Result</tt> class. It is possible to check member variables in the <tt>Result</tt> object by calling its methods. For example, the code below makes a Java API <tt>debug</tt> utility call, making a nested call to <tt>Result.getMessage</tt> which returns a string indicating whether the call succeeded. | ||
< | :<syntaxhighlight lang="java" enclose="div"> | ||
Logger.info("Result of addRecord for Account:" + accountAddResult.getMessage(), "Add"); | |||
</syntaxhighlight> | |||
The string specified in the <tt>debug</tt> call is written to the debug log. | The string specified in the <tt>debug</tt> call is written to the debug log. | ||
To view the Debug Log: | To view the Debug Log: | ||
:Click '''[[File:GearIcon.png]] > | :Click '''[[File:GearIcon.png]] > Customization > Developer Resources > Debug Log''' | ||
:When the code runs, use the Debug Log to troubleshoot problems during development. Many other <tt>debug</tt> calls are included in these code samples. | :When the code runs, use the Debug Log to troubleshoot problems during development. Many other <tt>debug</tt> calls are included in these code samples. | ||
Line 79: | Line 79: | ||
If the call is not successful, make a Java API <tt>throwError</tt> call, otherwise make a <tt>Result.getID</tt> call and continue | If the call is not successful, make a Java API <tt>throwError</tt> call, otherwise make a <tt>Result.getID</tt> call and continue | ||
< | :<syntaxhighlight lang="java" enclose="div"> | ||
if(accountAddResult.getCode() < 0) | if(accountAddResult.getCode() < 0) | ||
String msg = "Function: Add Account"; | String msg = "Function: Add Account"; | ||
Logger.info(msg + ":\n" + accountAddResult.getMessage(), "Add"); // Log details | |||
Functions.throwError(msg + "."); | Functions.throwError(msg + "."); // Error message | ||
else | else | ||
return accountAddResult.getID(); | return accountAddResult.getID(); | ||
</ | </syntaxhighlight> | ||
===Add a Contact Record=== | ===Add a Contact Record=== | ||
Here, you following the same process as adding an Account record to add a Contact Record: | |||
*Set up a Parameters Object | :*Set up a Parameters Object | ||
*Create an instance of <tt>Parameters</tt> by calling <tt>getParametersInstance</tt> | :*Create an instance of <tt>Parameters</tt> by calling <tt>getParametersInstance</tt> | ||
*Check the Result | :*Check the Result | ||
< | :<syntaxhighlight lang="java" enclose="div"> | ||
Parameters addContactParams = Functions.getParametersInstance(); | |||
</syntaxhighlight> | |||
Then set the key-value pairs in the <tt>Parameters</tt> instance: | Then set the key-value pairs in the <tt>Parameters</tt> instance: | ||
< | :<syntaxhighlight lang="java" enclose="div"> | ||
addContactParams.add("first_name", "John"); | addContactParams.add("first_name", "John"); | ||
addContactParams.add("last_name", "Smith"); | addContactParams.add("last_name", "Smith"); | ||
Line 105: | Line 107: | ||
addContactParams.add("flag_primary_contact", "true"); | addContactParams.add("flag_primary_contact", "true"); | ||
addContactParams.add("street", "12345 Lake st"); | addContactParams.add("street", "12345 Lake st"); | ||
</ | </syntaxhighlight> | ||
Call <tt>addRecord</tt>, specifying "CONTACT" as the object identifier and passing the <tt>Parameters</tt> object you set up above. | Call <tt>addRecord</tt>, specifying "CONTACT" as the object identifier and passing the <tt>Parameters</tt> object you set up above. | ||
< | :<syntaxhighlight lang="java" enclose="div"> | ||
Result contactAddResult = Functions.addRecord("CONTACT", addContactParams); | |||
</syntaxhighlight> | |||
Make a nested call to <tt>Result.getMessage</tt> to write to the debug log: | Make a nested call to <tt>Result.getMessage</tt> to write to the debug log: | ||
< | :<syntaxhighlight lang="java" enclose="div"> | ||
Logger.info("Result of addRecord for Contact - John Smith:" | |||
+ contactAddResult.getMessage(), "Add"); | |||
</syntaxhighlight> | |||
Check the return code by calling <tt>Result.getCode</tt>: | Check the return code by calling <tt>Result.getCode</tt>: | ||
< | :<syntaxhighlight lang="java" enclose="div"> | ||
if(contactAddResult.getCode() < 0) | if(contactAddResult.getCode() < 0) | ||
String msg = "Getting Started: Add John Smith"; | String msg = "Getting Started: Add John Smith"; | ||
Logger.info(msg + ":\n" + contactAddResult.getMessage(), "Add"); // Log details | |||
Functions.throwError(msg + "."); | Functions.throwError(msg + "."); // Error message | ||
else | else | ||
return contactAddResult.getID(); | return contactAddResult.getID(); | ||
</ | </syntaxhighlight> | ||
===Update a Record=== | |||
This code updates a record. | |||
:<syntaxhighlight lang="java" enclose="div"> | |||
public void changeRecordValue(Parameters p) throws Exception | |||
This code | |||
{ | { | ||
try { | |||
String objectID = p.get("object_id"); | |||
String recordID = p.get("id"); | |||
// Specify the parameters to modify. (Only specified params are changed.) | |||
Parameters params = Functions.getParametersInstance(); | |||
params.add("field_name", "new_value"); | |||
// Result.getCode() >= 0 on success, -1 on failure | |||
Result r = Functions.updateRecord(objectID, recordID, params); | |||
if (r.getCode() < 0) { | |||
Functions.throwError("Update failed\n" + r.getMessage() ); | |||
} | |||
} catch (Exception e) { | |||
Functions.throwError( e.getMessage() ); | |||
} | |||
Result | |||
Functions. | |||
} | } | ||
</ | </syntaxhighlight> | ||
=== | ===Change Record Ownership=== | ||
This code changes a record's owner. Note that it does not reassign any tasks that may be attached to the record. (Additional code would be required to do that.) | |||
:<syntaxhighlight lang="java" enclose="div"> | |||
public void changeOwner(Parameters p) throws Exception | |||
{ | { | ||
try { | |||
String objectID = p.get("object_id"); | |||
String recordID = p.get("id"); | |||
String newOwner = "..ID of owner with desired team.."; | |||
boolean notifyNewOwner = true; | |||
= | |||
// Result.getCode() >= 0 on success, -1 on failure | |||
Result r = Functions.changeOwnerShipInfo( | |||
objectID, recordID, newOwner, notifyNewOwner); | |||
if (r.getCode() < 0) { | |||
Functions.throwError("Update failed\n" + r.getMessage() ); | |||
} | |||
} catch (Exception e) { | |||
Functions.throwError( e.getMessage() ); | |||
} | |||
Result | |||
< | |||
{ | |||
} | } | ||
</syntaxhighlight> | |||
<noinclude> | <noinclude> | ||
[[Category:Java API|1]] | [[Category:Java API|1]] | ||
</noinclude> | </noinclude> |
Latest revision as of 21:08, 2 February 2015
About the Java API
The Java APIs are used when writing custom Classes that can be invoked from Rules.
The examples on the remainder of this page take you through the process of working with Java API, step by step.
- Considerations
-
- The Java 6 syntax and feature set are supported in custom classes.
- When pasting code into the online editor, make sure it comes from a plain text editor.
Learn More:
- Java Code Samples - an initial class template and advanced, ready-to-copy code samples
- Java Debugging Tips - for help debugging your application
- Java API - for detailed descriptions of the classes, objects and API calls used in these examples
- Localization#Java Programming - learn how to take into account localization of data for users
Add an Account Record
To add record using Java API, follow these steps:
- Set up a Parameters Object
- Call addRecord to add a new record
- Check the result by calling methods in an instance of a Result object
Set up a Parameters Object
Data is passed to the platform using a Parameters object. The Parameters object holds key-value pairs, where each key corresponds to a field name and each value corresponds to a field value in the database. Set the key-value pairs and then pass the Parameters object as an argument when the record is added. (To learn more about the parameters passed to a Java method, see the Standard Parameters.)
To set up a Parameters Object, create an instance of Parameters to hold the key-value pairs for the record by calling getParametersInstance. This instance is named addAccountParams:
- <syntaxhighlight lang="java" enclose="div">
Parameters addAccountParams = Functions.getParametersInstance(); </syntaxhighlight>
To see the fields that are defined in an object:
- Open a web browser and Login to the platform
- Click > Customization > Objects > {object} > Fields
- View the field list
Add the key-value pairs for the database fields to addAccountParams by calling Parameters.add:
- <syntaxhighlight lang="java" enclose="div">
addAccountParams.add("name","Hello World Account"); addAccountParams.add("number","0000001"); addAccountParams.add("city", "Orlando"); addAccountParams.add("country", "United States"); addAccountParams.add("county", "Marine County"); addAccountParams.add("phone", "222-222-2222"); addAccountParams.add("website", "www.helloworldaccount.com"); </syntaxhighlight>
Call addRecord
To add a new record, call addRecord and pass it an object identifier and the addAccountParams object. The values you added to addAccountParams are written to the account record in the database.
- <syntaxhighlight lang="java" enclose="div">
Result accountAddResult = Functions.addRecord("ACCOUNT", addAccountParams); </syntaxhighlight>
Like addRecord, many of the Java API record handling calls have an objectID parameter the object is specified ("ACCOUNT" in this example). In the objectID parameter, specify the Object Type Identifier.
Check the Result
The addRecord code (as well as several other Java API calls) returns an instance of the Result class. It is possible to check member variables in the Result object by calling its methods. For example, the code below makes a Java API debug utility call, making a nested call to Result.getMessage which returns a string indicating whether the call succeeded.
- <syntaxhighlight lang="java" enclose="div">
Logger.info("Result of addRecord for Account:" + accountAddResult.getMessage(), "Add"); </syntaxhighlight>
The string specified in the debug call is written to the debug log.
To view the Debug Log:
- When the code runs, use the Debug Log to troubleshoot problems during development. Many other debug calls are included in these code samples.
Debug Example for addRecord
This example checks the return code of addRecord by calling Result.getCode, and takes some action, based on the return code:
- Return codes:
- less then zero (<0); the call is not successful
- greater than or equal to zero (>= 0); successful
If the call is not successful, make a Java API throwError call, otherwise make a Result.getID call and continue
- <syntaxhighlight lang="java" enclose="div">
if(accountAddResult.getCode() < 0)
String msg = "Function: Add Account"; Logger.info(msg + ":\n" + accountAddResult.getMessage(), "Add"); // Log details Functions.throwError(msg + "."); // Error message
else
return accountAddResult.getID();
</syntaxhighlight>
Add a Contact Record
Here, you following the same process as adding an Account record to add a Contact Record:
- Set up a Parameters Object
- Create an instance of Parameters by calling getParametersInstance
- Check the Result
- <syntaxhighlight lang="java" enclose="div">
Parameters addContactParams = Functions.getParametersInstance(); </syntaxhighlight>
Then set the key-value pairs in the Parameters instance:
- <syntaxhighlight lang="java" enclose="div">
addContactParams.add("first_name", "John"); addContactParams.add("last_name", "Smith"); addContactParams.add("description", "Contact for Hello World added."); addContactParams.add("email", "mia@financio.com"); addContactParams.add("flag_primary_contact", "true"); addContactParams.add("street", "12345 Lake st"); </syntaxhighlight>
Call addRecord, specifying "CONTACT" as the object identifier and passing the Parameters object you set up above.
- <syntaxhighlight lang="java" enclose="div">
Result contactAddResult = Functions.addRecord("CONTACT", addContactParams); </syntaxhighlight>
Make a nested call to Result.getMessage to write to the debug log:
- <syntaxhighlight lang="java" enclose="div">
Logger.info("Result of addRecord for Contact - John Smith:"
+ contactAddResult.getMessage(), "Add");
</syntaxhighlight>
Check the return code by calling Result.getCode:
- <syntaxhighlight lang="java" enclose="div">
if(contactAddResult.getCode() < 0)
String msg = "Getting Started: Add John Smith"; Logger.info(msg + ":\n" + contactAddResult.getMessage(), "Add"); // Log details Functions.throwError(msg + "."); // Error message
else
return contactAddResult.getID();
</syntaxhighlight>
Update a Record
This code updates a record.
- <syntaxhighlight lang="java" enclose="div">
public void changeRecordValue(Parameters p) throws Exception {
try { String objectID = p.get("object_id"); String recordID = p.get("id"); // Specify the parameters to modify. (Only specified params are changed.) Parameters params = Functions.getParametersInstance(); params.add("field_name", "new_value");
// Result.getCode() >= 0 on success, -1 on failure Result r = Functions.updateRecord(objectID, recordID, params); if (r.getCode() < 0) { Functions.throwError("Update failed\n" + r.getMessage() ); } } catch (Exception e) { Functions.throwError( e.getMessage() ); }
} </syntaxhighlight>
Change Record Ownership
This code changes a record's owner. Note that it does not reassign any tasks that may be attached to the record. (Additional code would be required to do that.)
- <syntaxhighlight lang="java" enclose="div">
public void changeOwner(Parameters p) throws Exception {
try { String objectID = p.get("object_id"); String recordID = p.get("id"); String newOwner = "..ID of owner with desired team.."; boolean notifyNewOwner = true;
// Result.getCode() >= 0 on success, -1 on failure Result r = Functions.changeOwnerShipInfo( objectID, recordID, newOwner, notifyNewOwner); if (r.getCode() < 0) { Functions.throwError("Update failed\n" + r.getMessage() ); } } catch (Exception e) { Functions.throwError( e.getMessage() ); }
} </syntaxhighlight>