In Microsoft Dynamics 365, creating a tailored user experience often requires customizing the way records are selected and handled. One common requirement is to allow users to select an address record from a filtered list, specific to an account and certain other criteria. This article will demonstrate how to create a custom lookup for the CustomerAddress
entity using JavaScript and the Xrm.Utility API.
Prerequisites
- Access to Microsoft Dynamics 365 with customization rights.
- Basic understanding of JavaScript and Dynamics 365 CRM customization.
Important Consideration: Limitations of the CustomerAddress Entity
The CustomerAddress entity in Dynamics 365 is an out-of-the-box (OOB) entity. One significant limitation of this entity is that you cannot customize its relationships, meaning you cannot create new lookup columns that append to or from this entity. This restriction necessitates alternative solutions for linking addresses with other records in a user-friendly and compliant manner.
Step-by-Step Implementation
Step 1: Define the Lookup Function
First, define a JavaScript function that you can trigger from any form or button within Dynamics 365. This function will open a lookup dialog filtered by specific conditions.
function openAddressLookup(accountId)
{
if (!accountId)
{
console.log("No Account ID provided.");
return;
}
var lookupOptions = {
defaultEntityType: "customeraddress",
entityTypes: ["customeraddress"],
allowMultiSelect: false,
filters: [
{
filterXml: `<filter>
<condition attribute="parentid" operator="eq" value="994f0f66-da8f-ee11-8179-000d3af4fc19" uiname="Tardis Tech Ltd" uitype="account" />
<condition attribute="addressnumber" operator="gt" value="2" />
<condition attribute="addresstypecode" operator="eq" value="1" />
</filter>`,
entityLogicalName: "customeraddress"
}
]
};
Xrm.Utility.lookupObjects(lookupOptions).then(
function (results)
{
if (results && results.length > 0)
{
var selectedAddressId = results[0].id;
console.log("Selected Address ID: " + selectedAddressId);
}
else
{
console.log("No Address Selected");
}
},
function (error)
{
console.log("Error: " + error);
}
);
}
Step 2: Explanation of Code Components
accountId
: The function requires an Account ID as input to filter the addresses. Ensure this is provided before calling the function.lookupOptions
: This object configures the lookup. It specifies the entity and includes a filter XML that limits the records to those associated with the specified Account ID, with an addressnumber
greater than 2, and addresstypecode
equal to 1 ("Bill To").
Step 3: Triggering the Lookup
The function can be triggered from a button, script, or workflow within Dynamics 365, depending on the user interaction design. Here’s a simple example of how to call this function from the browser's console for testing:
openAddressLookup('994f0f66-da8f-ee11-8179-000d3af4fc19');
Replace '994f0f66-da8f-ee11-8179-000d3af4fc19'
with the actual GUID of your account.
Test Result:
Conclusion
This custom lookup functionality enhances user experience by streamlining the selection of related records according to specific business rules. By implementing this in your Dynamics 365 environment, you can ensure that users are selecting the correct addresses, reducing errors and improving data integrity.
An example to use the Lookup Dialog to copy the selected address to parent form.
function copyFromAccountBillToAddress(executionContext)
{
debugger;
var formContext = executionContext.getFormContext();
var billingOptions = formContext.getAttribute("new_billingaddressoptions");
// Check if the billing option is set to 'Copy From Account Bill To Address' (100000005)
if (billingOptions && billingOptions.getValue() === 100000005)
{
var lookupOptions = {
defaultEntityType: "customeraddress",
entityTypes: ["customeraddress"],
allowMultiSelect: false,
filters: [
{
filterXml: `<filter>
<condition attribute="parentid" operator="eq" value="994f0f66-da8f-ee11-8179-000d3af4fc19" uiname="Tardis Tech Ltd" uitype="account" />
<condition attribute="addressnumber" operator="gt" value="2" />
<condition attribute="addresstypecode" operator="eq" value="1" />
</filter>`,
entityLogicalName: "customeraddress"
}
]
};
Xrm.Utility.lookupObjects(lookupOptions).then(
function (results)
{
if (results && results.length > 0)
{
var selectedAddressId = results[0].id;
// Retrieve the selected address details
Xrm.WebApi.retrieveRecord("customeraddress", selectedAddressId, "?$select=new_apartmentnumber,new_streetnumber,line1,line2,city,new_province,postalcode,new_country").then(
function (address)
{
// Populate the Quote form with the address details
if (address)
{
formContext.getAttribute("new_billtoapartmentnumber").setValue(address.new_apartmentnumber);
formContext.getAttribute("new_streetnumber").setValue(address.new_streetnumber);
formContext.getAttribute("billto_line1").setValue(address.line1);
formContext.getAttribute("billto_line2").setValue(address.line2);
formContext.getAttribute("billto_city").setValue(address.city);
formContext.getAttribute("new_billtoprovince").setValue(address.new_province);
formContext.getAttribute("billto_postalcode").setValue(address.postalcode);
formContext.getAttribute("new_billtocountrynew").setValue(address.new_country);
}
},
function (error)
{
console.log("Error retrieving address: ", error);
}
);
}
else
{
console.log("No Address Selected");
}
},
function (error)
{
console.log("Error: " + error);
}
);
}
}
No comments:
Post a Comment