In this chapter, you will start developing the SBMA membership application. In this initial phase of development, the primary focus will be on out-of-the-box customizations. In Chapter 2, we discussed the details of how to create the base solution, and in this chapter, we will create basic customizations as solution patches and showcase the steps to add the solution to source control and to the target environment.
Form Customizations
To get started with form customizations, you will first create the solution patch that will include the form customizations. By using solution patching, you can include the forms that you are going to modify in the patch without bothering to have all the other components in the solution. This section will cover how to add fields and custom entities to the system and how to arrange the main forms of those entities.
Now you can add the components. Remember, you will be modifying only the main form and adding new fields as required. So, you have to add the entity and the main form only. This is the same as adding existing items to the base solution. The only difference is that you are selecting the required components only.
- 1.
Identify the fields that are required. Member is a custom entity, and it already has some fields out of the box. It is always good to use these as much as possible. Since the form is going to be modified to meet SBMA’s requirements, it is a best practice to create a new main form so that the default, out-of-the-box form remains as it is.
- 2.
If any additional fields are required for the final application, create those additional fields.
- 3.
Arrange the interface by adding the new fields and removing any unnecessary fields and sections.
The default Member form is a huge form; you do not require all the sections on the form for the application, so you can remove the sections not necessary for the application. For instance, you can remove Marketing, Contact References, etc., as per the SBMA application requirements. You can simply select the section and hit Delete on the keyboard or click the Remove button on the ribbon.
Note
When removing sections, keep in mind that the components in that section will also be removed. If that happens, then you will have to add the items again. For instance, by default on this form there are three columns, and if you change the formatting to have two columns, then you will lose the components in the third column. The trick is that you can move the items in column 3 to column 1 and change the formatting to One Column. You also have Undo and Redo options. The nice thing about the form editing is that while editing the form, you can preview the changes. See Figure 3-6.
Event Code: This field is used to identify the events and is a unique code. As you can see, the field is set to Business Required, and the length of the field is 10 characters. All these properties can be set when you create the field.
Name: This is also set to Business Required, and it is an out-of-the-box field.
Event Status: This is an option set that defines the workflow of the event from start to completion or cancellation.
Event Budget: This is a currency type field for allocating the funds for the event.
Start Date and End Date: These are date-time fields that define the event’s start date and end dates. When defining date-time fields, you can choose not to show the time field. But in this context, the time part is required. This is set to Business Required because for a given event, there must be a start date and an end date.
Registration Start and End Date: Like with the event start and end dates, you can define the start and end dates for event registrations. Again, these fields also require a time part.
Event Capacity: This defines the number of registrations allowed in the event, which is usually a whole number. This is not always required because there could be events with unlimited registrations such as fundraising events.
After completing all these customizations, it is good to add the changes to source control. So, as explained in Chapter 2, update the solution name in SPKL.json with the unique name of the solution patch (SBMASolution_Patch_4ce06dde). Then use the SPKL unpack.bat file to extract and check in the solution patch to source control, and use the SPKL pack+import.bat tool to package it and import it to the target environment.
In the next section, the book will elaborate on how to set autonumbering for the entities.
Autonumbering Option
Autonumbering was one of the biggest pain points until the recent past because not all entities supported autonumbering option. Developers had to invent ways of implementing autonumbering for custom entities. But, ever since the version 9.0 release, you can create autonumbering for any entity. At the time of writing this book, there is no interface to configure the autonumbering, but in XrmToolBox, there is a plugin called Auto Number Manager that can be used to configure the autonumbering for any entity of the solution.
There are several autonumber formats supported by this feature. You can find more details about autonumbering at https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/create-auto-number-attributes .
Since this is going to involve more customizations, you can create another solution patch and include the entities that require autonumbering. Similar to the previous patch you created, include only the entity and the main form, and click “No, do not include required components.” For the examples given here, we will be using the Member and Event entities.
Solution: When you are successfully connected to the Dynamics 365 instance, the plugin will list all the solutions in your instance. Select the solution patch you have created to configure the autonumbering.
- Entity: Select the entity for which you want to configure the autonumbering field.
You have two options: either you can create a new autonumber field by clicking the New Field button or you can select an existing field and update it with the autonumber you require, which is what you will be doing here to the Account Number field of the Member entity.
Next, define the number format. You can click the snippet, and it will enter the snippet in the field. For this example, enter the prefix SBMA, a sequence number with a length of 6 digits, and a random string.
Seed: This is the starting number of the sequence, and you will see the sample number that is generated when the new records are created.
When updating, you will be warned that autonumbering will make the field read-only across all the forms; click OK. In this example, you are updating an existing field and entering a seed number; it will also warn you about duplicate data. If you have duplicate data, you should create a new field. Since in this example you are using a fresh instance, it is OK.
Likewise, you can select all the relevant entities and add the autonumber as per the end-user requirements. Finally, check in the solution to Azure DevOps and release it to the target environment. In the next section, you will look into some of the basic validation techniques provided by Dynamics 365.
Basic Validations
When entering data, there is always a possibility of entering invalid or incorrect data into the system, which will cause incorrect transactions and outputs. Therefore, whatever system you develop, data validations are a must. In this section, we will be looking at a few of these techniques.
Setting a Field as Required
Calculated Fields
Since the execution occurs on the server side, the users will see the changes only after the save event is triggered, which is when the user clicks Save. Also, the calculated fields are read-only and do not consider the end user’s security roles. Note that there are several limitations with calculated fields such as they cannot trigger workflows or plugins on calculated fields, duplicate detection rules are not triggered, etc. You can find out more at https://docs.microsoft.com/en-us/previous-versions/dynamicscrm-2016/administering-dynamics-365/dn832103(v=crm.8)#Considerations .
Rollup Fields
Like calculated fields, rollup fields are an exciting feature introduced recently that have reduced the volume of code required. Rollup fields are accumulated values computed over a set of records in relation to a record such as the total number of registrations for a given event. For example, the event organizers may want to know the number of paid registrations, payment pending registrations, total number of attendance, and cancellations. All these can be considered as event statistics, and recording them with the event provides insight about the event so the planners can make critical decisions. Therefore, calculating these values should be automated and ensure the values are correct. In this example, you will be categorizing these fields into a tab on the form by inserting a tab.
Similar to calculated fields, the rollup fields are read-only, and they will not take the user’s security settings. You can define only 100 rollup fields per organization and only 10 per entity. You can find out more about rollup fields at https://technet.microsoft.com/en-us/library/dn832162.aspx .
Business Rules
When business rules were first introduced, users, especially power users, greeted them with great pleasure because they reduce the amount of JavaScript and plugin code required. They are effective in setting up fast-changing business rules while avoiding the trouble of writing complex code snippets. This is useful in scenarios where a rule is scoped at the entity level. Business rules are capable of showing or hiding fields, enabling/disabling fields, validating data and showing error messages, setting field values, etc.
When click the New Business Rule button, the business rules designer window will be displayed. The design window is used to define the rule as a graphical workflow. In the left corner of the design area, there is a mini-map that enables you to quickly navigate to components in the workflow. You can drag and drop components from the Components tab on the right. The Properties tab next to the Components tab allows you to set the conditions and the properties of each component. For instance, for a condition component, you will have to define the condition on the Properties tab.
You should know a few additional things about business rules. In the top-right corner of the business rules designer, there is an option to select the scope of the business rule, which was set to All Forms in the previous examples. This means that if there is more than one form, then all the forms will adhere to the business rules defined, and the rules will apply to the client side only. But if you want the rule to be available on the server side, when operating data outside of the form such as in grids, the Web API, the SDK, and bulk data imports, then you will have to set the scope to Entity. In addition to these nice features, there are few limitations.
First, there is a limit of 10 “if-else” conditions per business rule, and the rules cannot be applied to tabs or sections. The rules will be executed only with on-change and on-load events and will not fire with on-save events unless the scope is set as Entity. Finally, conditions cannot contain a mixture of AND and OR; they must be either one or the other. For additional information about business rules, please visit https://docs.microsoft.com/en-us/dynamics365/customer-engagement/customize/create-business-rules-recommendations-apply-logic-form .
JavaScript and TypeScript
When it comes to Dynamics 365 customizations, JavaScript is an essential component that is capable of providing that extra layer of customization to the client side. Being a client-side programming language, JavaScript enables you to do complex validations, record manipulation, and validation on the client side. But the catch is that when you continue to write JavaScript library after library, it becomes more and more complex and can easily end up as spaghetti code in no time. To avoid this, you must have a great deal of programming skills. Even though there are other techniques such as business rules, calculated fields, rollup fields, etc., to limit the client-side code, there are situations where you must write JavaScript to meet specific requirements.
With the Dynamics 365 v9.x release, Microsoft has made some changes to the client API object model and deprecated some of the existing client APIs. Compared to the changes introduced in CRM 2011, these are not significant changes. Everything that was used before is still there, but things have been moved around to add more consistency. You can find more about these changes at https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/clientapi/understand-clientapi-object-model .
JavaScript Example
The client-side code is triggered with events, which means you should connect the JavaScript code/function to a specific event such as OnSave, OnLoad, etc. Generally, you can use a JavaScript function, which is also known as an event handler, and each handler may or may not have parameters. These event handlers can be associated with the events using the GUI, but for those that are not available via the GUI, you should use the client API methods, which provide the mechanisms to call the event handlers. These methods can be used to collaborate with business process flow control, manage form navigation, get/set attribute values, communicate with multiple forms per entity, etc.
- 1.
Upload your JavaScript as a web resource to Dynamics 365 for Customer Engagement. In your new solution, select the web resources in the left pane and click New on the grid toolbar.
- 2.
Provide the name and display name, set Type as Script (Jscript), set Language to English, and browse your code library. Click Save.
- 3.
Close the web resources window, and in the Solution window, click Publish All Customizations.
- 4.
Now, let’s wire up the script to the OnLoad event of the form. Open the form via the form editor window. In the ribbon, click the Form Properties button, and in the window that pops up, add your library on the Form Libraries tab.
- 5.
On the Event Handlers tab, select FormLoad as the event and add your function.
- 6.
When adding the function, you must make sure you are passing executionContext as the first parameter. To do this, select the “Pass execution context as first parameter” checkbox.
TypeScript Example
When you have to write more complex JavaScript, the code will become more complex and will be difficult to maintain. The readability of your code will also degrade. In such scenarios, TypeScript is an ideal choice. It is not a new language, but it is the next generation of writing JavaScript code. The coolest thing about TypeScript is that you will be writing ES9 features that will be transpiled to ES6. You can learn more about TypeScript at https://www.typescriptlang.org/docs/home.html . Explaining TypeScript in detail is beyond the scope of this book, but you will learn how to write a simple TypeScript and transform it via the Transpiler into JavaScript that can be used with Dynamics 365.
The next step is to install the TypeScript definitions for the Dynamics 365 client-side SDK. In the same console window, enter the following command: npm install - -save @types/xrm.
In the upcoming chapter, the book will use TypeScript instead of JavaScript in the examples.
Summary
In this chapter, you looked at form customizations, autonumbering, calculated fields, rollup fields, business rules, and JavaScript to ensure the validity of data entry. The chapter also discussed the new changes introduced to the JavaScript object model. Finally, the chapter looked at how to use TypeScript to make the client-side scripting more exciting. In the next chapter, you will be looking at automating the business processes.