Updating lookup fields in Dynamics 365 / Dynamics CRM
Applies to: the Microsoft Dynamics 365 and Microsoft Dynamics CRM connectors Audience: workflow designers
This guide explains how to set, change and clear lookup fields (fields that point to another record) when you save records to Dynamics 365 or Dynamics CRM from appRules.
1. What is a lookup field?
Section titled “1. What is a lookup field?”A lookup field holds a reference to a record in another table. Common examples:
| Table | Lookup field | Points to |
|---|---|---|
| account | transactioncurrencyid |
the currency (transactioncurrency) |
| account | primarycontactid |
a contact |
| account | parentaccountid |
another account |
| contact | parentcustomerid |
an account or a contact |
| any record | ownerid |
a user or a team |
| task, email, phone call… | regardingobjectid |
many possible tables |
In appRules you set a lookup like any other field. The value you give it is the ID (GUID) of the target record, for example 6f1c2b1e-3a4d-4e8f-9a0b-1c2d3e4f5a6b. The connector turns it into the reference that Dynamics expects.
2. Setting a lookup: the basic case
Section titled “2. Setting a lookup: the basic case”Most lookups point to a single table. For these, the GUID is all you need.
In SaveRecord or UpdateRecord: map the lookup field to the GUID of the target record.
| Field | Value |
|---|---|
transactioncurrencyid |
6f1c2b1e-3a4d-4e8f-9a0b-1c2d3e4f5a6b |
In a SQL statement:
UPDATE accountSET transactioncurrencyid = '6f1c2b1e-3a4d-4e8f-9a0b-1c2d3e4f5a6b'WHERE accountid = 'a1b2c3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5d'The same works when you insert a new record.
Getting the GUID
Section titled “Getting the GUID”You usually know the target by a name or a code, not by its ID. Look the ID up first, for example with a LookupRecord activity or a query, then use the result in the save:
SELECT transactioncurrencyid FROM transactioncurrency WHERE isocurrencycode = 'EUR'SELECT contactid FROM contact WHERE emailaddress1 = 'jane@contoso.com'SELECT systemuserid FROM systemuser WHERE domainname = 'jane@contoso.com'3. Lookups that can point to several tables
Section titled “3. Lookups that can point to several tables”Some lookups can point to more than one table. For these, you must also tell the connector which table the GUID belongs to. There are two ways to do this.
Option A: add the type field
Section titled “Option A: add the type field”Set the lookup’s companion type field in the same save, with the table name as its value:
| Lookup | Type field | Example value |
|---|---|---|
parentcustomerid |
parentcustomeridtype |
account or contact |
customerid |
customeridtype |
account or contact |
ownerid |
owneridtype |
systemuser or team |
regardingobjectid |
regardingobjecttypecode |
account, contact, opportunity, … |
UPDATE contactSET parentcustomerid = '9e8d7c6b-…', parentcustomeridtype = 'account'WHERE contactid = '1234…'In SaveRecord, map both fields: the lookup gets the GUID and the type field gets the table name. The connector uses the type field to build the reference and does not send it to Dynamics.
Option B: write the table into the value
Section titled “Option B: write the table into the value”Give the value as entityset(guid). You then don’t need a type field:
UPDATE contact SET parentcustomerid = 'accounts(9e8d7c6b-…)' WHERE contactid = '1234…'UPDATE task SET regardingobjectid = 'opportunities(5a4b…)' WHERE activityid = '7788…'UPDATE account SET ownerid = 'teams(3c2d…)' WHERE accountid = 'abcd…'The entity set name is the plural name Dynamics uses in its API. Common ones:
| Table | Entity set |
|---|---|
| account | accounts |
| contact | contacts |
| lead | leads |
| opportunity | opportunities |
| incident (case) | incidents |
| systemuser | systemusers |
| team | teams |
| transactioncurrency | transactioncurrencies |
For other tables, including custom ones, check the table’s settings in Power Apps, or ask your Dynamics administrator.
4. Clearing a lookup
Section titled “4. Clearing a lookup”Set the field to empty (NULL):
UPDATE account SET primarycontactid = NULL WHERE accountid = 'abcd…'In SaveRecord, map the field to an empty value or [null].
5. Bulk loads
Section titled “5. Bulk loads”Lookups work the same way in bulk (batched) saves. If one row has an invalid lookup value, for example a missing type or a value that isn’t a GUID:
- only that row fails;
- the other rows are still saved when continue on error is enabled;
- the error is recorded for that row in the results (
LastResultInfo) and in the activity log.
6. Error messages and what to do
Section titled “6. Error messages and what to do”| Message | Meaning | What to do |
|---|---|---|
| Lookup column ‘x’ on ‘table’ needs the GUID of the target record; ‘…’ is not a GUID. | The value is a name or code, not an ID. | Look up the GUID first (section 2). |
| Lookup column ‘x’ on ‘table’ can point to account, contact. Add ‘xtype’ with the target table name… | The lookup can point to several tables and the table was not given. | Add the type field (option A) or use entityset(guid) (option B). |
| Lookup column ‘x’ on ‘table’ cannot point to ‘…’. Valid targets: … | The type or entity set given is not allowed for this lookup. | Check the spelling, and use one of the tables listed. |
| An undeclared property ‘x’ … (from Dynamics) | The connector couldn’t read the table’s metadata, so the field was sent unconverted. | Check the connection and its permissions, then look for [Lookup] lines in the connector log (section 7). |
| The record you are trying to reference does not exist (from Dynamics) | The GUID is valid, but there is no such record in the target table. | Check that the GUID belongs to the table you named. |
7. Troubleshooting with the connector log
Section titled “7. Troubleshooting with the connector log”Set Log Level = 2 in the data source connection settings and run the workflow again. Then look in the log file (DynamicsCRM_Debug.log or Dynamics365_Debug.log) for these lines:
| Line | Meaning |
|---|---|
[SchemaOnly] account: N column(s) from metadata |
appRules read the table’s columns. This happens once per save. |
[Lookup] account: N lookup column(s) mapped for writes |
The table’s lookups were loaded. This happens once per table. |
[Lookup] account.transactioncurrencyid -> transactioncurrencyid@odata.bind = /transactioncurrencies(…) |
The value was converted. This is what is sent to Dynamics. |
[Lookup] Metadata unavailable… |
The table’s metadata could not be read, so lookups were sent unconverted. |
8. Current limitations
Section titled “8. Current limitations”- Numeric type codes are not accepted. Use the table name (
account), not its code (1), in fields such asregardingobjecttypecode. - “Name” fields are read-only. Fields such as
transactioncurrencyidnameorprimarycontactidnamecannot be written. Set the lookup itself with a GUID. - Activity-party fields (
to,from,cc,requiredattendees, …) are a different kind of field and are not covered by this guide. - Record IDs cannot be changed. The ID of an existing record (for example
accountid) cannot be updated. Dynamics only accepts an ID when the record is created. - If the type field is not recognised. If you set
regardingobjecttypecodebut still get the “can point to…” error, use theentityset(guid)form (option B) instead.