CRM 2011 - 使用 JScript 设置默认值
我们本地有 CRM 2011。联系人实体已自定义为使用对自定义实体国家/地区的查找,而不仅仅是文本字段。创建新联系人时,我们希望将国家/地区字段默认设置为加拿大。我有以下函数可以做到这一点:
function SetDefaultCountryCode(countryFieldId) {
var _canadaId = "{FC167B4D-1C3B-E111-8904-F2EA3FE25706}";
var countryControl = Xrm.Page.getAttribute(countryFieldId);
// only attempt the code if the control exists on the form
if (countryControl != null) {
var currentCountry = countryControl.getValue();
// if country is not specified, then set it to the default one (Canada)
if (currentCountry == null) {
var defaultCountry = new Object();
defaultCountry.entityType = "cga_country";
defaultCountry.id = _canadaId;
defaultCountry.name = "Canada";
var countryLookupValue = new Array();
countryLookupValue[0] = defaultCountry;
countryControl.setValue(countryLookupValue);
}
}
}
在 OnLoad 表单上,我调用如下函数:
// set Country fields to Canada if not set
SetDefaultCountryCode('cga_address1country');
我们有两个服务器 - DEV 和 TEST。这个 JScript 在 DEV 中运行良好。当我在 TEST 中运行它时,它不起作用,因为 TEST 中的加拿大有不同的 id (GUID) - 当我手动创建它时。我希望可以从 DEV 导出 Country 实体值并将其导入到 TEST 中并保留其 GUID。不幸的是这不起作用。我将数据导出到 Excel 文件,其中包含国家/地区的 GUID。在导入之前,我还会删除 TEST 中任何现有的国家/地区记录。当我尝试导入它时,导入成功但没有创建任何记录。如果我在 Excel 文件中添加新行而不指定 Guid,它将导入它。在我看来,导入功能并不是为了保留记录的 GUID。但这也意味着我的脚本将无法工作,因为它依赖于 GUID。
我这里有两个问题:
是否可以导出/导入保留 GUID 的实体数据?
如果我在 DEV 和 TEST 中无法拥有相同的 GUID,我如何才能使 JScript 正常工作?
预先感谢您的任何帮助/反馈。
We have CRM 2011 on premise. The Contact entity was customized to use a lookup to a custom entity Country instead of just a text field. When creating a new Contact we would like the country field to be set to Canada by default. I have the following function that does that:
function SetDefaultCountryCode(countryFieldId) {
var _canadaId = "{FC167B4D-1C3B-E111-8904-F2EA3FE25706}";
var countryControl = Xrm.Page.getAttribute(countryFieldId);
// only attempt the code if the control exists on the form
if (countryControl != null) {
var currentCountry = countryControl.getValue();
// if country is not specified, then set it to the default one (Canada)
if (currentCountry == null) {
var defaultCountry = new Object();
defaultCountry.entityType = "cga_country";
defaultCountry.id = _canadaId;
defaultCountry.name = "Canada";
var countryLookupValue = new Array();
countryLookupValue[0] = defaultCountry;
countryControl.setValue(countryLookupValue);
}
}
}
On the form OnLoad I invoke the function like that:
// set Country fields to Canada if not set
SetDefaultCountryCode('cga_address1country');
We have two servers - DEV and TEST. This JScript works fine in DEV. When I run it in TEST it does not work because the Canada in TEST has different id (GUID) - when I create it manually. I was hoping I could export the Country entity values from DEV and import them in TEST preserving their GUIDs. Unfortunately this did not work. I export the data to Excel file and it has the GUIDs of the countries. I also delete any existing Country records in TEST before importing. When I try to import it the import succeeds but does not create any records. If I add a new row in the excel file without specifing a Guid it will import it. It seems to me the import functionality was not meant to preserve the GUIDs of the records. But this also means my script will not work because it depends on the GUIDs.
I have two questions here:
Is it possible to export / import entity data preserving the GUIDs ?
If I cannot have the same GUIDs in DEV and TEST how I can make the JScript to work properly?
Thank you in advance for any help / feedback.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对 GUID 进行硬编码是非常糟糕的做法,您发现了它的问题。
正如您上面所说,我们不能有相同的 GUID,但我们有相同的名称。因此,我们必须使用 JScript 和 jQuery 查询国家/地区名称来检索 GUID。
为了从客户端(或实体表单)撤回信息:
让我们寻找查询 REST 端点。
获取此 URL,替换您的实际值并将其粘贴到浏览器中,您会发现响应以 XML 格式返回。如果有任何错误,请确保实体名称及其属性区分大小写。
查看结果后,我们将使用 AJAX 调用来调用此 URL。
但在将代码复制到表单之前,您必须从此处下载 jQuery lib
然后将其作为 Web 资源上传,将此 Web 资源添加到表单加载库中。
以下是要放入表单加载事件处理程序中的完整代码:
还有一件事,不要忘记选中表单属性上的“将执行上下文作为第一个参数传递”框。
编辑:除了将 jQuery 库添加到表单加载事件处理程序之外,还添加 Json2 lib 作为网络资源。
有关REST 端点的详细信息。
It's very bad practice to hard code your GUIDs and you discovered the problems of it.
As you stated above, we cannot have the same GUIDs but we have the same name. So, we have to query the name of the country using JScript and jQuery to retrieve the GUID.
In order to retireve information from client-side (or Entity Form):
Lets, look for querying REST Endpoint.
Take this URL, subsitute your actual values and paste it into your browser, you'll find that the response is returned in XML format. If there is any error, please ensure that the Entity name and its attribute are case senisitve.
After seeing your your results, we are going to call this URL using an AJAX call.
But before you copy the code to your form, you have to download the jQuery lib from here
Then upload it as a Web resource, add this web resource to the Form load libs.
Here is the complete code to be put in the form load event handler:
One more thing, don't forget to check "Pass execution context as first parameter" box on the form properties.
EDIT: Beside adding the jQuery library into the form load event handler, add the Json2 lib as a web resource.
For more information about the REST Endpoint.
确实可以导出和导入记录及其指南,但不是本地的。您必须构建一个可以为您导出数据的应用程序,然后通过目标环境中的 CRM API 创建相同的记录。您只需清除对创建无效的字段(createdon、statecode 等)并指定相同的 Guid。然后 CRM 将使用该 Guid 创建记录。
旧的 4.0 配置数据工具执行此操作。我不记得它是否适用于 2011 年的组织,但这可能是一个起点。
It is indeed possible to export and import records along with their guids, just not natively. You'd have to build an app that would export the data for you, then create identical records through the CRM API in the target environment. You just have to clear out fields that aren't valid for create (createdon, statecode, etc.) and just specify the same Guid. CRM will then create the record with that Guid.
The old 4.0 Configuration Data Tool does this. I can't recall if it works against a 2011 org, but it could be a starting point.