Quantcast
Channel: CRM Keeper
Viewing all articles
Browse latest Browse all 171

Dynamics 365 AI code with ChatGPT

$
0
0

By now I am sure most of you have heard about ChatGPT. The AI product is pouring out surprisingly intelligent responses to all sorts of questions that people all around the world have given it lately.

There have been examples of the tool writing papers for school, creating blog posts for people, and helping formulate fairly complex text for webpages and marketers. One thing that someone tipped me off about was the ability to write code, and I was a bit curious as to what that would mean.

So one of the many gaps in my knowledge is specifically regarding Plugins. A plugin is a piece of backend code that runs in real time when triggers occur in Dataverse. I tried doing a little plugin with some help a few years ago and wrote about it on the Company Blog – Plugin in 1 hour.

I was curious how ChatGPT would do with a similar issue, so I took it for a spin😊

Question

First off I started out with a question that you write the same way you would anything Google/Yahoo/AltaVista/Bing/AskJeeves/DuckDuckGo/etc etc etc

The response I get is fairly spot on. Certainly, some will comment about something not being 100% accurate here, but this is more than good enough and quite well-written!

This wasn’t exactly what I was hoping for, so I asked a follow-up question to my original question.

It thought for a few moments before it started spitting out CODE!! 🤯🤯🤯🤯 (above is just a snip of the total code. I put it all in a code box below).

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Linq;

namespace MyPlugins
{
    public class SumProductValues : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            // Obtain the execution context from the service provider.
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

            // Obtain the organization service reference which you will need for CRUD operations.
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

            // Retrieve the Opportunity record that the plugin is running against.
            Entity opportunity = (Entity)context.InputParameters["Target"];

            // Create a query to retrieve the related products.
            QueryExpression query = new QueryExpression
            {
                EntityName = "product",
                ColumnSet = new ColumnSet("productid", "price"),
                Criteria = new FilterExpression
                {
                    Conditions =
                    {
                        new ConditionExpression
                        {
                            AttributeName = "parent_opportunity_id",
                            Operator = ConditionOperator.Equal,
                            Values = { opportunity.Id }
                        }
                    }
                }
            };

            // Retrieve the related products.
            EntityCollection products = service.RetrieveMultiple(query);

            // Sum up the value of the products.
            decimal totalPrice = products.Entities.Sum(product => product.GetAttributeValue<Money>("price").Value);

            // Update the Total Price field of the Opportunity record.
            opportunity["totalprice"] = new Money(totalPrice);
            service.Update(opportunity);
        }
    }
}

🤯🤯🤯🤯

At this point, I was extremely excited to see that the result is pretty darn good. Sure the trigger here is based on Opportunity, and not the opportunity product, but that is beside the point. It seems that I could have formulated my question in a way that I would have understood it correctly. This is actual usable code for a plugin that could run on Opportunity level to update the sum of all Opportunity products.

Not really sure what more to write about this ATM.. Still trying to wrap my head around it all for now.


Viewing all articles
Browse latest Browse all 171

Latest Images

Trending Articles





Latest Images