Sample code to update Access Mode of System User in C# (CRM 2016 or earlier)


Recently had a requirement to set Access Mode of around 500 users to Administrative from Read – Write. We wrote an on demand workflow for that. However, running that workflow on those 500 records in a batch of 100 records was causing them to stuck in waiting stage and we had to then manually resume those workflow instance. So we ended up writing a Console Application for that.


   QueryExpression queryExpression = new QueryExpression();
            queryExpression.EntityName = "systemuser";
            queryExpression.ColumnSet = new ColumnSet();
            queryExpression.ColumnSet.AddColumn("fullname");

            ConditionExpression conditionExpression1 = new ConditionExpression();
            conditionExpression1.AttributeName = "tk_businessarea";
            conditionExpression1.Values.Add("EE286698-AF08-E311-B55E-1CC1DE6DAA0B");
            conditionExpression1.Operator = ConditionOperator.Equal;

            ConditionExpression conditionExpression2 = new ConditionExpression();
            conditionExpression2.AttributeName = "isdisabled";
            conditionExpression2.Values.Add(false);
            conditionExpression2.Operator = ConditionOperator.Equal;

            // access mode ==> 0 --> Read Write and 1 --> Adminstrative
            ConditionExpression conditionExpression3 = new ConditionExpression();
            conditionExpression3.AttributeName = "accessmode";
            conditionExpression3.Values.Add(1);
            conditionExpression3.Operator = ConditionOperator.Equal;
            
            queryExpression.Criteria.AddCondition(conditionExpression1);
            queryExpression.Criteria.AddCondition(conditionExpression2);
            queryExpression.Criteria.AddCondition(conditionExpression3);

            queryExpression.Criteria.FilterOperator = LogicalOperator.And;

            EntityCollection entityColl = organizationProxy.RetrieveMultiple(queryExpression);
            foreach(var entity in entityColl.Entities)
            {
                Entity userEntity = new Entity("systemuser");
                userEntity.Id = entity.Id;
                userEntity.Attributes["accessmode"] = new OptionSetValue(0);
                organizationProxy.Update(userEntity);
            }

Hope it helps !!

Generic SQL error in CRM 2015 Online (and earlier)


Recently we were getting the Generic SQL Error in one of our Plugin which was registered in Create of Opportunity Line.

The scenario was that on qualifying the lead, opportunity was getting created and we had Async Plugin on Create of Opportunity that will create an Opportunity Line and had Sync Plugin on Create of Opportunity Line that was updating the parent Opportunity. This issue was occurring randomly.

We observed the following, that if both the Plugin are registered as Sync or Async the issue doesn’t occur. It also doesn’t occur if we have Opportunity Plugin on Create as Sync and the one on Opp. Line as Async.

The other way of solving the issue was to introduce delay before updating the Parent Opportunity from the child opportunity line create Plugin.

Hope this helps..

Using Parallel ForEach and ExecutionTransactionRequest in CRM 2015 for bulk creation of records.


Hi,

Recently we had a requirement to read each account record and create 4 related child records for each of them. First we wrote a console app that will loop through each of the account records and will create corresponding records. The tool was working fine however it’s execution was very slow. It was talking around 5 hours to process 10000 Account records and create corresponding 40000 child records.

To fine tune the performance, we implemented the Parallel.ForEach.

We got the performance improvement, however, we were getting the following error

The communication object, System.ServiceModel.Security.TransportSecurityProtocol, cannot be used for communication because it has been Aborted.

And because of this, we had few Account records which had one, two or three records instead of total 4 related records as required. (as the child records might be getting created by separate thread).

For e.g. we can see that for Account Number 98208 we have four records created one at 8:33, then another one at 8:36, 8:38 and 8:41. (gap of minimum 2 minutes). So if there was an exception we had account records created with less than 4 child records.

We solved this issue by using ExecuteTransactionRequest, and creating all the child records as part of the transaction so that we either have all the 4 records created or none.

All the child records were getting created at the same time

We also got huge performance improvement, the code that was initially taking 5 hours to process 10000 records, was now taking only 1 hour to process them.

Few points to remember:-

  • ExecuteMultipleRequest can contain ExecuteTransactionRequest and not vice versa.
  • Batch Size of 1000 applies to both. In Online the max parallel request could be 2.
  • ExecuteTransactionRequest can have mix of Create and Update Request.

Hope it helps..

Sample code to close a quote as won using WinQuoteRequest in CRM 2016 (and earlier)


Just sharing a sample code to close quote as won through C#


WinQuoteRequest winQuoteRequest = new WinQuoteRequest();
Entity quoteClose = new Entity("quoteclose");
quoteClose.Attributes["quoteid"] = new EntityReference("quote", new Guid("015816C2-2F10-E611-8112-3863BB353ED0"));
quoteClose.Attributes["subject"] = "Quote Close" + DateTime.Now.ToString();
winQuoteRequest.QuoteClose = quoteClose;
winQuoteRequest.Status = new OptionSetValue(-1);
organizationProxy.Execute(winQuoteRequest);

Hope it helps..

Using Rollup fields with Account Hierarchy in CRM 2015.


We recently had a requirement wherein we had one custom entity Financial Detail associated to Account as many to one relationship.

i.e. One account record can have multiple Financial Details records associated.

We had this field Total (currency) in Financial Detail entity of type currency.

The requirement was to have sum of Total of all related Financial Details records of the Account record plus of any Sub Accounts (child accounts up to n level).

It would have been painful if we had to implement the same using Plugin.

This we can achieve easily using Rollup Field in Account

Below is the definition of the Roll up field named Total Rollup

  • Set Hierarchy as Yes.
  • Select Financial Details as the related entity.
  • Filter based only Active records.
  • In Aggregation specify Sum of Total (currency field in Financial Details record).

 

Hope it helps.