Show/Hide Toolbars

When searching for contacts, use the ContactRepository readContactsByCriteria method. Use the Person, Company, and Contact data objects to reference the field names you can use in the search criteria and include in the properties list for values to return with the search results (contact records). From the search results, the numeric limit value you set determines how many contact records will be returned. Similar to when you are reading a contact record, you must specify the names of property values to return with search results.

For more general information about how to define search criteria, see Searching Records.

Code Snippet for readContactsByCriteria

//for this example, it is assumed that at least one contact record that meets the search criterion already exists

protected ContactRepository contactRepository;

public void searchPersonContacts() throws Exception {

StringFieldCriterion fieldCriterion = new StringFieldCriterion();

fieldCriterion.setComparator(StringComparator.EQUALS_ENFORCE_CASE);

LegacySearchFieldPathExpression fieldPathExpression = new LegacySearchFieldPathExpression();

fieldPathExpression.setSearchKeyPath("firstName");

fieldCriterion.setFieldPath(fieldPathExpression);

fieldCriterion.getValue().add("John");

FieldSearchClause searchCriteria = new FieldSearchClause();

searchCriteria.setOperator(LogicOperator.AND);

searchCriteria.getCriteria().add(fieldCriterion);

List<Contact> contacts =

contactRepository.readContactsByCriteria(searchCriteria, 100, getPropertiesToRead());

}

private List<String> getPropertiesToRead() {

List<String> properties = new List<String>();

properties.add("firstName");

properties.add("lastName");

properties.add("addresses.type");

properties.add("addresses.state");

return properties;

}

Code Snippet for readContactsByCriteria (specify return properties of contact property of type object)

//for this example, it is assumed that at least one contact record that meets the search criterion already exists

//the last three properties defined in the properties list illustrate how you specify contact search return property values where the contact property is an object

protected ContactRepository contactRepository;

public void searchPersonContacts() throws Exception {

StringFieldCriterion fieldCriterion = new StringFieldCriterion();

fieldCriterion.setComparator(StringComparator.EQUALS_ENFORCE_CASE);

LegacySearchFieldPathExpression fieldPathExpression = new LegacySearchFieldPathExpression();

fieldPathExpression.setSearchKeyPath("firstName");

fieldCriterion.setFieldPath(fieldPathExpression);

fieldCriterion.getValue().add("John");

FieldSearchClause searchCriteria = new FieldSearchClause();

searchCriteria.setOperator(LogicOperator.AND);

searchCriteria.getCriteria().add(fieldCriterion);

List<Contact> contacts =

contactRepository.readContactsByCriteria(searchCriteria, 100, getPropertiesToRead());

}

private List<String> getPropertiesToRead() {

List<String> properties = new List<String>();

properties.add("firstName");

properties.add("lastName");

properties.add("primaryPhoneNumber.number");

//where number is a property of the contact property, primaryPhoneNumber (type object)

properties.add("primaryEmailAddress.address");

//where address is a property of the contact property, primaryEmailAddress (type object)

properties.add("defaultRates.rate");

//where rate is a property of the contact property, defaultRates (type object)

return properties;

}

Code Snippet for readContactsByCriteria (search by custom field value)

//for this example, it is assumed that at least one contact record that meets the search criterion already exists

//also assume that a category, Employee (with unique code, EMPL) has been defined for the Contact object definition

//also assume that a custom string field, DLNumb (driver's license number) has been define for the Contact category, Employee

protected ContactRepository contactRepository;

public void searchPersonContacts() throws Exception {

StringFieldCriterion fieldCriterion = new StringFieldCriterion();

fieldCriterion.setComparator(StringComparator.EQUALS_ENFORCE_CASE);

LegacySearchFieldPathExpression fieldPathExpression = new LegacySearchFieldPathExpression();

fieldPathExpression.setSearchKeyPath("CONT_EMPL__DLNumb");

//note there are two underscores between "EMPL" and "DLNumb" in the searchKeyPath above

fieldCriterion.setFieldPath(fieldPathExpression);

fieldCriterion.getValue().add("B301792Z");

FieldSearchClause searchCriteria = new FieldSearchClause();

searchCriteria.setOperator(LogicOperator.AND);

searchCriteria.getCriteria().add(fieldCriterion);

List<Contact> contacts =

contactRepository.readContactsByCriteria(searchCriteria, 100,

getPropertiesToRead());

}

private List<String> getPropertiesToRead() {

List<String> properties = new List<String>();

properties.add("firstName");

properties.add("lastName");

properties.add("addresses.type");

properties.add("addresses.state");

return properties;

}