Difference between revisions of "AddRecord"

From AgileApps Support Wiki
imported>Aeric
imported>Aeric
m (Text replace - '// Error dialog' to '// Error message')
Line 41: Line 41:
     String msg = "Account could not be added";
     String msg = "Account could not be added";
     Logger.info(msg + ":\n" + result.getMessage(), "Account");  // Log details
     Logger.info(msg + ":\n" + result.getMessage(), "Account");  // Log details
     Functions.throwError(msg + ".");                            // Error dialog
     Functions.throwError(msg + ".");                            // Error message
}
}
else  
else  
Line 69: Line 69:
     String msg = "Book record could not be added";
     String msg = "Book record could not be added";
     Logger.info(msg + ":\n" + result.getMessage(), "Library");  // Log details
     Logger.info(msg + ":\n" + result.getMessage(), "Library");  // Log details
     Functions.throwError(msg + ".");                            // Error dialog
     Functions.throwError(msg + ".");                            // Error message
}
}
else
else

Revision as of 22:27, 10 September 2013

Add a new record to an object.

Syntax

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

Parameters

objectNameThe 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.
}