Dynamics 365 FO中更新姓名时查询用户错误的技术问题
在Dynamics 365 FO中修正用户姓名更新的查询逻辑问题
原代码的核心问题
- 第一个查询里,
dirPerson变量未初始化赋值,直接用dirPerson.RecId == dirPersonName.Person根本无法定位到目标用户,属于无效查询。 - 第二个查询的关联逻辑错误:
dirPersonName.RecId == dirPartyTable.RecId的关联关系不成立,正确的关联链应该是客户表(cust) -> 方表(dirPartyTable) -> 自然人表(dirPerson) -> 自然人姓名表(dirPersonName)。 - 全名拆分逻辑不合理:用固定位置的
subStr截取姓名,完全不考虑实际全名的格式(比如姓名长度、分隔符),会导致姓名取值错误。
修正后的完整代码示例
public void updatePersonNameFromCust(str _accountNum, str _fullName) { CustTable cust; DirPartyTable dirPartyTable; DirPerson dirPerson; DirPersonName dirPersonName; str[] nameParts; str firstName, middleName, lastName; Date currDate = systemDateGet(); // 1. 通过客户账号定位到对应的自然人记录 select firstonly cust where cust.AccountNum == _accountNum join dirPartyTable where dirPartyTable.RecId == cust.Party join dirPerson where dirPerson.Party == dirPartyTable.RecId; if (!dirPerson.RecId) { throw error(strFmt("未找到账号为%1的对应自然人记录", _accountNum)); } // 2. 合理拆分全名(示例按空格分隔,可根据实际格式调整) nameParts = strSplit(_fullName, ' '); switch (nameParts.length()) { case 2: firstName = nameParts[1]; lastName = nameParts[2]; break; case 3: firstName = nameParts[1]; middleName = nameParts[2]; lastName = nameParts[3]; break; default: // 处理其他格式,比如只有 lastName 或特殊命名规则 lastName = _fullName; break; } // 3. 查询并更新自然人姓名记录 ttsbegin; select forupdate validTimeState(currDate) dirPersonName where dirPersonName.Person == dirPerson.RecId; if (dirPersonName.RecId) { // 按需更新,空值才更新(保留原逻辑) if (!dirPersonName.FirstName && firstName) { dirPersonName.FirstName = firstName; } if (!dirPersonName.MiddleName && middleName) { dirPersonName.MiddleName = middleName; } if (!dirPersonName.LastName && lastName) { dirPersonName.LastName = lastName; } dirPersonName.validTimeStateUpdateMode(ValidTimeStateUpdate::Correction); dirPersonName.doUpdate(); } else { // 若不存在姓名记录,创建新记录 dirPersonName.Person = dirPerson.RecId; dirPersonName.FirstName = firstName; dirPersonName.MiddleName = middleName; dirPersonName.LastName = lastName; dirPersonName.StartDate = currDate; dirPersonName.EndDate = DateTimeUtil::maxValue(); dirPersonName.insert(); } ttscommit; }
关键修正点说明
- 关联逻辑修正:严格按照
CustTable -> DirPartyTable -> DirPerson -> DirPersonName的关联关系查询,确保定位到目标用户的姓名记录。 - 全名拆分优化:改用
strSplit按分隔符拆分全名,适配不同姓名格式,避免固定位置截取的错误。 - 增加异常判断:添加了未找到自然人记录的错误抛出,避免后续空值操作。
- 兼容无姓名记录场景:如果目标用户没有对应的
DirPersonName记录,直接创建新记录覆盖场景。
内容的提问来源于stack exchange,提问作者user4833581
相关产品推荐
相关产品推荐

