Microsoft Dataverse 的客戶資料表(account、contact、 customeraddress和)儲存客戶關係、聯絡資訊,以及地址和運送資訊。 本文說明這些資料表之間的關聯,以及如何管理嵌入與空置的客戶地址紀錄。
帳戶表格
帳戶表是 Dataverse 中大多數其他資料表所附屬或為其父表的其中一個資料表。 在 Dataverse 中,客戶代表與業務單位有關係的公司。 帳戶中包含的資訊是所有相關的聯絡資訊、公司資訊、類別、關係類型和地址資訊。 其他適用的資訊包括以下項目:
- 帳戶可以是大部分資料表類型的父項,包括另一個帳戶。
- 帳戶可以是獨立表格。
- 一個帳戶只能有一個帳戶作為其父帳戶。
- 帳戶可以有多個子帳戶和子聯絡人。
客戶管理是企業對企業客戶關係管理 (Dynamics 365) 的重要概念之一,因為組織想要查看他們與另一家公司的所有活動。 所有這些活動都會在帳戶層級匯集在一起。
查看帳戶表格參照。
在 Dataverse 中,連絡人代表與業務單位有關係的人員 (通常是個人),例如客戶、供應商或同事。 聯絡人資料表是大多數其他資料表所連結的資料表之一。 連絡人可以是一個獨立的資料表。 此表中包含專業、個人和家庭資訊,以及多個地址。
查看連絡人表格參考。
帳戶和連絡人都是客戶管理系統的一部分,並透過以下方式彼此相關:
聯絡表格會儲存人員的相關資訊,例如電子郵件地址、街道地址和電話號碼。 它還包括其他相關信息,例如此人的生日或週年紀念日。 根據業務單位擁有的客戶類型,它只需要連絡人,或連絡人和帳戶,即可全面了解其客戶。
將活動和附註等表格連結至 contact 表格,可讓使用者查看使用者與客戶的所有通訊、使用者代表客戶採取的任何動作,以及使用者需要的有關客戶的所有資訊。
CustomerAddress 資料表
此表格包含客戶記錄 (帳戶和連絡人) 的更多地址和運送資訊。 預設情況下,Dataverse 在建立新客戶紀錄時,會在這個表格中至少建立兩 customeraddress 筆紀錄,即使這些紀錄沒有相關資料。
學習如何改變這種行為。
您可以透過 Account_CustomerAddress and Contact_CustomerAddress 關係存取所有customeraddress與帳戶及聯絡人相關的紀錄。 這兩個關係都使用 parentid 查詢。 欄位 parentidtypecode 表示該地址所關聯的客戶紀錄類型。
融入於客戶記錄中的地址數據
您可以擷取或修改與客戶記錄相關的兩或三個內嵌 customeraddress 記錄的資料。
- 帳戶記錄具有欄位
address1_addressid 和 address2_addressid。
- 連絡人記錄有欄
address1_addressid、address2_addressid和address3_addressid。
這些欄位儲存 customeraddressid 數值。 其他客戶欄位,每個欄位前綴為 address1*、 address2*或 address3*,則包含表格中 customeraddress 對應的地址資訊。
欄位 customeraddressaddressnumber 顯示父客戶紀錄欄位適用的地址。 您無法將 addressnumber 欄位設定為同一父客戶相關的另一個 customeraddress 記錄所使用的值。 您可以將現有 addressnumber 值設定為 0 或 Null,然後如果您想要交換客戶記錄的記錄相對位置,請變更另一筆記錄的值。 雖然 addressnumber 的值在為客戶建立的每筆記錄中會遞增,並且控制客戶記錄中內嵌地址的位置(1、2 或 3),addressnumber 資料行的值則不會用於其他任何用途。
Dataverse 僅透過對應的客戶記錄欄更新這些 customeraddress 記錄,而不是直接更新 customeraddress 資料列。 不過,任何人都可以將這些記錄編輯為 customeraddress 記錄,或新增更多與 customeraddress 、 account 或 contact 記錄相關聯的記錄,這些記錄未嵌入在帳戶和連絡人記錄中。
不允許刪除內嵌的客戶地址列
預設情況下,如果你嘗試刪除 、 address2_addressid或 address3_addressid 所引用的客戶記錄中的內嵌customeraddress紀錄address1_addressid,會收到如下錯誤訊息:
名稱:CannotDeleteDueToAssociation
錯誤代碼:0x80040227
編號:-2147220953
訊息:Customer Address can not be deleted because it is associated with another object. Address Id = 4f33c2e4-d5a3-4b03-b050-21984c0e4c15, AddressNumber=2, ParentId=4b757ff7-9c85-ee11-8179-000d3a9933c9, ObjectTypeCode=1
瞭解如何改變此行為
禁用建立空白記錄
表格中的 customeraddress 每一列都會計入你付費的 Dataverse 容量,所以你可能想盡量降低這筆成本。
您可以變更 customeraddress中的停用空地址記錄建立設定,告訴 Dataverse 不要為每個客戶記錄建立空資料表列。 在更改這個行為之前,請考慮你是否有依賴預設行為的自訂功能。
深入了解此設定。
開啟此設定時,建立新客戶記錄時不會建立新的空 customeraddress 資料表列。 只有在傳入承載包含地址資料時,才會建立記錄。 通常,資料只包含有資料的欄位。 如果欄位沒有資料,該欄位將不包含在資料中,並且在記錄儲存時值為 null。 不過,如果承載包含值設定為 Null 的位址資料行,則會使用 Null 值建立位址。 如果您繼續看到有空白記錄被建立,請檢查它們是如何被建立的,以及該用戶端應用程式是否正在傳送具有空值的資料行資料。
如果關閉停用 空白地址記錄建立設定 ,則預設行為會恢復。 開啟此設定不會刪除任何現有 customeraddress 的表格列。 關閉此設定後重新開啟,不會重新建立未建立的記錄。
檢測是否已停用空白地址記錄的建立
這些範例函式示範如何偵測環境中是否啟用了停用 空白地址記錄建立 設定。
此 Test-IsEmptyAddressRecordCreationDisabled PowerShell 函式使用 Microsoft Power Platform CLIpac env list-settings 指令來檢查欄位中的Organization.OrgDbOrgSettings值。
function Test-IsEmptyAddressRecordCreationDisabled {
$orgdborgsettings = pac env list-settings `
--filter orgdborgsettings `
| Where-Object { $_ -match '^orgdborgsettings\s+' } `
| ForEach-Object { $_ -replace '^orgdborgsettings\s+', '' }
$xml = [xml]$orgdborgsettings
$element = $xml.SelectSingleNode("//CreateOnlyNonEmptyAddressRecordsForEligibleEntities")
# Return true only when the element exists and has the value of 'true'
return ($null -ne $element) -and ($element.InnerText -eq 'true')
}
了解更多關於 Microsoft Power Platform CLI 的資訊
此靜態 IsEmptyAddressRecordCreationDisabled 方法會使用 WhoAmIRequest 類別 和 IOrganizationService.Retrieve 方法 來檢查 Organization.OrgDbOrgSettings 資料行中的值。
static bool IsEmptyAddressRecordCreationDisabled(IOrganizationService service)
{
Guid orgId = ((WhoAmIResponse)service
.Execute(new WhoAmIRequest())).OrganizationId;
Entity organization = service
.Retrieve("organization", orgId, new ColumnSet("orgdborgsettings"));
XDocument orgdborgsettings = XDocument
.Parse((string)organization["orgdborgsettings"]);
XElement? element = orgdborgsettings
.XPathSelectElement("//CreateOnlyNonEmptyAddressRecordsForEligibleEntities");
// Return true only when the element exists and has the value of 'true'
return element != null && element.Value == "true";
}
此 Test-IsEmptyAddressRecordCreationDisabled PowerShell 函式使用了 Create 可重用函式的範例Get-WhoAmI及Get-Record函式,並依賴於 Connect 函式中設定的全域變數。
function Test-IsEmptyAddressRecordCreationDisabled {
$orgId = Get-WhoAmI | Select-Object -ExpandProperty OrganizationId
$orgdborgsettings = Get-Record `
-setName 'organizations' `
-id $orgId `
-query "?`$select=orgdborgsettings"
| Select-Object `
-ExpandProperty orgdborgsettings
$xml = [xml]$orgdborgsettings
$element = $xml.SelectSingleNode("//CreateOnlyNonEmptyAddressRecordsForEligibleEntities")
# Return true only when the element exists and has the value of 'true'
return ($null -ne $element) -and ($element.InnerText -eq 'true')
}
刪除內嵌地址記錄
預設情況下,你無法刪除客戶資料表中 、 、 或address3_addressid欄位所引用的嵌入customeraddress資料列address1_addressid。 address2_addressid 欲了解更多資訊,請參閱 「不允許刪除嵌入的客戶地址列」。
Power Platform 系統管理中心中的啟用刪除地址記錄設定會變更此行為。
深入了解此設定。
偵測是否啟用刪除地址記錄
這些範例函式示範如何偵測環境中是否已啟用 [啟用刪除位址記錄 ] 設定。
此 Test-IsDeleteAddressRecordsEnabled PowerShell 函式使用 Microsoft Power Platform CLIpac env list-settings 指令來檢查EnableDeleteAddressRecords欄位中的Organization.OrgDbOrgSettings值。
function Test-IsDeleteAddressRecordsEnabled {
$orgdborgsettings = pac env list-settings `
--filter orgdborgsettings `
| Where-Object { $_ -match '^orgdborgsettings\s+' } `
| ForEach-Object { $_ -replace '^orgdborgsettings\s+', '' }
$xml = [xml]$orgdborgsettings
$element = $xml.SelectSingleNode("//EnableDeleteAddressRecords")
# Return true only when the element exists and has the value of 'true'
return ($null -ne $element) -and ($element.InnerText -eq 'true')
}
了解更多關於 Microsoft Power Platform CLI 的資訊
此靜態IsDeleteAddressRecordsEnabled方法使用 WhoAmIRequest 類別及IOrganizationService.Retrieve該方法檢查EnableDeleteAddressRecords欄位中的Organization.OrgDbOrgSettings值。
static bool IsDeleteAddressRecordsEnabled(IOrganizationService service)
{
Guid orgId = ((WhoAmIResponse)service
.Execute(new WhoAmIRequest())).OrganizationId;
Entity organization = service
.Retrieve("organization", orgId, new ColumnSet("orgdborgsettings"));
XDocument orgdborgsettings = XDocument
.Parse((string)organization["orgdborgsettings"]);
XElement? element = orgdborgsettings
.XPathSelectElement("//EnableDeleteAddressRecords");
// Return true only when the element exists and has the value of 'true'
return element != null && element.Value == "true";
}
此 Test-IsDeleteAddressRecordsEnabled PowerShell 函式使用了 Create reusable functions 中描述的範例Get-WhoAmI及Get-Record函式,並依賴於 Connect 函式中設定的全域變數。
function Test-IsDeleteAddressRecordsEnabled {
$orgId = Get-WhoAmI | Select-Object -ExpandProperty OrganizationId
$orgdborgsettings = Get-Record `
-setName 'organizations' `
-id $orgId `
-query "?`$select=orgdborgsettings"
| Select-Object `
-ExpandProperty orgdborgsettings
$xml = [xml]$orgdborgsettings
$element = $xml.SelectSingleNode("//EnableDeleteAddressRecords")
# Return true only when the element exists and has the value of 'true'
return ($null -ne $element) -and ($element.InnerText -eq 'true')
}
大量刪除空白客戶地址記錄
在停用空位址記錄建立並啟用刪除位址紀錄後,請使用以下範例函式,透過訊息BulkDelete非同步刪除空customeraddress紀錄。
批量刪除程式碼範例
這些函式是以 地址 (CustomerAddress) 可寫入資料行/屬性 為基礎,不包含您環境中可能存在的任何自訂資料行。 如果您需要包含自訂資料行,您可能需要變更這些查詢。
Start-BulkDeleteEmptyCustomerAddressRecords PowerShell 函式會建立一個系統工作,透過使用 Microsoft Power Platform CLIpac data bulk-delete schedule 指令刪除空customeraddress記錄。
此功能依據範例及 Test-IsEmptyAddressRecordCreationDisabledDetect 是否啟用刪除位址記錄與偵測是否啟用空位址記錄建立的功能而定Test-IsDeleteAddressRecordsEnabled。 只有當兩個設定都允許刪除所有空客戶地址紀錄並防止建立新的空紀錄時,該函式才會建立該工作。
function Start-BulkDeleteEmptyCustomerAddressRecords {
if (!(Test-IsDeleteAddressRecordsEnabled)) {
Write-Host 'Enable deletion of address records before running this function.'
return
}
if (!(Test-IsEmptyAddressRecordCreationDisabled)) {
Write-Host 'Disable empty address record creation before running this function.'
return
}
$fetchXml = @(
'<fetch>'
"<entity name='customeraddress'>"
"<attribute name='customeraddressid' />"
"<filter type='and'>"
"<condition attribute='city' operator='null' />"
"<condition attribute='country' operator='null' />"
"<condition attribute='county' operator='null' />"
"<condition attribute='fax' operator='null' />"
"<condition attribute='freighttermscode' operator='null' />"
"<condition attribute='latitude' operator='null' />"
"<condition attribute='line1' operator='null' />"
"<condition attribute='line2' operator='null' />"
"<condition attribute='line3' operator='null' />"
"<condition attribute='longitude' operator='null' />"
"<condition attribute='postalcode' operator='null' />"
"<condition attribute='postofficebox' operator='null' />"
"<condition attribute='primarycontactname' operator='null' />"
"<condition attribute='shippingmethodcode' operator='null' />"
"<condition attribute='stateorprovince' operator='null' />"
"<condition attribute='telephone1' operator='null' />"
"<condition attribute='telephone2' operator='null' />"
"<condition attribute='telephone3' operator='null' />"
"<condition attribute='upszone' operator='null' />"
"<condition attribute='utcoffset' operator='null' />"
'</filter>'
'</entity>'
'</fetch>'
) -join ''
pac data bulk-delete schedule `
--entity customeraddress `
--fetchxml $fetchXml `
--job-name 'Delete Empty Customer Address Records'
}
靜態BulkDeleteEmptyCustomerAddressRecords方法則透過使用 BulkDeleteRequest 類別建立一個系統工作來刪除空customeradddress記錄。
此方法分別使用IsDeleteAddressRecordsEnabled和偵測是否IsEmptyAddressRecordCreationDisabled建立中所述的範例和靜態方法,以確保這些設定已設定為允許刪除所有空白客戶地址記錄,並確保不會建立新記錄。
/// <summary>
/// Create a Bulk Delete job to delete empty customer address records
/// </summary>
/// <param name="service">The authenticated IOrganizationService instance.</param>
/// <returns>The Id of the system job</returns>
/// <exception cref="Exception"></exception>
static Guid BulkDeleteEmptyCustomerAddressRecords(IOrganizationService service)
{
if (!IsDeleteAddressRecordsEnabled(service))
{
throw new Exception("Enable deletion of address records" +
" before running this method.");
}
if (!IsEmptyAddressRecordCreationDisabled(service))
{
throw new Exception("Disable empty address record creation" +
" before running this method.");
}
var query = new QueryExpression("customeraddress")
{
ColumnSet = new ColumnSet("customeraddressid"),
Criteria =
{
Conditions =
{
new ConditionExpression("city", ConditionOperator.Null),
new ConditionExpression("country", ConditionOperator.Null),
new ConditionExpression("county", ConditionOperator.Null),
new ConditionExpression("fax", ConditionOperator.Null),
new ConditionExpression("freighttermscode", ConditionOperator.Null),
new ConditionExpression("latitude", ConditionOperator.Null),
new ConditionExpression("line1", ConditionOperator.Null),
new ConditionExpression("line2", ConditionOperator.Null),
new ConditionExpression("line3", ConditionOperator.Null),
new ConditionExpression("longitude", ConditionOperator.Null),
new ConditionExpression("postalcode", ConditionOperator.Null),
new ConditionExpression("postofficebox", ConditionOperator.Null),
new ConditionExpression("primarycontactname", ConditionOperator.Null),
new ConditionExpression("shippingmethodcode", ConditionOperator.Null),
new ConditionExpression("stateorprovince", ConditionOperator.Null),
new ConditionExpression("telephone1", ConditionOperator.Null),
new ConditionExpression("telephone2", ConditionOperator.Null),
new ConditionExpression("telephone3", ConditionOperator.Null),
new ConditionExpression("upszone", ConditionOperator.Null),
new ConditionExpression("utcoffset", ConditionOperator.Null)
}
}
};
BulkDeleteRequest request = new()
{
QuerySet = new QueryExpression[] { query },
StartDateTime = DateTime.UtcNow,
RecurrencePattern = string.Empty,
SendEmailNotification = false,
JobName = "Delete empty customer address records",
ToRecipients = new List<Guid>().ToArray(),
CCRecipients = new List<Guid>().ToArray()
};
var response = (BulkDeleteResponse)service.Execute(request);
return response.JobId;
}
Start-BulkDeleteEmptyCustomerAddressRecords PowerShell 函式會透過 BulkDelete 動作建立一個系統工作來刪除空customeradddress紀錄。
此方法分別使用偵測是否Test-IsDeleteAddressRecordsEnabled和偵測是否Test-IsEmptyAddressRecordCreationDisabled中所述的範例和函數,以確保這些設定已配置為允許刪除所有空客戶地址記錄,並確保不會建立新地址記錄。
function Start-BulkDeleteEmptyCustomerAddressRecords {
if (!(Test-IsDeleteAddressRecordsEnabled)) {
Write-Host 'Enable deletion of address records before running this function.'
return
}
if (!(Test-IsEmptyAddressRecordCreationDisabled)) {
Write-Host 'Disable empty address record creation before running this function'
return
}
$query = [ordered]@{
EntityName = 'customeraddress'
ColumnSet = @{
Columns = @('customeraddressid')
}
Criteria = @{
Conditions = @(
@{
AttributeName = 'city'
Operator = 'Null'
},
@{
AttributeName = 'country'
Operator = 'Null'
},
@{
AttributeName = 'county'
Operator = 'Null'
},
@{
AttributeName = 'fax'
Operator = 'Null'
},
@{
AttributeName = 'freighttermscode'
Operator = 'Null'
},
@{
AttributeName = 'latitude'
Operator = 'Null'
},
@{
AttributeName = 'line1'
Operator = 'Null'
},
@{
AttributeName = 'line2'
Operator = 'Null'
},
@{
AttributeName = 'line3'
Operator = 'Null'
},
@{
AttributeName = 'longitude'
Operator = 'Null'
},
@{
AttributeName = 'postalcode'
Operator = 'Null'
},
@{
AttributeName = 'postofficebox'
Operator = 'Null'
},
@{
AttributeName = 'primarycontactname'
Operator = 'Null'
},
@{
AttributeName = 'shippingmethodcode'
Operator = 'Null'
},
@{
AttributeName = 'stateorprovince'
Operator = 'Null'
},
@{
AttributeName = 'telephone1'
Operator = 'Null'
},
@{
AttributeName = 'telephone2'
Operator = 'Null'
},
@{
AttributeName = 'telephone3'
Operator = 'Null'
},
@{
AttributeName = 'upszone'
Operator = 'Null'
},
@{
AttributeName = 'utcoffset'
Operator = 'Null'
}
)
}
}
$body = [ordered]@{
QuerySet = @($query)
StartDateTime = (Get-Date).ToUniversalTime().ToString('yyyy-MM-ddTHH:mm:ssZ')
RecurrencePattern = ''
SendEmailNotification = $false
JobName = 'Delete Empty Customer Address Records'
ToRecipients = @()
CCRecipients = @()
}
$postHeaders = $baseHeaders.Clone()
$postHeaders.Add('Content-Type', 'application/json')
$BulkDeleteRequest = @{
Uri = $baseURI + 'BulkDelete'
Method = 'Post'
Headers = $postHeaders
Body = ConvertTo-Json $body -Depth 5
}
Invoke-RestMethod @BulkDeleteRequest | Select-Object -ExpandProperty JobId
}