Exploring the Value of ChatGPT for Dynamics 365 Developers

In this blog, we will explore the ways in which ChatGPT can be a valuable tool for Dynamics 365 developers.

As we discussed in a previous blog (Exploring ChatGPT: A Language Model Powered by AI. Should Developers Feel Threatened by its Capabilities?), ChatGPT is a powerful language model that can provide fast and accurate responses to a wide range of queries. By leveraging ChatGPT, developers can quickly find answers to a wide range of questions, from general knowledge to specific code samples.

To illustrate this point, let’s consider an example of how a developer might use ChatGPT to create a custom development in Dynamics 365.

 

1) Creating Entity & Fields Needed

In a custom entity named Test we will have two tabs (General & Settings) in the main form. In the General tab we will have a few random fields (let’s say named : Field 2/3/4) which the user of the app is supposed to populate when creating a new record, but after the user saves the record and goes back to this record to make changes/updates then we want these fields to be hidden so the user can’t change them anymore.

In the Settings tab there will only be one field named ‘Fields To Hide’, which will be of type ‘Single Line of Text’ or ‘Multiple Lines of Text’ which in programming terms it means that this field holds string values. The purpose of this field is to contain the logical names of all the fields we want to hide in the form. In a serious development the admin of the system would be the on to populate this field not the user but since I’m acting as both the user and the admin we’ll populate them just to make the example work.

So let’s say the user created a new record and populated these fields as shown below:

Then the admin populates the field ‘Fields To Hide’ with the logical names of the fields we want to be hidden from the user after creating the record:

Now every time the main form of the Test entity reloads then a JavaScript web resource should be triggered, which will check if the form is on ‘Create’ mode or ‘Update’ mode , if it is on ‘Update’ mode then we will retrieve the value of the field ‘Fields To Hide’ which is a string value, then we will convert this value from string to an array so we can iterate through it by using the forEach function or by using a FOR loop, then for every logical name in this array we will perform a ‘hide field’ statement.

Let’s now use ChatGPT to help us create the JavaScript code that we need.

 

2) Using ChatGPT to Create the Code

After having a good understanding of what should be done then all left to do is formulate the ‘problem’ we want to solve, so that way the response ChatGPT gives back is close to what we need:

As you can see in the screenshot above I tried to make the problem clear (even though I left something out on purpose, can you catch it what was left out) so ChatGPT has no room to misunderstand.

Just by looking at the code I can tell that 80-90% of the development just got solved, all I have to do now is to create the new web resource in CRM, pass it this code and set it up so it can be triggered on load of the form.

Now that everything is ready, let’s go back to the record and let’s debug the code to see if everything works correctly or if it needs more modifications:

As soon as I re-loaded the form it threw an error, so the code didn’t work correctly and by looking at the information provided from the system we have the error ‘fieldsToHide.forEach is not a function‘. This error was created on purpose in the moment I formulated the problem for ChatGPT where I didn’t mention what value was going to be retrieved from the field ‘Fields To Hide’ so we don’t really need to debug the code to understand what’s wrong but for the example let’s say that we have this issue because the developer made a mistake now let’s further use ChatGPT to fix this issue.

Let’s use ChatGPT to find what’s wrong, let’s ask it about the proper syntax of the ‘forEach’ function :

So as seen above the forEach function iterates over the elements of an array but in our case the value retrieved from the field ‘fieldsToHide’ is not an array but a string, so the error makes total sense.

Now the next step is to find a way to turn the retrieved string value into an array, let’s see if ChatGPT can help us with this:

So as we can see ChatGPT helped us fix this issue too and all left to do is to modify the code so the string is turned into an array before the forEach iteration starts.

But just to show ChatGPT’s capabilities, let’s re-formulate the ‘problem’ in a more detailed way so ChatGPT can include this part in the response:

So as we can see as long as the problem is explained in great detail then the response is close to what we want, now that we got the correct code let’s go back to CRM, update the web resource with the new code and see if everything works correctly.

As seen in the video recording above when we open an existing record the fields specified in the field ‘Fields To Hide’ become hidden but if we create a new record this fields are visible so the user can populate them.

 

3) Conclusions

This example demonstrates the usefulness of ChatGPT as a tool for developers. Although the task in this example was not particularly complex, it highlights how quickly and easily developers can solve issues with the help of ChatGPT when they have a clear understanding of what needs to be done. However, it is important to note that if the problem’s description is not precise, the response from ChatGPT may not be the complete solution that you are looking for.

Sometimes even if the problem is properly explained, ChatGPT may not always provide perfectly accurate responses but it can still provide valuable benefits. For example, in our case the code returned by ChatGPT was an incorrect response (even though this was on purpose on our part, not ChatGPT’s fault), the developer can catch this mistake during testing and debugging, ensuring that the final implementation is accurate. In this way, the responsibility for quality control ultimately rests with the developer, making ChatGPT a helpful tool for enhancing their work.

Also in this example, we were able to successfully debug the code by using ChatGPT and by rephrasing the problem at hand so that ChatGPT could better understand the issue. The response we received from ChatGPT provided us with the correct code, which required no further modifications.

This demonstrates that ChatGPT is a powerful tool that can make the lives of developers easier.