javascript Dynamics CRM 2013:在实体的表单中设置默认值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19809896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dynamics CRM 2013: Setting default values in a form from an entity
提问by krlzlx
I'm new to Dynamics CRM 2013.
我是 Dynamics CRM 2013 的新手。
I'd like to be able to set default values on forms when the form loads. I was thinking of creating an entity "Default Parameter" to hold a couple of default values for a user.
我希望能够在表单加载时在表单上设置默认值。我正在考虑创建一个实体“默认参数”来为用户保存几个默认值。
For example, a delivery date or a default provider.
例如,交货日期或默认提供商。
Is it possible to create a script bound on a form on the event OnLoad and read the values of the entity "Default parameter" for the current user and set the fields of the form with theses values ?
是否可以在 OnLoad 事件上创建绑定在表单上的脚本并读取当前用户的实体“默认参数”的值并使用这些值设置表单的字段?
If it's possible, is there any documentation or sample code to do that ?
如果可能,是否有任何文档或示例代码可以做到这一点?
回答by Thomas Junk
tl;dr
tl;博士
it should be possible and the starting point is provided below.
应该是可能的,下面提供了起点。
One possibility to populate your form with data is via the query-string
用数据填充表单的一种可能性是通过 query-string
/main.aspx?etn=account&extraqs=name%3DNew%20Account&pagetype=entityrecord
taken from the documentation.
取自文档。
This way comes in pretty handy, when calling CRM-pages from 3rd Party software (e.g. your CTI-software: prepopulating a new contact-form with the number of the caller).
当从 3rd Party 软件调用 CRM 页面时,这种方式非常方便(例如,您的 CTI 软件:使用呼叫者的号码预先填充新的联系表格)。
Of course, you could use ordinary javascript to manipulate the form in any kind, you want. That is possible, but not encouraged by Microsoft:
当然,您可以使用普通的 javascript 来操作任何形式的表单,只要您愿意。这是可能的,但微软不鼓励:
JavaScript developers are used to interacting with Document Object Model (DOM) elements in code. You might use the window.getElementById method or the jQuery library. You are free to use these techniques in your HTML web resources, but they are not supported to access elements in Microsoft Dynamics CRM application pages or entity forms. Instead, access to entity form elements are exposed through the Xrm.Page object model. The Microsoft Dynamics CRM development team reserves the right to change how pages are composed, including the ID values for elements, so using the Xrm.Page object model protects your code from changes in how pages are implemented
JavaScript 开发人员习惯于在代码中与文档对象模型 (DOM) 元素进行交互。您可以使用 window.getElementById 方法或 jQuery 库。您可以在 HTML Web 资源中自由使用这些技术,但不支持它们访问 Microsoft Dynamics CRM 应用程序页面或实体表单中的元素。相反,对实体表单元素的访问是通过 Xrm.Page 对象模型公开的。Microsoft Dynamics CRM 开发团队保留更改页面组成方式的权利,包括元素的 ID 值,因此使用 Xrm.Page 对象模型可以保护您的代码免受页面实现方式的更改
The "Microsoft-Way" for doing things is via the Xrm.Page-Object.
做事的“微软方式”是通过Xrm.Page-Object。
If you need userspecific information, look at Xrm.Page.context
如果您需要特定于用户的信息,请查看Xrm.Page.context
Querying your REST-endpoint, you should get ervery information needed.
查询您的 REST 端点,您应该获得所需的所有信息。
回答by Finickyflame
Yes it is possible.
对的,这是可能的。
If you need the basic information on the CRM javascript scripting, I would recommend you to use this official Microsoft reference: http://msdn.microsoft.com/en-us/library/jj602964.aspx
如果您需要有关 CRM javascript 脚本的基本信息,我建议您使用 Microsoft 官方参考:http: //msdn.microsoft.com/en-us/library/jj602964.aspx
There's already some examples in the CRM SDK about how to query another entity to get information, but I would recommend you to use this library to do the job. http://crmrestkit.codeplex.com/
CRM SDK 中已经有一些关于如何查询另一个实体以获取信息的示例,但我建议您使用这个库来完成这项工作。 http://crmrestkit.codeplex.com/
You'll also need to add the JQuery library in your form scripts for ajax.
您还需要在 ajax 的表单脚本中添加 JQuery 库。
Now, assuming that your "Default parameter" entity is called "new_defaultparameter" and it contains the following attributes:
现在,假设您的“默认参数”实体称为“new_defaultparameter”并且它包含以下属性:
- new_key (for the related attribute name);
- new_value (for the default value);
- new_userid (for the user);
- new_key(用于相关属性名称);
- new_value(默认值);
- new_userid(对于用户);
You should have something like this:
你应该有这样的事情:
function onLoad()
{
if (Xrm.Page.ui.getFormType() == 1/*Create*/)
{
getDefaultFields(Xrm.Page.context.getUserId(), updateWithDefaultValue);
}
}
function getDefaultFields(userId, callback)
{
var filter = "new_userid/Id eq guid'" + userId + "'";
//you need to use the "Schema Name" for both the entity and the attributes
CrmRestKit.ByQuery('new_defaultparameter', ['new_key', 'new_value'], filter)
.done(function (data, status, xhr)
{
callback(data.d.results.map(function (field)
{
return { key: field['new_key'], value: field['new_value'] }
}));
});
}
function updateWithDefaultValue(keyValues)
{
keyValues.forEach(function (keyValue)
{
var attribute = null;
if (attribute = Xrm.Page.getAttribute(keyValue.key))
{
// You may need to add some logic to convert the value to
// the correct format.
// You can use the attribute.getAttributeType() to help you.
// See: http://msdn.microsoft.com/en-us/library/6881e99b-45e4-4552-8355-2eef296f2cd8#BKMK_getAttributeType
attribute.setValue(keyValue.value);
}
});
}
回答by Marcelle Jacobs
When I just started doing Jscript in CRM I came across a great link that contains all the basic scripts you need to do just about anything.
当我刚开始在 CRM 中使用 Jscript 时,我发现了一个很棒的链接,其中包含您执行任何操作所需的所有基本脚本。
http://garethtuckercrm.com/2011/03/16/jscript-reference-for-microsoft-dynamics-crm-2011/
http://garethtuckercrm.com/2011/03/16/jscript-reference-for-microsoft-dynamics-crm-2011/
You cant go wrong.
你不会出错。
回答by AdamV
I would suggest you look at using Business Rules to do this. Condition = SomeField Does not contain data. Action = Set value (to whatever you want it to be). Business Rules will be portable to mobile and tablet clients as well without rewriting or re-testing.
我建议你看看使用业务规则来做到这一点。Condition = SomeField 不包含数据。操作 = 设置值(设置为您想要的任何值)。业务规则也可以移植到手机和平板电脑客户端,无需重写或重新测试。
回答by Ant
Just wanted to follow up this answer in case it helps anyone.
只是想跟进这个答案,以防它对任何人有帮助。
Although extraqs works to automatically set a CRM field value based on a querystring parameter this only works on creation records. see https://msdn.microsoft.com/en-us/library/gg334375.aspx
尽管 extraqs 可以根据查询字符串参数自动设置 CRM 字段值,但这仅适用于创建记录。请参阅https://msdn.microsoft.com/en-us/library/gg334375.aspx
I had a situation where I needed this to work on edit of a record also to do this I had to use standard javascript window.location.href functionality.
我有一种情况,我需要它来编辑记录,为此我必须使用标准的 javascript window.location.href 功能。
Howevever note if using CRM 2015 update 1 onwards due to the new Turbo Forms rendering engine you will need to use parent.window.location.href. see below.
但是请注意,如果由于新的 Turbo Forms 渲染引擎而使用 CRM 2015 update 1 以后,您将需要使用 parent.window.location.href。见下文。
Hope this helps someone.
希望这可以帮助某人。