# Lookup Fields

In Dynamics CRM, a Lookup Field is used to refer to other entities in the system.

Examples of Lookup fields include:

- account: Parent Account, Primary Contact, Owning Business Unit
- contact: Owning Business Unit, Price List, Preferred Service

A Lookup actually has two components:

1. The unique identifier (a guid) of the target entity
2. The type of the target entity (account, contact, equipment, ...)

To set the value of a Lookup Field, just map the ID of the target to the field. Inaport will automatically set the type of the target based on the field; for example, if you set the Preferred Service field of a contact, the type will be automatically set to "service".

### Lookups with multiple target types (regardingobjectid)

Some lookup fields can refer to more than one type of entity; for example, the regardingobjectid field of the appointment entity may refer to account, contact, case, opportunity or many others.

Inaport allows you to handle this in two ways:

1. If you map a string with ID::targetname, the ID will be set and the target name will be used to set the target. An example of this might be: "6485c8c6-5758-de11-b8fe-001e0b4641ec::account" This will set the lookup value to the account with the given ID.
2. If you just map the ID to the field, Inaport will assume that the target entity is the parent entity in the table map. For example, if you match on contact and then have appointment as a child, the type of the lookup will be set to contact.

### Primary Key and Foreign Keys

Often, the Lookup Field will be chosen by Inaport as the foreign key; for example, when contact is added as a child of account, the Parent Customer field is automatically chosen as the foreign key, and will be populated.

## Obtaining an Entity ID

If you need an entity ID so that you can map to a lookup field, you can use the dbselect() function to query Dynamics CRM. For example:

- dbexecsql("T", "SELECT serviceid FROM service WHERE name = 'ServiceAA'")

will retrieve the serviceid for the service named "ServiceAA".

This can then be mapped to the Preferred Service field in a contact or account.