Showing posts with label Dynamics CRM. Show all posts
Showing posts with label Dynamics CRM. Show all posts

Monday, May 22, 2017

Dynamics 365: To replace the labels in Entity Form using JavaScript


In this blog, have explained the steps for replacing the labels in Entity Form using JavaScript, XML and CSS files in Dynamics 365.

The total procedure contains 3 steps they are:

1.     XML file containing the label names to be replaced.

2.     Two JavaScript files are used here, one is for replacing the label and the other one to load the CSS dynamically.

3.     CSS file to change the styles of labels to wrap the text in the label.

XML Filename:

The most important things to be done in XML are naming root and nodes, id is the key to replace the label name. Field display name should be mentioned in id.

Tasks.XML

<?xml version="1.0" ?>

<Tasks>  

    <TaskName id="PnP">Promoted best practices, such as design, error handling and documentation. </TaskName>

    <TaskName id="Architecture">To build the process flow and infrastructure required for system. </TaskName>

    <TaskName id="SustainPlan">Worked on a sustainability plan</TaskName> 

</Tasks>

 Below JavaScript files will take care of the label text change.

JavaScript:

ReplaceLabels.js

function ChangeLabelTaskNameFrmXML() {  

   var xmlPath="../WebResources/new_Tasks.xml";

   //Put the <TaskName> element into an object. 

   var xmlnodePath = "//Tasks/TaskName";

   var xmldoc = new ActiveXObject("Microsoft.XMLDOM");

   xmldoc.preserveWhiteSpace = true;

   xmldoc.async = false;

   xmldoc.load(xmlPath);

   //Extract the different values using a loop.  

   var xmlnodelist;

   xmlnodelist= xmldoc.selectNodes(xmlnodePath);  

   for (var i = 0; i < xmlnodelist.length; i++) {

      var lbltaskname = xmlnodelist(i).getAttribute('id');

         var newlbltaskname =  xmlnodelist(i).text;

      ChangeLabelTaskName(lbltaskname, newlbltaskname);

   }

}



function ChangeLabelTaskName(lblTaskName, newlblTaskName)

{

  var lbl = "new_" + lblTaskName.toLowerCase();

  Xrm.Page.ui.controls.get(lbl).setLabel(newlblTaskName);  

}

Below JavaScript will load the CSS that contains the style for wrapping the text in label.

LoadCSS.js (for On-Premises and Online)

function LoadCSS(path) {

    var head = window.parent.document.getElementsByTagName('head')[0];

    var link = window.parent.document.createElement('link');

    link.rel = 'stylesheet';

    link.type = 'text/css';

    link.href = path;

    link.media = 'all';

    window.parent.document.head.appendChild(link);

}


 
Web Resources:
To Upload files (JavaScript, XML and CSS) to Web Resource.

While uploading files to Web Resource, you should provide few values like name, display name, file type and then upload file using browse button.


Form Properties:
After uploading files to Web Resources, you must select the Entity Form where the labels have to be replaced.

Now, go to Form Properties as shown above and add required JavaScript files as shown below. Once you have included the JavaScript files, you must map the functions from JavaScript and provide parameter values in Event Handlers section as shown in the below screenshots.

Please remember, we need to call these JavaScript functions on Form load (Control = Form, Event= OnLoad). That too CSS should be loaded first and then followed by replacing label text.

Here the function name is ChangeLabelTaskNameFrmXML and the parameters are provided as comma separated values.

Hope this article will help you to solve the issue in replacing form control labels with desired text.

Thursday, May 11, 2017

To set value for owner field in Dynamics CRM using Plugin


In this blog, I will demonstrate how to set value for the owner field on record creation in Child entity (Contact) from Parent entity (Account) plugin using C# in Dynamics CRM.

Creating a Plugin

The below code snippet shows how the owner field of the newly created record is being set. But one thing we need to know is this process can be done only during pre-creation stage, if we are trying to update the owner field of a record which is already created then we may need to use AssignRequest as shown in section 2.



Section:1

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Microsoft.Xrm.Sdk;

using Microsoft.Xrm.Sdk.Messages;

using System.ServiceModel.Description;

using Microsoft.Xrm.Sdk.Query;

using System.Runtime.Serialization;



namespace projectname.fieldmappings

{

    public class FieldMappings : IPlugin

    {

       //Main function of the plugin

        public void Execute(IServiceProvider serviceProvider)

        { 

            //Extract the tracing service for use in debugging sandboxed plug-ins.

            ITracingService tracingService =

                (ITracingService)serviceProvider.GetService(typeof(ITracingService));



            // Obtain the execution context from the service provider.

            Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)

                serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));



            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));



            IOrganizationService orgService = serviceFactory.CreateOrganizationService(context.UserId);

           

            // The Input Parameters collection contains all the data passed in the message request.

            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)

            {

                 // Obtain the target entity from the input parameters.

                  Entity entity = (Entity)context.InputParameters["Target"];



                 //Create record in Contact Entity

                  Entity ContactDetails = new Entity("Contact");

                  // To assign the owner id of Contact Details

                  EntityReference owner = new EntityReference();

                  owner.Id = ((EntityReference)(entity.Attributes["primarycontactid"])).Id;// Taking id from current entity and assigning it to contact’s owner

                  owner.LogicalName = ((EntityReference)(entity.Attributes["primarycontactid"])).LogicalName; // Taking id from current entity and assigning it to contact’s owner

                  ContactDetails["ownerid"] = owner;



orgSvc.Create(ContactDetails);

            }

         }

      }

}



Please refer this link for registering and debugging the plugin.

Section: 2

To update the owner field of existing record, use the below code.

// Create the Request Object and Set the Request Object's Properties

AssignRequest assignowner = new AssignRequest

    {

        Assignee = new EntityReference(SystemUser.EntityLogicalName,

            _otherUserId),

        Target = new EntityReference(Account.EntityLogicalName,

            _accountId)

    };





// Execute the Request

orgService.execute(assignowner);

Output:

Account

Primary Contact: Dave Adam

Contact:

Owner: Dave Adam

No matter who creates the contact from Account but the plugin will set the primary contact as owner.

 Hope this blog may be helpful for you, will come up with another blog soon.

Augmented Reality and Virtual Reality

Here is the quick overview of Augmented Reality and Virtual Reality, also have explained how it is being used today and how it can chan...