History Records
Creating history records
The following sample creates a history record.
Code Snippet
Date date = new Date(); protected HistoryRepository historyRepository; String uniqueKey = historyRepository.insertHistory(history); private void createHistory() { HistoryCreate history = new HistoryCreate(); //archivedOn is a required field history.setArchivedOn(date); //shortDescription is a required field history.setShortDescription("Subject - Update contact phone number"); //text is an additional field that stores more information than the shortDescription; text allows up to 20000 characters history.setText("Additional information about the contact record update"); Category cat = new Category(); //the next two lines identify a category and then add the category to the history record cat.setUniqueKey("HIST_STAT"); history.getCategories().add(cat); //use the parentObject property to associate the history with an existing record by unique key, for example, to describe a contact record that you have recently modified history.setParentObject("CONT_1234"); return history; }
Updating history records
The following sample updates a history record. Prerequisite to working with history records, you must also get the unique key value for the history record to update. If the history record is associated with another record, you need to get that record's unique key
Code Snippet
Date date = new Date(); protected HistoryRepository historyRepository; historyRepository.updateHistory(history); private void updateHistory() { HistoryUpdate history = new HistoryUpdate(); //use setUniqueKey to identify the target history record to update history.setUniqueKey("HIST_0011"); history.setShortDescription("Subject - Update contact phone number"); history.setText("Additional information about the contact record update"); history.setArchivedOn(date); Category cat = new Category(); //the next two lines identify a category and then add the category to the history record cat.setUniqueKey("HIST_MANA"); history.getCategories().add(cat); //use the parentObject property to associate the history with an existing record by unique key, for example, to describe a contact record that you have recently modified history.setParentObject("CONT_1234"); return history; }
Reading history records
The following sample reads a history record. You must get the unique key value for the history record to update.
Code Snippet
//for this sample, it is assumed that the unique key of the target History record to read is a String, "HIST_0011" protected HistoryRepository historyRepository; History readHistory = historyRepository.readHistory("HIST_0011", getPropertiesToRead()); private List<String> getPropertiesToRead() { List<String> properties = new List<String>(); properties.add("version"); properties.add("createdBy"); properties.add("createdOn"); properties.add("modifiedBy"); properties.add("modifiedOn"); properties.add("archivedOn"); properties.add("text"); properties.add("shortDescription"); properties.add("parentObject"); properties.add("categories"); return properties; }
Searching history records
The following sample searches for history records by short description (records that contain "updated contact record" in the Description) and date (records created after 12/01/2008).
Code Snippet
protected HistoryRepository historyRepository; StringFieldCriterion fieldCriterion1 = new StringFieldCriterion(); fieldCriterion1.setComparator(StringComparator.CONTAINS); LegacySearchFieldPathExpression fieldPathExpression1 = new LegacySearchFieldPathExpression(); fieldPathExpression1.setSearchKeyPath("shortDescription"); fieldCriterion1.setFieldPath(fieldPathExpression); fieldCriterion1.getValue().add("updated contact record"); DateFieldCriterion fieldCriterion2 = new DateFieldCriterion(); fieldCriterion2.setComparator(StringComparator.AFTER); LegacySearchFieldPathExpression fieldPathExpression2 = new LegacySearchFieldPathExpression(); fieldPathExpression2.setSearchKeyPath("createdOn"); fieldCriterion2.setFieldPath(fieldPathExpression); fieldCriterion2.getValue().add(01-DEC-08); FieldSearchClause searchCriteria = new FieldSearchClause(); searchCriteria.setOperator(LogicOperator.AND); searchCriteria.getCriteria().add(fieldCriterion1); searchCriteria.getCriteria().add(fieldCriterion2); List<History> searchHistory = historyRepository.readHistoriesByCriteria(searchCriteria, 100, getPropertiesToRead()); private List<String> getPropertiesToRead() { List<String> properties = new List<String>(); properties.add("version"); properties.add("createdBy"); properties.add("createdOn"); properties.add("modifiedBy"); properties.add("modifiedOn"); properties.add("archivedOn"); properties.add("text"); properties.add("shortDescription"); properties.add("parentObject"); properties.add("categories"); return properties; }
Deleting history records
The following sample deletes a history record. You must get the unique key value for record to delete.
Code Snippet
//for this sample it is assumed that a history record exists with the unique key equal to a String, uniqueKey protected HistoryRepository historyRepository; historyRepository.deleteHistory(uniqueKey);