Prevent Data loss/Duplicates Using Concurrency Behavior In Dynamics CRM

This blog is just a reference to very important article on dealing with a problem when two or more update or delete operations on the same piece of data happen at the same time. This situation could potentially result in data loss.

Explanation : Reduce potential data loss using optimistic concurrencyReduce potential data loss using optimistic concurrency.

Sample Code : Use optimistic concurrency with update and delete operations

Get User’s Teams Using WebApi JavaScript In Dynamics 365

If you need to get current/logged in or of any other user’s all teams. You have to look for the N:N relationship schema name and use that association in WebApi request:

1

So below is the code you can use to achieve this requirement. I have just retrieving the name of the teams associated and putting an alert on Onload of a form.

I have generated the code using Rest Builder Tool . And added the filter ($filter=systemuserid eq  IdOfTheuser) manually.

function GetUserTeams()
{
    var userSettings = Xrm.Utility.getGlobalContext().userSettings; // userSettings is an object with user information.
    var current_User_Id = userSettings.userId; // The user's unique id
    var newid = current_User_Id.slice(1, -1);	
    var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/teams?$select=name&$expand=teammembership_association($filter=systemuserid eq "+newid+")", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
req.onreadystatechange = function() {
    if (this.readyState === 4) {
        req.onreadystatechange = null;
        if (this.status === 200) {
            var results = JSON.parse(this.response);
            for (var i = 0; i < results.value.length; i++) {
                var name = results.value[i]["name"];
                             
                alert(name);
            }
        } else {
            Xrm.Utility.alertDialog(this.statusText);
        }
    }
};
req.send();
}

I am calling the function on Onload event of lead record. My user has two teams associated to it:

1


2


Important – Make sure you format the code fully before you use it. i.e use the latest method in the script i.e not using Xrm.Page but rather the formContext for v9.

I hope this helps! Cheers!

 

Get Selected Record Information From Main View Or A Subgrid In Dynamics 365

I recently had a requirement to get selected record id from a main view of an entity i.e. Home page view of an entity and also on a subgrid within the form.

I also needed to pass this ID to my action to do some further operations in the plugin. This is how I did

I have taken reference from  here  and here.

  1. Download the managed solution of Ribbon Workbench from here and import it to your Dynamics CRM 365 environment: https://www.develop1.net/public/rwb/ribbonworkbench.aspx
  2. Create an unmanaged  solution and add contact entity to it. ( I am taking contact entity as I need to perform action on that, you can select any entity)
  3. Create two JPG images web resource with sizes as 16×16 and 32×32 for your ribbon button. You can take the desired image and rescale it online.
  4. Create a javascript web resource and below code to it:
    function GetRecordInformation(item)
    {
        var selectedItem = item[0];
       alert("You have Select Record with Id=" + selectedItem.Id + "\nName=" 
    	+ selectedItem.Name + "\nEntity Type Code=" + selectedItem.TypeCode.toString() + "\nEntity=" + selectedItem.TypeName);
    }
  5. Publish all your customization and open ribbon workbench tool from your solution area and select the solution that you have created and click ok:1.png
  6. Now drag and drop a button to the home grid view:1.png
  7. I have named this button as “SELECT” from the button properties you will get once you add and select the button on bottom right side of the ribbon workbench also select the Image for you button by providing the web resources we created in step 3 :1
  8. Now that out button is ready, we need to add a command and enable rule to it. to do this from the solution component area click on commands and click on + button and then from the right side click on add “Action”  and then click on “Javascript action”:1.png
  9. Add your javascript resource by searching in the lookup, if you dont find the script( i faced the issue). Simply add $webresource:WebResourceName for me its $webresource:new_SelectRecord  
  10. In the next box – add the function name i.e for me its GetRecordInformation
  11. Then click on Add parameter and select SelectedControlSelectedItemReferences Parameter. It should look like below:

    1

  12. The final step is to add an enable rule to this command. To do this – from the solution component area click on Enable Rules and click on + button. On your right side you will be presented with below, Make sure you keep the information as it is:1
  13. We are done , now click on Publish from the ribbon. wait for it to completed as it might take some time. Once done navigate to your dynamics CRM and the main entity home view, you should see the button:1.png
  14. Select a record and click on the button and Bammmm! if everything goes right you will get the record information.1

You can do the same thing on a form subgrid, all you have to do is place the button on a form grid instead of home view.

I needed to further processing with the data so I called an action and passed the record id described here : http://sacconsulting.blogspot.com/2017/03/how-to-call-global-action-from.html

I hope this has been helpful!

Make All fields Mandatory/Optional On Form Using Javascript

This should be a very quick blog on JavaScript helper which will let you make all fields on the form mandatory or optional. This requirement is hypothetical; I have not received any till now.

I am going to be calling below function on OnLoad event of the form based on a field value but you can very well call is on OnSave of record, or OnChange event of field.

For Dynamics CRM below v9:

function SetRequiredLevel()    
{
  var attributes = Xrm.Page.data.entity.attributes.get();
    for (var i in attributes) {
        attributes[i].setRequiredLevel("required");
    }
}

For Dynamics CRM v9 or above use below and make sure pass the context as first parameter ( checkbox) on the form editor while calling the function.

function SetRequiredLevel(executionContext)    
{
  var context = executionContext.getFormContext();
  var attributes =  context.data.entity.attributes.get();
    for (var i in attributes) {
        attributes[i].setRequiredLevel("required");
    }
}

1

 

1. If you need to check some value you can do that i.e.

If ( fieldAValue = 1)

{
//the above code goes here//

}

2. If you have to make fields optional rather then mandatory , do this:
setRequiredLevel(“none“);

I hope this helps!!

Quick Note: Dynamics 365 CE Limitations

limitation

I often find myself struggling to get collated information on limitations on number of different component you can create in dynamics CRM.

I researched and found below link very useful, could not re-blog hence sharing them here. full credits to the owner of this content.

Dynamics 365 CE Limitations On Technet

Limitations On Creating CRM Metdata

If you don’t find anything related to Business Process Flow & Alternate Key Limitations :

  • There can be no more than 10 activated business process flow processes per entity.
  • Each process can contain no more than 30 stages.
  • Multi-entity processes can contain no more than five entities.
  • There can be a maximum of 5 alternate key definitions for an entity

Reference : Business Process Flow Overview  & Define Alternate Keys

Please note – It is strictly applicable to Dynamics CRM 365 Online, May or May not be true for other versions.

Please feel free to comment down below if you know any other limitation on any component in dynamics CRM.

Cheers!

MB2-877 Field Service Exam Retiring…….

As we all know or for those who are not aware, Microsoft plans to restructure its certifications path due to which below dynamics CRM certification are going to retire:

Retiring on April 30, 2019

  • MB2-715 Microsoft Dynamics 365 customer engagement Online Deployment (originally published Feb. 24, 2017)
  • MB2-717 Microsoft Dynamics 365 for Sales (originally published Feb. 21, 2017)
  • MB2-877 Microsoft Dynamics 365 for Field Service (originally published Dec. 8, 2017)

If you have been following my blog , I have been preparing and also creating blog for MB2-877 Field Service exam. However because of this news I have stopped publishing more blogs. I might continue with these blog once we get the updated syllabus from Microsoft. 

Below blogs have been created so far:

  1. Overview
  2. Part -1 
  3. Part -2
  4. Part -3
  5. Part -4
  6. Part -5
  7. Part -6
  8. Part -7
  9. Part -8
  10. Part -9
  11. Part -10
  12. Part -11
  13. Part -12

I will restructure or create content again for this exam once we have updated exam on field service for dynamics crm.

See you soon!

Quick Fix : Mandatory Field Error Shows Schema Name Instead Of Display Name

Quick blog on how to fix the error you get when you save a record which has some required fields on the header of the form and CRM shows the schema name instead of display name like below:

1.png

it shows the error which includes specific field logical name instead of display name which might be confusing for user and doesn’t look very much user friendly. In my case its still very much understandable but sometimes people change name on the form and in that case it will be worse situation.

FIX :

All you have to do is put the same field on the form which is there on the header and hide it on the form. Open the form editor > select the section > look for the field and add it. Go to field properties and hide it by unchecking the visible by default box:

1

Save and publish all your changes and now try to save the record:

1.png

I hope this help!

Get Api Version Dynamically For WebApi Requests

In this post I will quickly cover the requirement of dynamically getting API version of dynamics CRM in JavaScript rather then hard coding it , so that in next releases your JavaScript doesn’t break. I am referring to this:

1

For Dynamics CRM version 8.2 or below, do this:

var apiVersion = Xrm.Page.context.getVersion();

For Dynamics CRM version 9 or above, do this:

var apiVersion = Xrm.Utility.getGlobalContext().getVersion();

2

It will give you an expanded version which includes minor and major versions. Now, all you have to do is crop the number to get only initial numbers such as 9.1 in this case:

var shortVersion= apiVersion.substring(3, myStr.indexOf(“.”) -1);

then finally use it in your request as:

    req.open("GET",Xrm.Utility.getGlobalContext().getClientUrl()+ "/api/data/"+shortVersion+"/systemusers

Hope this helps!

A bit About “Post” In Dynamics 365 CE

What Is It?

Do not get confused between a note & a post, They are two different things. With Posts you can get attention of other users to show some crucial information, they can be auto generated by the system or a user can manually add them. You can do a great collaboration between organisation members using posts. 

On the other hand, Notes are added by a user always and can consist of attachments (posts cannot have attachments).

How to enable post for custom entity OR how to turn on/off post for out of the box entities.

Whenever you create a custom entity a “post configuration” record is created in CRM. for out of the box entities, the post configuration already exists such as opportunity , cases etc.

For any entity (Custom or OOTB) if you have activities/notes enabled; you can have post enabled for it. but for any reason if you dont see it. Go to Settings > Activity Feeds configurations :

1.png

look for the entity, i.e in my case I would like to look for contact:

1

If you don’t see your entity listed here, try to look for it in the “inactive post configurations” view.  Also refresh the feeds from ribbon button, the configuration should appear:

1

Open the record, you have option to enable it or turn it off:

1

In my case for my custom entity “Loan” which I have enabled post but those were not appearing on the wall & all i was seeing is Activities and Notes:

1

So I followed steps mentioned above , went to Settings > Activity feeds configurations > couldn’t see the entity > clicked on refresh button and then I could see the entity and it was in inactive status:

1

LEARNING : For a custom entity by default the posts are in inactive status & has to enabled manually:

So I opened it,  activated it and enabled for the wall: 

1.png

Make sure you publish the related entity and then open the record, you will the post has been enabled:

1

That’s for this blog – I will try to cover few important features of post in dynamics CRM.

MB2-877 MICROSOFT DYNAMICS 365 FOR FIELD SERVICE MODULE 3 TOPIC – OPTIMIZE RESOURCE SCHEDULING

This is the 13th blog of this series, before this I have created a blog from module 3 on the topic “Implement the Schedule Assistant”  which you can check here: MB2-877 MICROSOFT DYNAMICS 365 FOR FIELD SERVICE MODULE 3 TOPIC – IMPLEMENT THE SCHEDULE ASSISTANT

In this blog we are starting with the topic – Optimize resource scheduling from module 3 “Scheduling and dispatch work orders”:

1

  1. Identify licensing requirements for resource scheduling add-on

    Resource Scheduling Optimization(RSO) is an add-on capability for the Dynamics 365 for Field Service application that enables you to automatically schedule work orders for the most appropriate resources, by optimizing bookings for the shortest travel time, available work hours, and many other constraints. Below licenses are required in order to use RSO.

    ⦿ Requires a valid production instance of Field Service
    ⦿ Licensed per resource you want to optimize

    1.png

  2. Create optimization profiles

    Optimization is based on profiles that define what you want to optimize and how you want to optimize it       Example: Optimize currently scheduled items for the next three hours to minimize travel time.

    I would really suggest you to go through the brief article here : RSO Configuration   as I will not be able to perform these steps to show you how its done. It requires a valid field service license. So we will need to just understand the concept!

     

  3. Define objectives and constraints 

    When setting up RSO , you will have objectives and constraints that needs defining.  Objectives are nothing but your primary goal i.e what you want to achieve using RSO, while constraints are boundaries you apply to your goal.

    Objectives can be:

    Maximize total working hours: The version of the engine results (iteration) with the total highest aggregate work time will win this objective.
    Minimize total travel time: The version of the engine results (iteration) with the total lowest aggregate travel time will win this objective.
    Locked Booking: The engine will try and schedule the iteration/scenario that successfully creates/assigns the most “Locked” schedules to appropriate resources.
    High priority Requirement: The fitness function will evaluate this objective, and will give priority to the solutions with the highest score for priority.

    Constraints can be:

    Schedule Within Working Hours: Verifies that the task is not scheduled out side of the resource’s working hours.
    Meets Required Characteristics: Verifies the resource meets all the required skills needed to complete the work.
    Locked to Resource: If marked, Resource Scheduling Optimization will not change any locked resources in the booking requirement.
    Scheduling Windows: If marked, Resource Scheduling Optimization will not change any locked scheduling windows in the booking requirement.
    Restricted Resources: If marked, Resource Scheduling Optimization will check resource preference to see if it’s “restricted”. If the requirement has a restricted resource in which there is no expiration, or expiration in the past, Resource Scheduling Optimization will not schedule this resource for this booking requirement.

  4. Configure resource optmization
    You can configure resource optimization, which majorly covers three points, When  RSO will run, What RSO will optimize & How booking will be optimized.

    1



We could not do much  handson on this topic due to the limitation of having a paid field service trial but will try/focus on as much as handson on other topics. see you in the next blog!