Package com.veeva.vault.sdk.api.page
To send data back to the client on page load, the custom PageController implementation must
define the PageController.onLoad(com.veeva.vault.sdk.api.page.PageLoadContext)
method.
The following example page controller defines an onLoad method that returns a JsonObject
to the client once a page has been loaded.
@ExecuteAs(ExecuteAsUser.REQUEST_OWNER)
@PageControllerInfo
public class HelloWorldController implements PageController {
@Override
public PageLoadResponse onLoad(PageLoadContext context) {
JsonService jsonService = ServiceLocator.locate(JsonService.class);
JsonObject jsonObject = jsonService.newJsonObjectBuilder()
.setValue("userId", RequestContext.get().getInitiatingUserId())
.build();
return context.newLoadResponseBuilder()
.withData(jsonObject)
.build();
}
}
A custom page controller can return data to the client as a UserDefinedModel on page load by
specifying the type of UserDefinedModel along with the object to return.
// com.veeva.vault.custom.models.HelloUserResponse.java
@UserDefinedModelInfo
public interface HelloUserResponse extends UserDefinedModel {
@UserDefinedProperty
String getUserId();
void setUserId(String userId);
@UserDefinedProperty
String getMessage();
void setMessage(String message);
}
// com.veeva.vault.custom.pages.HelloWorldController.java
@ExecuteAs(ExecuteAsUser.REQUEST_OWNER)
@PageControllerInfo
public class HelloWorldController implements PageController {
@Override
public PageLoadResponse onLoad(PageLoadContext context) {
UserDefinedModelService userDefinedModelService = ServiceLocator.locate(UserDefinedModelService.class);
HelloUserResponse helloUserResponse = userDefinedModelService.newUserDefinedModel(HelloUserResponse.class);
helloUserResponse.setUserId(RequestContext.get().getInitiatingUserId());
helloUserResponse.setMessage("Welcome!");
return context.newLoadResponseBuilder()
.withData(helloUserResponse, HelloUserResponse.class)
.build();
}
}
A custom page controller implementation can also respond to events sent from the client by defining
its PageController.onEvent(com.veeva.vault.sdk.api.page.PageEventContext) method.
Like the onLoad method, a definition for onEvent may also return either a JSON object or UserDefinedModel.
The following example defines an onEvent method that returns a JSON object when the client sends
pageEventA and returns a UserDefinedModel when the client sends pageEventB.
@ExecuteAs(ExecuteAsUser.REQUEST_OWNER)
@PageControllerInfo
public class HelloWorldController implements PageController {
@Override
public PageEventResponse onEvent(PageEventContext context) {
String event = context.getEventName()
if ("pageEventA".equals(event)) {
JsonService jsonService = ServiceLocator.locate(JsonService.class);
JsonObject jsonObject = jsonService.newJsonObjectBuilder()
.setValue("userId", RequestContext.get().getInitiatingUserId())
.build();
return context.newEventResponseBuilder()
.withData(jsonObject)
.build();
} else if ("pageEventB".equals(event)) {
UserDefinedModelService userDefinedModelService = ServiceLocator.locate(UserDefinedModelService.class);
HelloUserResponse helloUserResponse = userDefinedModelService.newUserDefinedModel(HelloUserResponse.class);
helloUserResponse.setUserId(RequestContext.get().getInitiatingUserId());
helloUserResponse.setMessage("Thank you!");
return context.newLoadResponseBuilder()
.withData(helloUserResponse, HelloUserResponse.class)
.build();
} else {
// If the event sent from the client is not one of pageEventA or pageEventB, return an error response
// with a custom title and user-facing message.
return context.newEventErrorResponseBuilder()
.withTitle("Event not found")
.withUserMessage("Error occurred while processing page event")
.build();
}
}
}
-
ClassDescriptionRepresents a request to open a page.Builds a
NavigateToLocationRequestRepresents a request to open a page.Builds aNavigateToPageRequestProvides data upon loading a page or responding to events after a page has been loaded.Indicates a class is a page controller.Provides information about an event to a page'sPageController.onEvent(com.veeva.vault.sdk.api.page.PageEventContext)method and provides methods to create a response.Represents a structured error response from a page'sPageController.onEvent(com.veeva.vault.sdk.api.page.PageEventContext)method.Creates an instance ofPageEventErrorResponse.Represents a structured response from a page'sPageController.onEvent(com.veeva.vault.sdk.api.page.PageEventContext)method.Creates an instance ofPageEventResponse.Provides information about the page load request to the page'sPageController.onLoad(com.veeva.vault.sdk.api.page.PageLoadContext)method and provides methods to create a response.Represents a structured error response from a page'sPageController.onLoad(com.veeva.vault.sdk.api.page.PageLoadContext)method.Creates an instance ofPageLoadErrorResponse.Represents a structured response from a page'sPageController.onLoad(com.veeva.vault.sdk.api.page.PageLoadContext)method.Creates an instance ofPageLoadResponse.Represents a page's HTTP request.