See: Description
Interface | Description |
---|---|
QueryResponse |
Represents the result of a query execution.
|
QueryResult |
Represents a single row in
QueryResponse . |
QueryService |
Service to execute VQL queries.
|
The following objects are available for query:
user__sys
group__sys
group_membership__sys
doc_role__sys
binder_node__sys
active_workflow__sys
inactive_workflow__sys
active_workflow_task__sys
inactive_workflow_task__sys
active_workflow_task_item__sys
inactive_workflow_task_item__sys
// Locate services
RecordService recordService = ServiceLocator.locate(RecordService.class);
QueryService queryService = ServiceLocator.locate(QueryService.class);
// Query the Country object to retrieve all countries in a region
String queryCountry = "SELECT id, LONGTEXT(review_info__c) AS info FROM country__v WHERE region__c=" + "'" + region + "'";
QueryResponse queryResponse = queryService.query(queryCountry);
List countryReviewRecords = VaultCollections.newList();
// Create the related Country Review records
queryResponse.streamResults().forEach(queryResult -> {
String countryId = queryResult.getValue("id", ValueType.STRING);
// When a function alias is used, retrieve the value using the alias
String reviewInfo = queryResult.getValue("info", ValueType.STRING);
Record record = recordService.newRecord("country_review__c");
record.setValue("country__c", countryId);
record.setValue("info__c", reviewInfo);
countryReviewRecords.add(record);
});
// Batch save new Country Review records
recordService.batchSaveRecords(countryReviewRecords).ignoreErrors().execute();
When a document enters the Approved state, this lifecycle entry action queries for the document's parent. Once found, we query a second time to get the value of the parent document's Expiration Date field. We then set this field value on the Parent Expiration Date field on our original, newly approved document.
// Get current doc version and initialize services
DocumentVersion docVersion = documentActionContext.getDocumentVersions().get(0);
QueryService queryService = ServiceLocator.locate(QueryService.class);
DocumentService documentService = ServiceLocator.locate(DocumentService.class);
String docId = docVersion.getValue("id", ValueType.STRING);
// Get parent doc id with query service
String parentDocumentQuery = "SELECT target_doc_id__v FROM relationships WHERE source_doc_id__v = '"
+ docId+ "' AND relationship_type__v = 'basedon__v' ";
QueryResponse parentDocument = queryService.query(parentDocumentQuery);
Iterator<QueryResult> iterator = parentDocument.streamResults().iterator();
if (iterator.hasNext()) {
QueryResult idResults = iterator.next();
String parentDocId = idResults.getValue("target_doc_id__v", ValueType.STRING);
// Get expiration date field from parent doc with query service
String expirationDateQuery = "SELECT LATESTVERSION expiration_date__c FROM documents WHERE id = '" + parentDocId + "'";
QueryResponse expirationDate = queryService.query(expirationDateQuery);
// Set parent expiration date field on current doc
docVersion.setValue("parent_expiration_date__c",
((QueryResult) expirationDate.streamResults().iterator().next()).getValue("expiration_date__c", ValueType.DATE));
// Update current doc with document service
List<DocumentVersion> documentsToUpdate = VaultCollections.newList();
documentsToUpdate.add(docVersion);
documentService.saveDocumentVersions(documentsToUpdate);
}
else {
throw new RollbackException("OPERATION_NOT_ALLOWED", "Cannot find parent for document: " + docId);
}
Copyright © Veeva Systems 2017–2021. All rights reserved.