Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

Trigger plugin when account has been viewed

(0) ShareShare
ReportReport
Posted on by 25

Hi there,

I am new to Dynamics 365 development. I created a plugin which executes when a new account has been created and works fine.

Now I would like to execute the same plugin when an Account is being viewed/Retrieved.

So this is what I have on my plugin code:

pastedimage1605854336283v2.png

And this is how I register a step on Retrieve of account.

pastedimage1605854211294v1.png

Could you please let me know what I am missing here?

Regards,

Gzee

  • Suggested answer
    Adrian Begovich Profile Picture
    Adrian Begovich 1,014 Super User 2024 Season 2 on at
    RE: Trigger plugin when account has been viewed

    Hi gee1463,

    The approach I would take is developing on load JavaScript that calls a custom action. This allows you to pass JavaScript variables as parameters of a C# function and receive a response. This will have better performance than the Retrieve and RetrieveMultiple messages approach, and URLs and secret tokens of the API will not be exposed. This article explains how to create JavaScript, custom actions, and plugins that work together. You will just need to modify the code to meet your specific scenario.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.Xrm.Sdk;
     
    namespace Carl.AddNumbers
    {
        public class CustomAction : IPlugin
        {
            public void Execute(IServiceProvider serviceProvider)
            {
                IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = factory.CreateOrganizationService(context.UserId);
     
                var Number1 = (int)context.InputParameters["Number1"];
                var Number2 = (int)context.InputParameters["Number2"];
                var Number3 = Number1   Number2;
     
                context.OutputParameters["Number3"] = Number3;
            }
        }
    }
    

    var parameters = {};
    parameters.Number1 = 7;
    parameters.Number2 = 9;
     
    var new_AddNumbersRequest = {
        Number1: parameters.Number1,
        Number2: parameters.Number2,
     
        getMetadata: function () {
            return {
                boundParameter: null,
                parameterTypes: {
                    "Number1": {
                        "typeName": "Edm.Int32",
                        "structuralProperty": 1
                    },
                    "Number2": {
                        "typeName": "Edm.Int32",
                        "structuralProperty": 1
                    }
                },
                operationType: 0,
                operationName: "new_AddNumbers"
            };
        }
    };
     
    Xrm.WebApi.online.execute(new_AddNumbersRequest).then(
        function success(result) {
            result.json().then(
                function (response) {
                    var sixteen = response.Number3;
                }
            );
        }
        ,
        function (error) {
            Xrm.Utility.alertDialog(error.message);
        }
    );

  • Gee1463 Profile Picture
    Gee1463 25 on at
    RE: Trigger plugin when account has been viewed

    Hi Adrian, thanks a lot for your suggestion.

    Actually, I will need to make a call to an external API to gather some data an then add them in specific fields on the account form.

    I would like to make this call from the plugin instead of javascript. The reason is about exposing URLs and secret tokens of the API in the javascript. Do you have any other suggestion that I can make this work?

    Really appreciate your answer.

    Regards

  • Suggested answer
    Adrian Begovich Profile Picture
    Adrian Begovich 1,014 Super User 2024 Season 2 on at
    RE: Trigger plugin when account has been viewed

    Hi Gee1463,

    Have you considered on load JavaScript as an alternative? The best practice is to limit the registration of plug-ins for Retrieve and RetrieveMultiple messages. In general, it is not recommended to register plug-ins for these messages due to the risks associated with slowing down the requests to return an entity record or records from various entry points. You should only use Retrieve and RetrieveMultiple messages on rare occasions.

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,959 Super User 2024 Season 1 on at
    RE: Trigger plugin when account has been viewed

    Hi,

    Check below article to write your business logic on Retrieve plugin.

    nothingnessit.wordpress.com/.../

    Your steps are correct, check if you have registered as synchronous mode. Do you get the error message which you have in code when you open any record?

    Retrieve plugin will trigger when you open any record from Home page.

    Please mark my answer verified if i were helpful

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,684 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,414 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans