简体   繁体   中英

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

I'm new to 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 ?

If it's possible, is there any documentation or sample code to do that ?

tl;dr

it should be possible and the starting point is provided below.

One possibility to populate your form with data is via the 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).

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 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

The " Microsoft-Way " for doing things is via the Xrm.Page -Object.

If you need userspecific information, look at Xrm.Page.context

Querying your REST-endpoint, you should get ervery information needed.

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

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/

You'll also need to add the JQuery library in your form scripts for ajax.

Now, assuming that your "Default parameter" entity is called "new_defaultparameter" and it contains the following attributes:

  • new_key (for the related attribute name);
  • new_value (for the default value);
  • new_userid (for the user);

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.

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. 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. see 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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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