Posts

Showing posts from 2020

Dynamics 365 paging with fetchxml

Dynamics 365 web service has limitation to return 5000 records in a single web service call. Many a times we get the requirement to fetch more than 5000 records to meet the business requirement. Microsoft has provided the concept of paging where we can get the data returned in multiple pages. We can make use of paging and page cookie to get more then 5000 record. Below code snippet will help you achieve this. public List<Entity> GetRecordsByFetchXml(IOrganizationService service, String fetchXML)         {             List<Entity> objList = new List<Entity>();             try             {                 // Define the fetch attributes.                 // Set the number of records per page to retrieve.                 int fetchCount = 500;                 // Initialize the page number.                 int pageNumber = 1;                 // Specify the current paging cookie. For retrieving the first page,                  // pagingCookie should be null.                

Dynamics 365 Check User has Send As Privilege programatically

Business Requirement :- Many a times in our project we get the requirement to check if logged user has Send As privilege allowed in his personal setting programatically. We can use below code snippet to check the logged in user personal settings from plugin code. public bool CheckUserSendAsPrivilege(IOrganizationService service, Guid userGuid)         {             try             {                 if (userGuid != Guid.Empty)                 {                     QueryExpression queryUserSettings = new QueryExpression(UserSettings.EntityLogicalName);                     queryUserSettings.NoLock = true;                     queryUserSettings.Criteria.AddCondition(UserSettings.systemuseridAttribute, ConditionOperator.Equal, userGuid);                     queryUserSettings.ColumnSet = new ColumnSet(UserSettings.issendasallowedAttribute);                     EntityCollection ecUserSettings = null;                     ecUserSettings = service.RetrieveMultiple(queryUserSettings);            

Dynamics 365 Error #2 Object reference not set to an instance of an object

As Microsoft Dynamics developer, We came across this error regularly in our day to day life. Problem Description :-  "Object reference not set to an instance of an object" There could be multiple reason we come across this issue. However below are few things which can be looked at to fix this issue and save your time. Entity Attribute/Field does not contains data or the value is null. Say you are trying to get FirstName from Lead Entity attribute and splitting the value into two string based on some parameter. string[] strArray=objLead["firstname"].split(','); This particulat line of code will throw Object Reference error if FirstName is null. To avoid this, We can add check condition as below. if(objLead.Attributes.Contains(firstname) && objLead["firstname"]!=null); string[] strArray=objLead["firstname"].split(','); Other way to fix this is to use objLead.GetAttributeValue<string>("firstname"); Then ch

Dynamics 365 Sample Code to set null value for all data types using c#

Recently we had a requirement to set null value for all data types field from C# as part of our integration. I tried searching on internet but couldn't find proper documentation or sample code for all data types field. So i decided to try using console utility and also wanted to share with CRM communities. Sample Code Snipped                 Entity objLead = new Entity("lead");                 objLead.Id = Guid.Parse("Record GUID");                 //Currency                 //objLead["ims_loanamount"] = null;                 //Decimal                 //objLead["ims_loanrate"] = null;                 //Datetime                 //objLead["ims_birthday"] = null;                 //optionset                 //objLead["ims_gender"] = null;                 //WholeNumber                 //objLead["ims_creditscore"] = null;                 //Lookup                 //objLead["im