Thursday, 28 December 2017

Filter Option Sets in Dynamics CRM using Javascript

Introduction:
Suppose you have an Option set field named "optionSetField" that needs to show different values when field "optionSetFilterField" value is 1, and when field "optionSetFilterField" value is 2, it needs to show different values. This requirement can easily be achieved using JavaScript

Solution:
Create a new Web resource as shown below





In the Text Editor, paste the following JavaScript

function filterOptionSets() {
    //get option set control and its options
    var optionsetControl = Xrm.Page.ui.controls.get("optionSetField");
    var options = optionsetControl.getAttribute().getOptions();

    //get the field value on which bases the option set needs to be filtered
    var filterValue = Xrm.Page.getAttribute("optionSetFilterField").getValue();

    //get current selected value of option set
    var currentValue = Xrm.Page.getAttribute("optionSetField").getValue();

    //based on the filterValue, clear option set and then add the required options
    if (filterValue = 1) {
        optionsetControl.clearOptions();
        optionsetControl.addOption(options[0]);
        optionsetControl.addOption(options[1]);
        optionsetControl.addOption(options[2]);
    }
    else if (filterValue = 2) {
        optionsetControl.clearOptions();
        optionsetControl.addOption(options[3]);
        optionsetControl.addOption(options[6]);
    }
    else if (filterValue = 3) {
        optionsetControl.clearOptions();
        optionsetControl.addOption(options[4]);
        optionsetControl.addOption(options[5]);
        optionsetControl.addOption(options[7]);
    } 
   
    //if a value in option set field was already selected, re-set the previous value
    if (currentValue != null)
        Xrm.Page.getAttribute("optionSetField").setValue(currentValue);
}

//this method needs to be configured on "optionSetFilterField" field OnChange event
function onFieldChange() {
    Xrm.Page.getAttribute("optionSetField").setValue(null);
    filterOptionSets();
}

//this method needs to be configured on Form OnLoad event
function onFormLoad() {
    filterOptionSets();
}


Now, navigate to the Form Properties on the form where Option set control needs to be filtered and add the above created Web resource






Now set the Event Handlers as shown in the following images





Save the form and Publish the customization to start using the functionality!

Wednesday, 27 December 2017

Difference between CRM On-prem and Online Organization Service Authentication

Microsoft Dynamics CRM Organization service uses a slightly different method for authentication for CRM online version as compared to CRM on-premise. The difference between them is highlighted below:

//client credentials object
ClientCredentials credentials = new ClientCredentials();
credentials.SupportInteractive = false;

//when using CRM online
credentials.UserName.UserName = "test@test.com";
credentials.UserName.Password = "test123$";

//when using CRM on-premise
credentials.Windows.ClientCredential = new System.Net.NetworkCredential("test", "test123$", "test");

//create Organization service object
OrganizationServiceProxy proxyService = new OrganizationServiceProxy(new Uri(OrganizationURL), null, credentials, null);

Monday, 25 December 2017

Get Metadata in Operations 365

Introduction:
In Dynamics AX 2012, to get the tables, fields, forms, menu-items, you can query UtilElements table. However, starting Operations 365, this support is deprecated, even though the table is there, but it is empty. Instead of it, Microsoft has introduced a new class library, Microsoft.Dynamics.Ax.Xpp.MetadataSupport, which helps you to get Metadata objects.


This post will help you in getting all fields as well as table relations of SalesTable

Code Snippet:

public static void main(Args _args)
{
    //get SalesTable table name
    str tableName = tableStr(SalesTable);
    
    //Call the MetadataSupport method, GetTable to get table details
    AxTable axTable = Microsoft.Dynamics.Ax.Xpp.MetadataSupport::GetTable(tableName);  
        
    if(axTable != null)
    {                 
        info("****Fields****");
        
        //get list of fields and use an enumerator to loop through all fields      
        var axTableFieldList = axTable.Fields;        
        System.Collections.IEnumerator axTableFieldsEnumerator = axTableFieldList.GetEnumerator();

        while (axTableFieldsEnumerator.moveNext())
        {
            AxTableField axTableField = axTableFieldsEnumerator.get_Current();

            info(axTableField.Name);
        }
                
        info("****Relations****");
        
        //get all table relations as Key value pair
        var keyValueRelations  = Microsoft.Dynamics.Ax.Xpp.MetadataSupport::GetTableRelations(tableName);       
        var relationsEnumr = keyValueRelations.GetEnumerator();

        while (relationsEnumr.MoveNext())
        {
            //the relations are stored in Constraints object
            AxTableRelation tableRelation =  relationsEnumr.Current;
            var constraints = tableRelation.Constraints;
            var constraintsEnmr = constraints.GetEnumerator();

            while (constraintsEnmr.MoveNext())
            {
                AxTableRelationConstraintField constraintField =  constraintsEnmr.Current;
                
                info(tableRelation.RelatedTable);
                info(constraintField.Field);
            }            
        }
    }
}

Open attached file from DocuRef in browser in Dynamics Operations 365

Introduction:
To open an attach file on a browser tab in Dynamics Operations 365, you can use the following code snippet

Code Snippet:
    //get Public URL of the attach file from the DocuRef Buffer
    str displayUrl = DocumentManagement::getAttachmentPublicUrl(docuRef);
   
    //initialize browser object and navigate
    Browser br = new Browser();
    br.navigate(displayUrl);

Sunday, 24 December 2017

Attach File from a URL in Dynamics Operations 365

Introduction:
Suppose you have a File URL and you want to attach the file stored on that URL against a record in Dynamics Operation 365. Below snippet will help you in uploading the file to server and creating a new record in DocuRef table

Code Snippet:
    System.Net.WebClient webClient = new System.Net.WebClient();
    System.IO.Stream fileStream;
    System.Byte[] fileByteArr;
    DocuRef docuRef;  
    str fileURL = "www.myfilelocation.com/testfile.pdf"; //the URL of the file location
      
    //Download the file on client as byte array
    fileByteArr = webClient.DownloadData(fileURL);

    //convert byte array into a memory stream
    fileStream = new System.IO.MemoryStream(fileByteArr);

    //Use DocumentManagement class to attach the file and create a new record
    docuref = DocumentManagement::attachFile(tableId, recordId, companyId, docuTypeId, fileStream, fileName, mimeType, fileName);

Saturday, 23 December 2017

Get Value of Selected Record from a Reference Group Control in Dynamics AX

Suppose you have a reference group control ReferenceGroupCtrl in your form and you have defined an EDT on it. Now you want to get its selected record value.

To get RecId of the selected Record, you can use,
ReferenceGroupCtrl.value()

To get Text of the selected Record, you can use,
ReferenceGroupCtrl.controlNum(1).valueStr()
where 1 is the first control inside a reference group