web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Retrieve plug-in does not run when opening a record

(0) ShareShare
ReportReport
Posted on by

I'm writing a plug-in for Dynamics CRM Online 2015 which will be triggered to run every time the user clicks on an account. However, when I clicked on an account to go the account detail page, the plug-in didn't run. Here's my setup

CRM-ask-2.JPG

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Aiden Kaskela Profile Picture
    19,696 on at
    RE: Retrieve plug-in does not run when opening a record

    Hi,

    All the settings on your plugin registration are right (they all look right at least). You should probably check all your code - maybe throw an exception on the first line of the plugin so you can see if it's getting in there. In my experience, the plugin will be registered fine but a bug makes it look like it isn't firing.

    Thanks,

     Aiden

  • Community Member Profile Picture
    on at
    RE: Retrieve plug-in does not run when opening a record

    Thanks for the quick reply. I tried to test my plug-in code by registering the plug-in as "Update" on "Account" with field "Account Name" and when I clicked on an account, changed the "Account Name" -> plug-in was firing. So I think my code works. However, I still need the plug-in to be fired when the users click on an Account :(

  • Aiden Kaskela Profile Picture
    19,696 on at
    RE: Retrieve plug-in does not run when opening a record

    Would you mind posting your plugin code? I want to try and run through it on my environment.

    Thanks,

     Aiden

  • Community Member Profile Picture
    on at
    RE: Retrieve plug-in does not run when opening a record

    Sure. Here it is:

    Response.cs class

    using System.Runtime.Serialization;
    
    namespace PROSPlugin
    {
        [DataContract]
        public class Response
        {
            [DataMember(Name = "responseID")]
            public string ResponseID { get; set; }
            [DataMember(Name = "timestamp")]
            public string Timestamp { get; set; }
            [DataMember(Name = "type")]
            public string Type { get; set; }
            [DataMember(Name = "context")]
            public Context Context { get; set; }
            [DataMember(Name = "data")]
            public Data Data { get; set; }
        }
    
        [DataContract]
        public class Context
        {
            [DataMember(Name = "tenant")]
            public string Tenant { get; set; }
            [DataMember(Name = "maxLimit")]
            public int MaxLimit { get; set; }
            [DataMember(Name = "minRevenue")]
            public long MinRevenue { get; set; }
            [DataMember(Name = "states")]
            public string States { get; set; }
            [DataMember(Name = "scope")]
            public string Scope { get; set; }
        }
    
        [DataContract]
        public class Data
        {
            [DataMember(Name = "opportunities")]
            public PAO[] Opportunities { get; set; }
            [DataMember(Name = "summary")]
            public Summary Summary { get; set; }
        }
    
        [DataContract]
        public class PAO
        {
            [DataMember(Name = "scope")]
            public Scope Scope { get; set; }
            [DataMember(Name = "opportunityId")]
            public string OpportunityId { get; set; }
            [DataMember(Name = "status")]
            public string Status { get; set; }
            [DataMember(Name = "opportunityType")]
            public string OpportunityType { get; set; }
            [DataMember(Name = "revenuePotential")]
            public long RevenuePotential { get; set; }
        }
    
        [DataContract]
        public class Scope
        {
            [DataMember(Name = "accountID")]
            public string AccountID { get; set; }
            [DataMember(Name = "productGroup")]
            public string ProductGroup { get; set; }
        }
    
        [DataContract]
        public class Summary
        {
            [DataMember(Name = "revenuePotential")]
            public long RevenuePotential { get; set; }
        }
    }


    ImportPAOsPlugin.cs

    using System;
    using System.Net;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Json;
    
    // Microsoft Dynamics CRM namespace(s)
    using Microsoft.Xrm.Sdk;
    
    namespace PROSPlugin
    {
        public class ImportPAOsPlugin : IPlugin
        {
            static string subscriptionKey = "47efbf9794ee4f3daf866a843051b40d";
    
            public void Execute(IServiceProvider serviceProvider)
            {
                // Extract the tracing service for use in debugging sandboxed plug-ins
                ITracingService tracingService = 
                    (ITracingService)serviceProvider.GetService(typeof(ITracingService));
    
                // Obtain the execution context from the service provider
                IPluginExecutionContext context = (IPluginExecutionContext)
                    serviceProvider.GetService(typeof(IPluginExecutionContext));
    
                // The InputParameters collection contains all the data passed in the message request
                if (context.InputParameters.Contains("Target") &&
                    context.InputParameters["Target"] is Entity)
                {
                    // Obtain the target entity from the input parameters
                    Entity entity = (Entity)context.InputParameters["Target"];
    
                    // Verify that the target entity represents an account.
                    // If not, this plug-in was not registered correctly
                    if (entity.LogicalName != "account")
                        return;
    
                    // Obtain the organization service reference
                    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
    
                    try
                    {
                        string paosRequest = CreateRequest("Honda");
                        Response opportunititesResponse = MakeRequest(paosRequest);
    
                        int PAONum = opportunititesResponse.Data.Opportunities.Length;
    
                        for (int i = 0; i < PAONum; i++)
                        {
                            PAO paoResponse = (PAO)opportunititesResponse.Data.Opportunities[i];
    
                            // Create a new PAO entity
                            Entity pao = new Entity("new_pao");
    
                            pao["new_name"] = paoResponse.Scope.ProductGroup;
                            pao["new_accountid"] = paoResponse.Scope.AccountID;
                            pao["new_productgroup"] = paoResponse.Scope.ProductGroup;
                            pao["new_status"] = paoResponse.Status;
                            pao["new_opportunityid"] = paoResponse.OpportunityId;
                            pao["new_opportunitytype"] = paoResponse.OpportunityType;
                            pao["new_revenuepotential"] = new Money(paoResponse.RevenuePotential);
                            pao["new_revenuepotentialstring"] = paoResponse.RevenuePotential.ToString();
    
                            // Create the PAO in Microsoft Dynamics CRM
                            tracingService.Trace("ImportPAOsPlugin: Creating the PAO.");
                            service.Create(pao);
                        }
                    }
    
                    catch (Exception ex)
                    {
                        tracingService.Trace("ImportPAOsPlugin Error: {0}", ex.ToString());
                        throw;
                    }
                }
            }
    
            public static string CreateRequest(string accountID)
            {
                string UrlRequest = "prospaodev.azure-api.net/.../prescriptive-actions-summary" +
                                        accountID +
                                        "&subscription-key=" +
                                        subscriptionKey;
                return (UrlRequest);
            }
    
            public static Response MakeRequest(string requestUrl)
            {
                try
                {
                    HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
                    using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                    {
                        if (response.StatusCode != HttpStatusCode.OK)
                            throw new Exception(String.Format(
                            "Server error (HTTP {0}: {1}).",
                            response.StatusCode,
                            response.StatusDescription));
                        DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(Response));
                        object objResponse = jsonSerializer.ReadObject(response.GetResponseStream());
                        Response jsonResponse
                        = objResponse as Response;
                        return jsonResponse;
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    return null;
                }
            }
        }
    }
  • Verified answer
    Community Member Profile Picture
    on at
    RE: Retrieve plug-in does not run when opening a record

    I figured out why. For the Retrieve message the right parameter to check is BusinessEntity inside OutputParameters.

    IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
    
    Entity entity;
    
    if (context.OutputParameters.Contains("BusinessEntity") &&
    
    context.OutputParameters["BusinessEntity"] is Entity)
    {
        entity = (Entity)context.OutputParameters["BusinessEntity"];
    }
    else
    {
        return;
    }


  • Aiden Kaskela Profile Picture
    19,696 on at
    RE: Retrieve plug-in does not run when opening a record

    Hi,

    Sorry I wasn't helpful, I'm glad you figured it out.

    Thanks,

    Aiden

  • Community Member Profile Picture
    on at
    RE: Retrieve plug-in does not run when opening a record

    Thanks for the reply though. However, now my plug-in is fired multiple times when I click on an account. Any advice?

  • Aiden Kaskela Profile Picture
    19,696 on at
    RE: Retrieve plug-in does not run when opening a record

    Do you have any other code where you're restoring the account by ID? That would spawn a new retrieve event.

  • Community Member Profile Picture
    on at
    RE: Retrieve plug-in does not run when opening a record

    I don't use accountId anywhere in my code. Btw this is the plug-in was fired three times in a row (with the same depth = 1). If I set the Excution Mode from Sync to Async, the plug-in will be fired 6 times.

    CRM-ask-3.JPG

  • Community Member Profile Picture
    on at
    RE: Retrieve plug-in does not run when opening a record

    Hi,

    Did you find the solution for this. I've done the same thing and running it for three times even though the depth = 1. Mode = Sync

    Thanks,

    bnsp 

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#1
UllrSki Profile Picture

UllrSki 2

#3
ED-30091530-0 Profile Picture

ED-30091530-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans