Show/Hide Toolbars

If you need to perform common functions using the API, you can use the UtilityService interface. This interface encompasses miscellaneous methods that you might use throughout your code. These methods do not set anything in TeamConnect, but instead they retrieve information or run your code as a different user.

Note: Use the following code to access UtilityService: UtilityService utilityService = platform.getUtilityService();

UtilityService includes methods for the following types of actions:

Converting or changing formatsFor example, you can use the booleanValueForString() method to convert a string to a boolean and the formatDateByUserSetting() method to format the date based on the user's date settings, as shown in the following code samples.

UtilityService utilityService = platform.getUtilityService();

         

//Converting strings to booleans and returning boolean true.

utilityService.booleanValueForString("YES");

utilityService.booleanValueForString("TRUE");

utilityService.booleanValueForString("1");

 

//Converting strings to booleans and returning boolean false.

utilityService.booleanValueForString("NO");

utilityService.booleanValueForString("FALSE");

utilityService.booleanValueForString("0");

 

//Formatting the date for the user based on the current settings for that

User user = utilityService.getCurrentUser();

Date today = new Date();

String formattedDate = utilityService.formatDateByUserSetting(today, user);

Retrieving information based on the current day, time, or userFor example, you can also use the getCurrentUser() method to retrieve the user object for the user currently running the code. You can also use the getBeginningOfToday() method to return the start of the current day in the current user's time zone, as shown in the following code sample.

Date date = utilityService.getBeginningOfToday();

Retrieving information about single-project object definitionsFor example, you can use the getSingletonForObjectDefinition() method to return the single project for an object definition, as shown in the following code sample.

// Unique 4-letter code for custom project.

String projectDefinitionUniqueCode = "PROJ";

Project singletonProject = utilityService.getSingletonForObjectDefinition(projectDefinitionUniqueCode);

Retrieving system informationFor example, you can use the getDatabaseInfo() to return information about the TeamConnect database, as shown in the following sample.

DatabaseInfo databaseInfo = utilityService.getDatabaseInfo();

String serverName = databaseInfo.getServerName();

String databaseName = databaseInfo.getDatabaseName();

Date databaseCreatedOn = databaseInfo.getDatabaseCreatedOn();

String version = databaseInfo.getVersion();

DatabaseProduct product = databaseInfo.getDatabaseProduct();

Comparing two different daysFor example, you can use the isDayAfter() method to determine whether one date occurs before or after another date, as shown in the following sample about comparing the created on date for two different invoices.

//find two invoices

Long invoiceId1 = 1L;

Invoice invoice1 = invoiceService.read(invoiceId1);

Long invoiceId2 = 2L;

Invoice invoice2 = invoiceService.read(invoiceId2);

//Determine if invoice2 was created after invoice1

if (utilityService.isDayAfter(invoice2.getCreatedOn(), invoice1.getCreatedOn()))

{

//do something

}

Sorting a listYou can use the sortUsingPath() method to sort a list, as shown in the following sample.

List<User> users = platform.getUserService().getAllUsers();

//sort users based on the contact primary city. In ascending order.

FieldPath pathToContactsPrimaryCity = new FieldPath(User.CONTACT).add(Contact.PRIMARY_ADDRESS).add(Address.CITY);

boolean isAscending = true;

List sortedUsers= platform.getUtilityService().sortUsingPath(users, pathToContactsPrimaryCity, isAscending);

Running code as a different userYou can use the runAsSystemUser() method to run code as the system user. You can also use the runAsUser() method to run code as a specific user, as shown in the following sample.

//get current user

User user = utilityService.getCurrentUser();

//run a call as the user

utilityService.runAsUser(user, new Callable() {

@Override

public void call() {

//do something

}

});