xrm-ex
offers the setLookupValue
method, which simplifies assigning values to lookup fields. The method accepts the entity type, ID, formatted value, and an optional append
parameter (useful for multi-value lookups).
Example of Setting a Single Lookup Field
fields.Customer.setLookupValue("account", "cfaeb904-fc99-4d48-b7a1-a13fdd6bfea1", "Contoso");
In this example:
"account"
specifies the entity type.
"cfaeb904-fc99-4d48-b7a1-a13fdd6bfea1"
is the record’s ID (GUID).
"Contoso"
is the formatted display name.
Handling Multi-Value Lookups with append
For lookups that allow multiple values, such as the “To” field in an email, you can set the append
parameter to true
:
fields.To.setLookupValue("contact", "47f33b76-4fd2-40de-9c0e-23fa0b928ebb", "John Doe", true);
When append
is true
, the new value will be added to the existing list of values rather than replacing it.
Clearing Lookup Fields
You can clear a lookup field’s value by setting it to null
, which removes the linked record from the field:
fields.Customer.Value = null; // Clears the lookup field
This feature is useful when you need to reset or remove the association with the linked record.
Setting Lookup Fields from Retrieved Data
In scenarios where you need to set a lookup based on data retrieved from other records, xrm-ex
provides a streamlined method with setLookupFromRetrieve
. This method is particularly useful in workflows where lookups are set dynamically from Web API data.
Example with a Single Record Retrieve
Imagine you want to link a contact’s parent account to another record. Here’s how you’d do it with setLookupFromRetrieve
:
var contact = await fields.Contact.retrieve("?$select=_parentcustomerid_value");
fields.Account.setLookupFromRetrieve("_parentcustomerid_value", contact);
In this example:
fields.Contact.retrieve(...)
fetches the contact’s parent account.
setLookupFromRetrieve
sets the account lookup field with the retrieved value.
Example with retrieveMultipleRecords
You may want to fetch multiple records and set the lookup based on a specific condition. Here’s how:
var result = await Xrm.WebApi.retrieveMultipleRecords("contact", "?$select=_parentcustomerid_value&$filter=prioritycode eq 1&orderby=createdon desc&$top=1");
if (result.entities.length > 0) {
fields.Account.setLookupFromRetrieve("_parentcustomerid_value", result.entities[0]);
}
This example retrieves contacts with priority code 1, sorted by creation date. The first record’s account is then used to set the lookup.
Summary of Key Points
- Accessing Properties: Use
.Id
, .EntityType
, and .FormattedValue
to retrieve specific properties of a lookup field.
- Setting Lookup Values: Use
setLookupValue
with parameters for entity type, ID, and formatted value. - Handling Multi-Value Lookups: Use the
append
parameter in setLookupValue
for lookups that support multiple values. - Clearing Lookup Fields: Set the lookup field’s
Value
to null
to remove the association. - Setting from Retrieved Data: Use
setLookupFromRetrieve
with Web API data to dynamically set lookup fields.