Close an incident or case in MS CRM

To close incident record we need to update statecode, for this generally we do use SetStateRequest but this will works only for cancel the case or incident not for close or resolve. Below is the code snippet to close an incident.

                    CloseIncidentRequest closeIncidentRequest = new CloseIncidentRequest();
                    Entity incidentEntity = new Entity("incidentresolution");
                    incidentEntity.Attributes.Add("incidentid", new EntityReference("incident", caseId));


                    closeIncidentRequest.IncidentResolution = incidentEntity;
                    closeIncidentRequest.Status = new OptionSetValue(statusReason);
                    CloseIncidentResponse closeIncidentResponse = (CloseIncidentResponse)service.Execute(closeIncidentRequest);


Update statecode and statuscode in MS CRM

In general we do use update method of organization service to update any record in crm, but it doesn't work to update statecode or statuscode. Below is the code snippet we should use to update these fields.

                SetStateRequest setStateRequest = new SetStateRequest();
                setStateRequest.EntityMoniker = new EntityReference("your entity name", recordGuid);

                setStateRequest.State = new OptionSetValue(stateCode);
                setStateRequest.Status = new OptionSetValue(statusReason);

                SetStateResponse setStateResponse = (SetStateResponse)service.Execute(setStateRequest);

Set session timeout in MS CRM

By default online session timeout is 1440 minutes or 24 hours. We can configure or set session timeout as per requirement as shown below.

Go to settings->Adminstration->System settingss->General Tab.


Under Set session timeout section, click on Set Custom radio so that, below text boxes will be enabled for you to set session timeout.


Entity Change Tracking in MS CRM

Change Tracking is usefull when we are synching the data with external sources. Earlier it was very difficult to track or maintain the changes since last synchronized. With small piece of code and samll setting it will be very easy now.

By selecting, or deselecting, change tracking for specific entities you can reduce the load on your server resources and save processing time when extracting Dynamics 365 data and synchronizing it to an external store. You can enable change tracking for both system and custom entities


  1. Go to Customizations > Customize the System.
  2. Select an entity, and under Data Services, select the Change Tracking check box.














string token;

            // Initialize page number.
            int pageNumber = 1;
            List<Entity> initialrecords = new List<Entity>();

            // Retrieve records by using Change Tracking feature.
            RetrieveEntityChangesRequest request = new RetrieveEntityChangesRequest();
            request.EntityName = _customBooksEntityName.ToLower();
            request.Columns = new ColumnSet("sample_bookcode", "sample_name", "sample_author");
            request.PageInfo = new PagingInfo() { Count = 5000, PageNumber = 1, ReturnTotalRecordCount = false };


            // Initial Synchronization. Retrieves all records as well as token value.
            Console.WriteLine("Initial synchronization....retrieving all records.");
            while (true)
            {
                RetrieveEntityChangesResponse response = (RetrieveEntityChangesResponse)_serviceProxy.Execute(request);

                initialrecords.AddRange(response.EntityChanges.Changes.Select(x => (x as NewOrUpdatedItem).NewOrUpdatedEntity).ToArray());
                initialrecords.ForEach(x => Console.WriteLine("initial record id:{0}", x.Id));
                if (!response.EntityChanges.MoreRecords)
                {
                    // Store token for later query
                    token = response.EntityChanges.DataToken;
                    break;

                }
                // Increment the page number to retrieve the next page.
                request.PageInfo.PageNumber++;
                // Set the paging cookie to the paging cookie returned from current results.
                request.PageInfo.PagingCookie = response.EntityChanges.PagingCookie;
            }


We need to store token for later query. Below are the constraints of entity change.

  1. Only one entity will be tracked in retrieve changes. If retrieve changes is executed with no version / or token, the server will treat it as the system minimum version, returning all of the records as new. Deleted objects won’t be returned.
  2. Changes will be returned if the last token is within a default value of 90 days. If it is more than 90 days, the system will return all the records.
  3. If a client has a set of changes for an entity, say version 1, a record is created and deleted prior to the next query for changes, they will get the deleted item even if they didn’t have the item to begin with.
  4. Records are retrieved in the order determined by server side logic. Usually, the end user will always get all new or updated records first (sorted by version number) followed by deleted records.  If there are 3000 records created or updated and 2000 records deleted, Dynamics 365 returns a collection of 5000 records, which have the first 3000 entries comprised of new or updated records and the last 2000 entries for deleted records.
  5. If the new or updated item collection is greater than 5000, the user can page through the collection.

Prevent auto save in MS CRM

By default auto save is enabled in organization level. We can disable auto save in organization level or form level using small script.

Disabling auto save in organization level:
  1. Go to Settings > Administration.
  2. Choose System Settings.
  3. For the Enable auto-save for all forms option, select No.

Disable auto save in form level:

As there is no setting to disable auto save for a form we have to write down small piece of code as shown below. And register the method on onsave and don't forget to check Pass execution context as first parameter

function preventAutoSave(econtext) {
    var eventArgs = econtext.getEventArgs();
    if (eventArgs.getSaveMode() == 70 || eventArgs.getSaveMode() == 2) {
        eventArgs.preventDefault();
    }
}


We have to repeat the same process for the all the forms on which we want to disable autosave. 70 is the save mode for Auto save.

Below are the some interesting points about autosave.

  1. Form will automatically be saved thirty seconds after the change is made.
  2. The field that someone is currently editing isn’t included in an auto-save
  3. If someone else has updated the same record while you’re editing it, those changes will be retrieved and displayed in the form when auto-save occurs.
  4. When auto-save is enabled the record will be saved whenever you navigate away from a record or close a separate window displaying a record.

MSCRM Tutorials Sitemap

Site is still under development. I would like to hear your inputs to make it better

Vector Format or Scalable Vector Graphics web resource in MS CRM Dynamics 365 9.0

Dynamics 365, version 9.0 introduced a new Image web resource called Vector Format  (SVG). SVG stands for Scalable Vector Graphics an XML-based vector image format. This is very useful for any icon presented in application. Earlier we have to define multiple of image with  different sizes to set an icon. Now, with Vector image you can define one vector image and re-use it rather than provide multiple sizes of images.

Featured Post

Improving MS CRM Performance

Performance on MS CRM is always a crucial thing and we may follow different ways to achieve the performance thing. Below is the one more a...