Skip navigation links

Package com.veeva.vault.sdk.api.role

This package provides interfaces to retrieve and manage role membership for a document or object record.

See: Description

Package com.veeva.vault.sdk.api.role Description

This package provides interfaces to retrieve and manage role membership for a document or object record.

The interfaces in this package allow you to:

Object Records

The following example illustrates checking role membership on a record and adding a user to a role.
     
      // Role to check on the Record
      String ROLE_TO_CHECK = "approver__c";

      // Locate services
      LogService logService = ServiceLocator.locate(LogService.class)
      RecordRoleService recRoleService = ServiceLocator.locate(RecordRoleService.class);

      // Get record from the recordActionContext (One record for user action)
      List<Record> recList = recordActionContext.getRecords();
      Record rec = recList.get(0);

      // Get current user ID
      String currentUserId = RequestContext.get().getCurrentUserId();

      // Get approver role for the record
      GetRecordRolesResponse recRolesResp = recRoleService.getRecordRoles(recList, ROLE_TO_CHECK);
      RecordRole approverRole = recRolesResp.getRecordRole(rec);

      // Check if current user is in Approver role
      boolean isUserInApproverRole = recRoleService
          .getUserInRecordRoles(currentUserId, VaultCollections.asList(approverRole))
          .isUserInRecordRole(approverRole);

      // Add current user to Approver role
      if (!isUserInApproverRole) {
          // Create new RecordRoleUpdate and add users
          RecordRoleUpdate recRoleUpdate = recRoleService.newRecordRoleUpdate(ROLE_TO_CHECK, rec);
              recRoleUpdate.addUsers(VaultCollections.asList(currentUserId));

          // Execute the role update
          recRoleService.batchUpdateRecordRoles(VaultCollections.asList(recRoleUpdate))
              .rollbackOnErrors()
              .execute();
      }

 

Documents

The following example illustrates checking role membership on a document for the current user and adding the user to a role if not already assigned.
     
      // Role to check on the Document
      String ROLE_TO_CHECK = "approver__c";

      // Locate services
      LogService logService = ServiceLocator.locate(LogService.class);
      DocumentRoleService docRoleService = ServiceLocator.locate(DocumentRoleService.class);

      // Assuming this is a Document user action, there is one document in the documentActionContext
      List<DocumentVersion> docVersionList = documentActionContext.getDocumentVersions();
      List<DocumentRoleUpdate> docRoleUpdList = VaultCollections.newList();
      DocumentVersion docVersion = docVersionList.get(0);

      // Get current user ID
      String currentUserId = RequestContext.get().getCurrentUserId();

      // Get Approver document role
      GetDocumentRolesResponse docRolesResp = docRoleService.getDocumentRoles(docVersionList, ROLE_TO_CHECK);
      DocumentRole approverRole = docRolesResp.getDocumentRole(docVersion);

      // Check if current user is in Approver role
      boolean isUserInApproverRole = docRoleService
          .getUserInDocumentRoles(currentUserId, VaultCollections.asList(approverRole))
          .isUserInDocumentRole(approverRole);

      // Add current user to Approver role
      if (!isUserInApproverRole) {
          DocumentRoleUpdate docRoleUpdate = docRoleService.newDocumentRoleUpdate(ROLE_TO_CHECK, docVersion);
              docRoleUpdate.addUsers(VaultCollections.asList(currentUserId));

          // Execute the role update
          docRoleService.batchUpdateDocumentRoles(VaultCollections.asList(docRoleUpdate))
              .rollbackOnErrors()
              .execute();
      }

 

Performance Considerations

Avoid retrieving or updating roles one at a time in a loop. Instead, process multiple records or documents in a single service method call.
Skip navigation links

Copyright © Veeva Systems 2017–2018. All rights reserved.