locked
Plug-in error with 'default' code on message: "Retrieve" RRS feed

  • Question

  • Hi,

    I'm trying to make a plugin, that should run when ever an Account is opened.

    As you can see below, I've tried inserting an Exception to see how far the plugin is getting before failing, but it doesn't even reach the Exception before getting this Error message:

    "Error

    An error has occurred.

    Try this action again. If the problem continues, check the Microsoft Dynamics CRM Community for solutions or contact your organization's Microsoft Dynamics CRM Administrator. Finally, you can contact Microsoft Support."

    Here is some more info about the plugin:

    Primary Entity: Account
    Message: Retrieve
    Pipeline Stage: Pre-Operation
    Execution Mode: Synchronous

    *Don't pay much attention to the code below the Exception*

    namespace TimestampOnAccount.Plugins { using System; using System.ServiceModel; using System.ServiceModel.Description; // These namespaces are found in the Microsoft.Xrm.Sdk.dll assembly // found in the SDK\bin folder. using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Client; using Microsoft.Xrm.Sdk.Query; using Microsoft.Xrm.Sdk.Discovery; /// <summary> /// PreAccountRetrieve Plugin. /// </summary> public class PreAccountRetrieve: Plugin { /// <summary> /// Initializes a new instance of the <see cref="PreAccountRetrieve"/> class. /// </summary> public PreAccountRetrieve() : base(typeof(PreAccountRetrieve)) { base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(20, "Retrieve", "account", new Action<LocalPluginContext>(ExecutePreAccountRetrieve))); // Note : you can register for more events here if this plugin is not specific to an individual entity and message combination. // You may also need to update your RegisterFile.crmregister plug-in registration file to reflect any change. } /// <summary> /// Executes the plug-in. /// </summary> /// <param name="localContext">The <see cref="LocalPluginContext"/> which contains the /// <see cref="IPluginExecutionContext"/>, /// <see cref="IOrganizationService"/> /// and <see cref="ITracingService"/> /// </param> /// <remarks> /// For improved performance, Microsoft Dynamics CRM caches plug-in instances. /// The plug-in's Execute method should be written to be stateless as the constructor /// is not called for every invocation of the plug-in. Also, multiple system threads /// could execute the plug-in at the same time. All per invocation state information /// is stored in the context. This means that you should not use global variables in plug-ins. /// </remarks> /// private Guid _accountID; protected void ExecutePreAccountRetrieve(LocalPluginContext localContext) { //ITracingService _tracingService = localContext.TracingService; //_tracingService.Trace("Start på plugin"); throw new Exception("throw new Exception"); if (localContext == null) { throw new ArgumentNullException("localContext"); } // TODO: Implement your custom Plug-in business logic. IPluginExecutionContext _context = localContext.PluginExecutionContext; IOrganizationService _service = localContext.OrganizationService; Entity account = new Entity(Account.EntityLogicalName); ColumnSet accountatt = new ColumnSet(new string[] { "name", "description" }); account = _service.Retrieve(account.LogicalName, _accountID, accountatt); account["description"] = "test"; _service.Update(account); } }
    }



    • Edited by M.Jeppesen Friday, November 14, 2014 10:18 AM code change
    Friday, November 14, 2014 10:15 AM

Answers

  • Replace last 5 lines of code with:-

    Entity updateAccount = new Entity();
    updateAccount.Id = _context.PrimaryEntityId.ToString();
    updateAccount.LogicalName = "account";
    updateAccount["description"] = "test";
    _service.Update(updateAccount);


    Regards Faisal


    • Edited by Faisal Fiaz Friday, November 14, 2014 1:43 PM
    • Marked as answer by M.Jeppesen Monday, November 17, 2014 9:56 AM
    • Unmarked as answer by M.Jeppesen Monday, November 17, 2014 9:56 AM
    • Marked as answer by M.Jeppesen Monday, November 17, 2014 9:56 AM
    Friday, November 14, 2014 1:43 PM

All replies

  • Replace last 5 lines of code with:-

    Entity updateAccount = new Entity();
    updateAccount.Id = _context.PrimaryEntityId.ToString();
    updateAccount.LogicalName = "account";
    updateAccount["description"] = "test";
    _service.Update(updateAccount);


    Regards Faisal


    • Edited by Faisal Fiaz Friday, November 14, 2014 1:43 PM
    • Marked as answer by M.Jeppesen Monday, November 17, 2014 9:56 AM
    • Unmarked as answer by M.Jeppesen Monday, November 17, 2014 9:56 AM
    • Marked as answer by M.Jeppesen Monday, November 17, 2014 9:56 AM
    Friday, November 14, 2014 1:43 PM
  • Thanks for the advice on thoes line to get them working, but the plugin doesn't even run that far...

    It fails even before the line: throw new Exception("throw new Exception");
    Because it never throws that exception.



    • Edited by M.Jeppesen Friday, November 14, 2014 1:58 PM
    Friday, November 14, 2014 1:57 PM
  • try throw new InvalidPluginExecutionException("");


    If my response helped you find your answer please show your thanks by taking the time to "Mark As Answer" and "Vote As Helpful".

    Twitter LinkedIn Facebook Blog Magnetism

    Saturday, November 15, 2014 7:32 AM
  • Hm, that 'worked'. It threw the InvalidPluginExecutionException(""); 

    What I don't understand is why the plugin is failing. 
    Now I've tried setting up an empty plugin in the CRM SDK, with no code except what is created by default, and it's still failing.
    It's however not happening when I create a plugin to run on Update.

    I know it's not much info to go from, but any ideas?


    • Edited by M.Jeppesen Monday, November 17, 2014 7:26 AM
    Monday, November 17, 2014 7:20 AM
  • Finally, I got it working.
    I know this is not much help, but after running the InvalidPluginExecutionException("");  a few times different places in the plugin, it worked...

    Also to Faisal Fiaz:
    I changes your code a bit, and now that workes to :)

                Entity updateAccount = new Entity();
                updateAccount.Id = _context.PrimaryEntityId;
                updateAccount.LogicalName = "account";
                updateAccount["description"] = "test";
                _service.Update(updateAccount);

    Monday, November 17, 2014 7:41 AM
  • Hi, if your issue is resolved now please close this question by marking the posts that helped you as 'answer'. This will also help others with the same problem find the correct answer more easily in future.

    One other thing that can help you debug plugins is to use the ITracingService. You've got this commented out in your code, however when you perform a 'Trace', the text will be logged in case the plugin crashes (via downloading the log in the InvalidPluginExecutionException popup). This can be useful for determining where the plugin has crashed, and if you're tracing out variables you could also see why (null exceptions etc).

    Hope that helps

    Paul


    If my response helped you find your answer please show your thanks by taking the time to "Mark As Answer" and "Vote As Helpful".

    Twitter LinkedIn Facebook Blog Magnetism

    Monday, November 17, 2014 9:21 AM
  • Thanks a lot, I'll give that a try :)
    Monday, November 17, 2014 9:56 AM