Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 496
krlzlx Avatar asked Nov 06 '13 10:11

krlzlx


People also ask

How do I set default value in lookup field in CRM?

You need the guid of the record, because every people, every lookup, every sorting also will give different index. To set a default lookup value, crm only accepts guid of that exact record hope that can help you.

How do you lock fields in CRM form?

Lock Fields on the Form – Manual First step is to manually locking this field on the form. If you open the Field Properties, you'll see that you can actually select Lock the field on the form. This will work and ensure that the field is locked on the form.

Can we create a new field from form customization?

From the list of fields, click New to create a new field or double-click any of the fields on the list to edit them. Expand the entity and choose the Forms node. Open a form in the form editor and below the Field Explorer click New Field to create a new field.


2 Answers

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 (e.g. 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.

like image 196
Thomas Junk Avatar answered Oct 12 '22 04:10

Thomas Junk


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);
            }
        });
    }
like image 40
Finickyflame Avatar answered Oct 12 '22 04:10

Finickyflame