AgileApps Support Wiki Pre Release

Difference between revisions of "AddRecord"

From AgileApps Support Wiki
imported>Aeric
 
 
(23 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{DISPLAYTITLE:addRecord}}
{{DISPLAYTITLE:addRecord}}
Add a new record to an object.
The addRecord Java API helps in performing the following operations:
:* Adding a new record to an object.
:* Creating a new platform user in AgileApps.  


===Adding a new record to an object===
'''Syntax'''
'''Syntax'''
:<syntaxhighlight lang="java" enclose="div">
:<syntaxhighlight lang="java" enclose="div">
Result result = Functions.addRecord(String objectID, Parameters params);
Result result = Functions.addRecord(String objectName, Parameters params);
</syntaxhighlight>
</syntaxhighlight>


'''Parameters'''
'''Parameters'''
:;objectID:The [[Object]] to add the record to.
:* objectName - The [[Object]] to add the record to.
:;params:
:* params:
::* The field-value pairs for the object you are adding.
:** The field-value pairs for the object you are adding.
::* Optionally, add the [[#Data Policy Bypass Parameter|Data Policy Bypass Parameter]] to bypass data policies defined in the platform for this operation.
:** Turn off rules that might otherwise be triggered as a result of this action:
:::<syntaxhighlight lang="java" enclose="div">
params.add(PLATFORM.PARAMS.RECORD.DO_NOT_EXEC_RULES,"1");
</syntaxhighlight>
::* Pass file parameters in the current request to any subsequent API calls made in a data policy defined on this object:
:::<syntaxhighlight lang="java" enclose="div">
:::<syntaxhighlight lang="java" enclose="div">
params.add(PLATFORM.PARAMS.RECORD.DO_NOT_EXEC_DATA_POLICY,"1");
params.add(PLATFORM.PARAMS.RECORD.ENABLE_MULTIPART,"1");
</syntaxhighlight>
</syntaxhighlight>


Line 21: Line 27:
:[[Result Class|<tt>Result</tt> object]]
:[[Result Class|<tt>Result</tt> object]]


{{Note|The Result object contains the record ID for the new record. Use <tt>Result.getID</tt> to retrieve it.}}
{{Note|The Result object contains the record ID for the new record. Use <tt>Result.getID()</tt> to retrieve it.}}


====Example #1: Add a Record to the Account Class ====
====Example #1: Add a Record to the Account Class ====
This example creates an instance of <tt>Parameters</tt> and adds name-value pairs to it. The code then calls <tt>addRecord</tt>, assigning the returned value to an instance of <tt>Result</tt> and calling <tt>Result.getCode</tt> to assign the error code to a variable which is then conditionally checked to determine the code to execute. If the call was not successful, the code calls <tt>[[throwError]]</tt> to display an error dialog.
This example creates an instance of <tt>Parameters</tt> and adds name-value pairs to it. The code then calls <tt>addRecord</tt>, assigning the returned value to an instance of <tt>Result</tt> and calling <tt>Result.getCode()</tt> to assign the error code to a variable which is then conditionally checked to determine the code to execute. If the call was not successful, the code calls <tt>[[throwError]]</tt> to display an error dialog.
A
 
:<syntaxhighlight lang="java" enclose="div">
:<syntaxhighlight lang="java" enclose="div">
Parameters params = Functions.getParametersInstance();
Parameters params = Functions.getParametersInstance();
Line 36: Line 42:
     // Some error happened.  
     // Some error happened.  
     String msg = "Account could not be added";
     String msg = "Account could not be added";
     Functions.debug(msg + ":\n" + result.getMessage());  // Log details
     Logger.info(msg + ":\n" + result.getMessage(), "Account");  // Log details
     Functions.throwError(msg + ".");                     // Error dialog
     Functions.throwError(msg + ".");                           // Error message
}
}
else  
else  
Line 45: Line 51:
}
}
</syntaxhighlight>
</syntaxhighlight>


====Example #2: Add a Record with a Multi Object Lookup field====
====Example #2: Add a Record with a Multi Object Lookup field====
This example add a record to an object that contains a [[Multi Object Lookup]], where:
This example add a record to an object that contains a [[Multi Object Lookup]], where:
:* <tt>Books</tt> is an object that contains a MultiObject Lookup field
:* <tt>Books</tt> is an object that contains a MultiObject Lookup field
:* <tt>libraryBook</tt> is a Multi Object Lookup field that points to a particular library and the book it came from
:* <tt>libraryBook</tt> is a Multi Object Lookup field that points to a particular book and the library it came from
:* <tt>e5fadf228160483b81ea8d84c71259c9</tt> is the ID of the Library object that contained the book  
:* <tt>9978946545</tt> is the ID the <tt>library</tt> object that contains the book.<br>(Object ID must be specified. Object name will not work.)
:* <tt>767645492</tt> is the record ID of the book in that library
:* <tt>767645492</tt> is the record ID of the book in that library


Line 57: Line 62:
Parameters params = Functions.getParametersInstance();
Parameters params = Functions.getParametersInstance();
params.add("title", "A Good Book");  
params.add("title", "A Good Book");  
params.add("libraryBook", "e5fadf228160483b81ea8d84c71259c9:767645492");  
params.add("libraryBook", "9978946545:767645492");  
                                    //  {library_object_id}:{record_id}
                  //  {object_id}:{record_id}
Result result = Functions.addRecord("Books", params);  
Result result = Functions.addRecord("Books", params);  
int resultCode = result.getCode();
int resultCode = result.getCode();
Line 65: Line 70:
     // Some error happened.  
     // Some error happened.  
     String msg = "Book record could not be added";
     String msg = "Book record could not be added";
     Functions.debug(msg + ":\n" + result.getMessage());  // Log details
     Logger.info(msg + ":\n" + result.getMessage(), "Library");  // Log details
     Functions.throwError(msg + ".");                     // Error dialog
     Functions.throwError(msg + ".");                           // Error message
}
}
else
else
Line 73: Line 78:
}</syntaxhighlight>
}</syntaxhighlight>
<noinclude>
<noinclude>
===Creating a new platform user in AgileApps===
You can create a new platform user in AgileApps by using '''addRecord''' Java API. Place the following code in the call method and invoke this from the business rule.
'''Example:'''
:<syntaxhighlight lang="java" enclose="div">
Parameters userParams = Functions.getParametersInstance();
userParams.add("first_name","John");
userParams.add("last_name", "Bob");
userParams.add("email", "xxx@softwareag.com");
userParams.add("username","john.bob");
userParams.add("time_zone", "23"); // Get the value of the select box option from the browser inspector
userParams.add("team_id", "xxx");  // Get the respective id from teams.
userParams.add("access_profile_id", "xxx"); // Get the respective id from access profiles.
userParams.add("user_type", "P");        // Platform user
userParams.add("notify_by_email", "0");  // 1=send message with system-generated password. 0=don't
userParams.add("password", "xxxx"); // Set a fixed initial password when notify_by_email is 0.
userParams.add("application_id", "xxx"); // Get the respective id from applications list.
userParams.add("role_id", "xxxx"); // Get the role id from application roles
Result r = Functions.addRecord("USER", userParams);
if (r.getCode() < 0) {
  String msg = "Error while adding User record:\n"+ r.getMessage();
  // To show the error in the UI. 
Functions.throwError(msg);   
} else {
// have code for successful user creation
</syntaxhighlight>


[[Category:Record Handling]]
[[Category:Record Handling]]
</noinclude>
</noinclude>

Latest revision as of 06:06, 6 February 2023

The addRecord Java API helps in performing the following operations:

  • Adding a new record to an object.
  • Creating a new platform user in AgileApps.

Adding a new record to an object

Syntax

Result result = Functions.addRecord(String objectName, Parameters params);

Parameters

  • objectName - The Object to add the record to.
  • params:
    • The field-value pairs for the object you are adding.
    • Turn off rules that might otherwise be triggered as a result of this action:
params.add(PLATFORM.PARAMS.RECORD.DO_NOT_EXEC_RULES,"1");
  • Pass file parameters in the current request to any subsequent API calls made in a data policy defined on this object:
params.add(PLATFORM.PARAMS.RECORD.ENABLE_MULTIPART,"1");
Return
Result object

Notepad.png

Note: The Result object contains the record ID for the new record. Use Result.getID() to retrieve it.

Example #1: Add a Record to the Account Class

This example creates an instance of Parameters and adds name-value pairs to it. The code then calls addRecord, assigning the returned value to an instance of Result and calling Result.getCode() to assign the error code to a variable which is then conditionally checked to determine the code to execute. If the call was not successful, the code calls throwError to display an error dialog.

Parameters params = Functions.getParametersInstance();
params.add("name", "Acme Solutions");
params.add("number", "GRG2323339");
Result result = Functions.addRecord("ACCOUNT", params);
int resultCode = result.getCode();
if(resultCode < 0)
{
    // Some error happened. 
    String msg = "Account could not be added";
    Logger.info(msg + ":\n" + result.getMessage(), "Account");  // Log details
    Functions.throwError(msg + ".");                            // Error message
}
else 
{
    // Take other actions on successful addition
    // of the account
}

Example #2: Add a Record with a Multi Object Lookup field

This example add a record to an object that contains a Multi Object Lookup, where:

  • Books is an object that contains a MultiObject Lookup field
  • libraryBook is a Multi Object Lookup field that points to a particular book and the library it came from
  • 9978946545 is the ID the library object that contains the book.
    (Object ID must be specified. Object name will not work.)
  • 767645492 is the record ID of the book in that library
Parameters params = Functions.getParametersInstance();
params.add("title", "A Good Book"); 
params.add("libraryBook", "9978946545:767645492"); 
                  //  {object_id}:{record_id}
Result result = Functions.addRecord("Books", params); 
int resultCode = result.getCode();
if(resultCode < 0)
{
    // Some error happened. 
    String msg = "Book record could not be added";
    Logger.info(msg + ":\n" + result.getMessage(), "Library");  // Log details
    Functions.throwError(msg + ".");                            // Error message
}
else
{
    // Successful add. Take other actions, as needed.
}


Creating a new platform user in AgileApps

You can create a new platform user in AgileApps by using addRecord Java API. Place the following code in the call method and invoke this from the business rule.

Example:

Parameters userParams = Functions.getParametersInstance();

userParams.add("first_name","John");

userParams.add("last_name", "Bob");

userParams.add("email", "xxx@softwareag.com");

userParams.add("username","john.bob");

userParams.add("time_zone", "23"); // Get the value of the select box option from the browser inspector

userParams.add("team_id", "xxx");  // Get the respective id from teams.

userParams.add("access_profile_id", "xxx"); // Get the respective id from access profiles.

userParams.add("user_type", "P");         // Platform user

userParams.add("notify_by_email", "0");   // 1=send message with system-generated password. 0=don't

userParams.add("password", "xxxx"); // Set a fixed initial password when notify_by_email is 0.

userParams.add("application_id", "xxx"); // Get the respective id from applications list.

userParams.add("role_id", "xxxx"); // Get the role id from application roles

Result r = Functions.addRecord("USER", userParams);

if (r.getCode() < 0) {

  String msg = "Error while adding User record:\n"+ r.getMessage();

  // To show the error in the UI.   

Functions.throwError(msg);     

} else {

// have code for successful user creation