Posts

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

Dynamics CRM 365 Error 'The Given Key is not present in dictionary'

As a Dynamics CRM developer we always come across the error saying that 'The Given Key is not present in dictionary'. The reason we receive this error message because of below issues:- When we try to get the attribute value from Entity object without checking the attribute. String AccountNumber=objAccount["accountnumber"];   The above line will throw error if ObjAccount doesn't contains this attributes.  TO fix this we should always check attributes in Entity object then get the value. Resolution: if(objAccount.attributes.contains("accountnumber")) String AccountNumber=objAccount["accountnumber"];            We can also use below code which will return null if attribute is not present in Entity object. string AccountNumber=objAccount.GetAtrributeValue<String>("accountnumber"); Happy CRMing!

CRM dynamics Hiding Add Existing Subgrid (+) Button

We had the requirement to Hide Add existing button for one of the grid on the form. We have achieved above requirement using DOM explorer. Due to multiple Enable/Display rule associated to this button it was hard to hide this button. //Hiding Add Existing Activity Contacts Subgrid (+) Button document.getElementById("subGrid_ActivityContacts_addImageButton").disabled = true; document.getElementById("subGrid_ActivityContacts_addImageButton").style.visibility = "hidden"; document.getElementById("subGrid_ActivityContacts_addImageButton").style.display = "none"; If you need to Hide Add Existing button for any SubGrid, please replace Subgrid Button ID with your SubGrid ID. You Can get this ID from Developer tool of any browser. Hope this Helps! Happy CRMing!

CRM Dynamics 2013 Set Header Field Value

Often we need to set value of Header field in CRM 2013. XRM.Page object model doesn't support setting up value to header form. We tried putting the field on main form and Header as well. Using XRM.Page setValue() to Main form field doesn't auto populate field value on Header form. To overcome this problem we have used below code to set the value on Header field. if (document.getElementById("header_new_membership_d") { document.getElementById("header_new_membership_d").innerText = membershipText; } Please note that above code is unsupported and not recommended by Microsoft. This code will not work if Microsoft makes changes to DOM for newer release. Hope it helps! Happy CRMing!