Posts

Showing posts with the label C#

SET DYNAMICS 365 MULTISELECT OPTIONSET FIELD IN C#

As known, Multi-Select Option set field helps you select more than one option. Sometimes, you want to dynamically select one or more option in the multiselect field. In another post, we saw how to set a value of a multiselect option set field in JavaScript In this post, we will see how to set options of a multiselect option set field in C# The below code snippet can be used to add the needed options you want to set for the multi select option set field OptionSetValueCollection osvc = new OptionSetValueCollection(); osvc.Add(new OptionSetValue(<integerValueOptionSet>)); Once the needed options are added to the OptionSetValueCollection , you can add it to the list of attributes of...

CREATE DYNAMICS 365 ENTITY IN SDK

Image
In this post, we will learn how to create a Dynamics 365 custom entity in SDK and how to set some of its properties. The below function can be used to create a custom entity public void CreateEntity() { CreateEntityRequest createrequest = new CreateEntityRequest { Entity = new EntityMetadata { SchemaName = "cak_CustomEntity", DisplayName = new Label("Custom Entity", 1033), DisplayCollectionName = new Label("Custom Entities", 1033), Description = new Label("Custom entity created using SDK", 1033), OwnershipType = OwnershipTypes.UserOwned, IsActivity = false, IsBusinessProcessEnabled = true, ...

CREATE OR UPDATE (UPSERT) RECORDS USING ALTERNATE KEYS IN DYNAMICS 365 SDK

In another post, we saw how to retrieve a record based on alternate key in Dynamics 365 using the SDK In this post, we will see how to create or update (upsert) a record based on alternate key in Dynamics 365 using the SDK . The below sample code can be used to achieve the UpdateRequest. For the sake of this post, the alternate key is set up on the Account Name (name) field of the account entity. public void UpsertRecordByKey() { KeyAttributeCollection keys = new KeyAttributeCollection { { Account.Fields.Name, "cak account" } }; Account objAccount = new Account(); objAccount.Name = "cak account"; Upse...

RETRIEVE RECORDS USING ALTERNATE KEYS IN DYNAMICS 365 SDK

Image
With Dynamics 365 SDK, you can retrieve a single record by its GUID using the Retrieve method, but, what if you don't have the GUID of the record ? Another approach is to retrieve the record by its alternative keys. However, you cannot use the Retrieve method to do this because it does not have any option to pass the alternate key value. This approach will need the RetrieveRequest to be done. In another post, we saw how to create or update (upsert) a record based on alternate key in Dynamics 365 using the SDK Below, is a sample code on how to retrieve a record based on alternate key in Dynamics 365 using the SDK . For the sake of this post, the alternate key is set up on the Account Name (name) field of the account entity. public void RetrieveRecordB...

CONVERT QUERYEXPRESSION TO FETCHXML AND VICE-VERSA IN DYNAMICS 365

Image
In this quick post, we will see how to convert Query Expression to FetchXML and how to convert FetchXML to Query Expression in Dynamics 365 . QUERY EXPRESSION TO FECTH XML The below function can be used to convert a Query Expression into Fetch Xml query public void ConvertQueryExpressionToFetchXml() { List<Account> lstAccounts = new List<Account>(); QueryExpression qeQuery = new QueryExpression(Account.EntityLogicalName) { ColumnSet = new ColumnSet(Account.Fields.Id), Criteria = new FilterExpression() { FilterOperator = LogicalOperator.And, Conditions = { new ConditionExpression(Account.Fields.StateCod...

DYNAMICS 365 HOW TO EXECUTE FETCHXML QUERIES IN C#

Previously, we saw how to execute FetchXml query using Xrm.WebApi in JavaScript In this quick post, we will see how to execute FetchXml query in C# . Prepare the FetchXml you want to execute whether from the Advanced Find, or any other FetchXml generator in the community Create the C# function you want to call in order to execute the retrieve request and Copy/Paste the FetchXml query In order to be able to execute FetchXml queries in C#, you must use the FetchExpression class instead of QueryExpression as per the below public void FetchXmlInFetchExpression() { var fetchQuery = @" <fetch version='1.0' output-format='xml-platform' ma...

DYNAMICS 365 HOW TO RETRIEVE BUSINESS UNIT DEFAULT TEAM IN C# AND JAVASCRIPT

In Dynamics 365, each Business Unit can have one or more Teams under it, but has one Default Team that will be automatically created by the system each time a new Business Unit is created, and will have the same name as the Business Unit name. The membership of this team will be systematically managed, where users are automatically added to the team whenever users are added to the business unit and removed from the team whenever users are removed from the business unit. In addition, the default team can be used to simplify the management of the security roles when users have common permissions, where you can associate a security role to the Default Team with a set of permissions that will be given to all users in the business unit. On the other hand, Default Teams cannot be deleted and will have the field Team Type set to Owner ; therefore, they can own records if needed by setting it ...

DYNAMICS 365 HOW TO CREATE AND MANAGE POLYMORPHIC LOOKUP ATTRIBUTE IN SDK C#

Image
In other posts, we saw how to create a Polymorphic Lookup Attribute using the Polymorphic Lookup Manager plugin and how to create a Polymorphic Lookup Attribute using WEB API As per Microsoft docs, the creation of the Polymorphic Lookup Attribute can be currently done through SDK or WebAPI, therefore, in this post, we will see how to create a Polymorphic Lookup Attribute using SDK . We will see as well, what are the different actions that can be done against a polymorphic lookup attribute. In order to test these actions, I've created a console application that connects to a trial environment using OAUTH authentication. For more details on how to connect to a Dynamics 365 environment using OAUTH authentication , you can check the ...

DYNAMICS 365 CALL CUSTOM ACTION FROM JAVASCRIPT AND C#

Image
In another post, we saw how to call workflows from JavaScript and C# . In this post, I will show you how to call a Custom Action from JavaScript and C# . For the sake of the post, I created a simple custom action against the Account entity with two input parameters and one output parameter. This action will create a task record and set the Subject field to Task created by custom Action from JavaScript after calling the action from JavaScript Task created by custom Action from C# after calling the action from C# Below is the end result after calling the action from JavaScript Below is the end result after calling the action from C# The below function calls the...

DYNAMICS 365 CALL WORKFLOW FROM JAVASCRIPT AND C#

Image
In another post, we saw how to call custom actions from JavaScript and C# . In this post, I will show you how to call workflows from JavaScript or C# and to accomplish this, you need two parameters: The Workflow Id that you want to call AND The Record Id that the workflow will be executed against For the sake of the demo, I created a workflow that will set the Account Name field to: WF Called and Updated From JavaScript after calling the workflow from JavaScript WF Called and Updated From C# after calling the workflow from C# Below is the end result after calling the workflow in JavaScript Below is the end result after calling the workflow in C# ...

DYNAMICS 365 HOW TO FORCE RECALCULATE ROLLUP FIELDS

In this quick Blog post, we will see how to force recalculate Rollup fields in JavaScript and C# using the below functions. Recalculate Rollup field in JavaScript (for account entity) function reCalculateRollupField(entityPluralName, recordGuid, rollupFieldName) { recordGuid = recordGuid.replace("{", "").replace("}", ""); var requestUrl = Xrm.Page.context.getClientUrl() + "/api/data/v9.0/CalculateRollupField(Target=@tid,FieldName=@fn)?@tid={'@odata.id':'" + entityPluralName + "(" + recordGuid + ")'}&@fn='" + rollupFieldName + "'"; var HttpReq = new XMLHttpRequest(); // Double Check the API URL HttpReq.open("GET", requestUrl, true); ...

DYNAMICS 365 HOW TO RETRIEVE MORE THAN 5000 RECORDS IN C#

Image
In this post, you will see how to retrieve more than 5000 records from CRM/D365 entities in C# . To do this, I imported thousands of records into the contact entity Created a console application From the Main method, called the two methods: RetrieveMultiple : It will use the Service.RetrieveMultiple request RetrieveMultipleRequest : It will use the RetrieveMultipleRequest message Ran the console application that will give the below result The RetrieveMultiple method is limited to retrieve 5000 records maximum and will look as follows. public string RetrieveMultiple() { EntityCollection objCollection = new EntityCollection(); ...