简体   繁体   English

Dynamics CRM 2013:从实体设置表单中的默认值

[英]Dynamics CRM 2013: Setting default values in a form from an entity

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 ? 如果可能的话,是否有任何文档或示例代码可以做到这一点?

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 (eg your CTI-software: prepopulating a new contact-form with the number of the caller). 当从第三方软件调用CRM页面时,这种方式非常方便(例如,您的CTI软件:使用调用者号码预填充新的联系表单)。

Of course, you could use ordinary javascript to manipulate the form in any kind, you want. 当然,您可以使用普通的javascript来操作您想要的任何类型的表单。 That is possible, but not encouraged by Microsoft: 这是可能的,但不是微软鼓励的:

JavaScript developers are used to interacting with Document Object Model (DOM) elements in code. JavaScript开发人员习惯于在代码中与文档对象模型(DOM)元素进行交互。 You might use the window.getElementById method or the jQuery library. 您可以使用window.getElementById方法或jQuery库。 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. 您可以在HTML Web资源中自由使用这些技术,但不支持它们访问Microsoft Dynamics CRM应用程序页面或实体表单中的元素。 Instead, access to entity form elements are exposed through the Xrm.Page object model. 而是通过Xrm.Page对象模型公开对实体表单元素的访问。 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 Microsoft Dynamics CRM开发团队保留更改页面组成方式的权限,包括元素的ID值,因此使用Xrm.Page对象模型可保护代码免受页面实现方式的更改

The " Microsoft-Way " for doing things is via the Xrm.Page -Object. 处理事务的“ Microsoft-Way ”是通过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端点,您应该获得所需的错误信息。

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. CRM SDK中已经有一些关于如何查询其他实体以获取信息的示例,但我建议您使用此库来完成工作。 http://crmrestkit.codeplex.com/ 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_key(用于相关属性名称);
  • new_value (for the default value); new_value(默认值);
  • new_userid (for the user); 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);
            }
        });
    }

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. 你不能出错。

I would suggest you look at using Business Rules to do this. 我建议你看看使用业务规则来做到这一点。 Condition = SomeField Does not contain data. Condition = SomeField不包含数据。 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. 业务规则也可移植到移动和平板电脑客户端,无需重写或重新测试。

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. 虽然extraqs可以根据查询字符串参数自动设置CRM字段值,但这仅适用于创建记录。 see https://msdn.microsoft.com/en-us/library/gg334375.aspx 请参阅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. 但是,如果由于新的Turbo Forms渲染引擎而使用CRM 2015更新1,则需要使用parent.window.location.href。 see below. 见下文。

https://community.dynamics.com/crm/b/develop1/archive/2015/05/24/turbo-forms-get-your-javascript-ready-for-crm2015-update-1 https://community.dynamics.com/crm/b/develop1/archive/2015/05/24/turbo-forms-get-your-javascript-ready-for-crm2015-update-1

Hope this helps someone. 希望这有助于某人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM