Wednesday 9 May 2018

Dynamics CRM : Composite Fields


Dynamics CRM provide Composite Address for Lead, Account and Contact entity. Currently there is no option to create custom composite attribute Might be in future Microsoft provide this capability.
In this article we will understand below topics: -

  • Composite Field
  • Lookup attribute filter
  • Passing message(Event) from CRM form to HTML web resource
  • Sending Parent Control in open form
  • Download Solution


1. Composite Field: - 
                                     As name says composite field is made of two or more fields. Before Dynamics CRM 2016 Address was only composite field preset in CRM, but now we have two composite attributes.
First Address attribute and second is Customer attribute.

At database level separate table is created to handle composite attributes.
Example: - When we create a new account in CRM from web, console, integration etc. (Except SQL insert query-not recommended, but possible) with or without address. Two records are created in address table for that account. This address table contains Guid and Record type for reference of created record.
Customer Attribute is special type of attribute which contain reference of account and contact entity. For Customer attribute also have different table created at database level.

Difference between Address and Customer composite attribute is we can create custom Customer attribute for our entity, but we cannot create custom composite Address.

2. Lookup attribute filter: - 
                                               From Dynamics CRM 2016 onward, we can configure filter for our lookup without use of any Javascript code. To enable lookup filter target entity must contains in source entity and target entity must contain in current entity.
Working: -
Current Entity: Composite Field
Source Lookup: State lookup
Target Lookup: Country lookup










































Open form editor and select state attribute and click on change properties. In Display tab we will see new “Related Record Filtering” option.
Now select country attribute and contains state.



For this filter State entity must contains Country lookup.


After this configuration when we select country, state will only show for selected country.


3. Passing message(Event) from CRM form to HTML web resource: - 
                                                                                                                    As we know we can use Dynamics CRM XRM library in our HTML web-resource with use of window.parent
window.parent.Xrm
But passing information from CRM form to HTML web-resource is little compilated.
It involves two step processes
First, create a function in Javascript an register onload, OnChange, OnSave or any other form event to post message to html web resource.
Second, create an event listener in your HTML web resource to process message.

Example: -
Create JavaScript web resource and add function to Post message to HTML web resource.

function CompositeFieldFunction()
{
   
    if (Xrm.Page.ui.controls.get('WebResource_CompositeHtml')) {
        var iFrame = Xrm.Page.ui.controls.get('WebResource_CompositeHtml').getObject();
        var sav_address = window.parent.Xrm.Page.getAttribute("sav_address").getValue();
        if (sav_address) {
            iFrame.contentWindow.postMessage({ 'addressDiv': sav_address[0].name }, '*');
        }
        else
            iFrame.contentWindow.postMessage({ 'addressDiv': "Click here to add address" }, '*');
    }
}


Add Event Listener Code in HTML web-resource
<script type="text/javascript" >
  window.addEventListener('message', function (event) {
            debugger;
            //Only accept messages from your trusted origins.
            if (~event.origin.indexOf(window.parent.Xrm.Page.context.getClientUrl())) {
                var messageData = event.data;
                if (messageData.addressDiv) {
                    document.getElementById("addressDiv").innerText = messageData.addressDiv;
                    document.getElementById("addressDivtolltip").innerText = messageData.addressDiv;
                }
               
            } else {
                return;
            }
        })

      
    </script>

We have created a web-resource and added in Account Form named (“WebResource_CompositeHtml”)






























4. Sending Parent Control in open form: -
                                                                       Dynamics CRM XRM library provide function to open any form with using code Xrm.Utility.openEntityForm(name, id, parameters, windowOptions);
When we use this code, we can open new create form or an existing record based on id parameter.
But this code will not allow us to update target Lookup control until we provide parent Control.
Example: - We have created a HTML web-resource and on click we are opening entity create/update form. We need to set new created record in our lookup attribute.
So we need to use below parameter.
parameters["parentLookupControlId"] = "Your lookup attribute value.";
Complete function as below.
function OnclickHtml() {
    var sav_address = window.parent.Xrm.Page.getAttribute("sav_address").getValue();
    var id = null;
    var name = "sav_composite";
    var parameters = {};
    parameters["formid"] = "8A9401A5-AB22-4828-9622-8B9814CA70EE";
    var windowOptions = {
        openInNewWindow: true
    };
    if (sav_address) {
        id = sav_address[0].id;
    }
    parameters["parentLookupControlId"] = "sav_address";
    parameters["pagetype"] = "entityrecord";
    Xrm.Utility.openEntityForm(name, id, parameters, windowOptions);
}

For demo working of these concepts you can also download complete solution below link. This solution is created in Dynamics 365 version and We should use this in online trail version for learning.


Click on Follow to be update about my new blogs and share your feedback.

No comments:

Post a Comment

Field Security Profile - Based on Owner

 Recently received requirement related to Field security profile. Expectation : - 1.       Set to users need access of secure attributes. 2....

Test