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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Retrieve method does not work in dynamics 365 online (version 9.0)

(0) ShareShare
ReportReport
Posted on by

Here is full code, when i trying to retrieve data. it does not work saying,

d8be1dff-fb39-e811-a833-000d3a33bdbd: Test.Test: Retrieve of contact] (full error file is attached with this thread)ErrorDetails-_2800_35_2900_.txt

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;

namespace Test
{
public class Test : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
throw new NotImplementedException();
ITracingService tracingService =
(ITracingService)serviceProvider.GetService(typeof(ITracingService));

// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));

if (context.Depth == 1)
{
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

// Obtain the target entity from the input parmameters.
EntityReference entity = (EntityReference)context.InputParameters["Target"];
//EntityReference entity = (EntityReference)context.OutputParameters["BusinessEntity"];


tracingService.Trace("3 - entity");
var contact = service.Retrieve("contact", entity.Id, new ColumnSet(true));
tracingService.Trace("4 - entity");
if (contact != null)
{
if (contact.Attributes.Contains("address1_name") == false)
{
Random rndgen = new Random();
contact.Attributes.Add("address1_name", "first time value: " + rndgen.Next().ToString());
}
else
{
contact["address1_name"] = "i already exist";
}
service.Update(contact);
}
}
}
}
}

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Mahendar Pal Profile Picture
    45,095 on at

    Hi,

    On which event you have registered your plugin ??

    // Obtain the target entity from the input parmameters.
    EntityReference entity = (EntityReference)context.InputParameters["Target"];

    you are not retrieving entity here instead you are using entityreference, to get entity you need to use below code

    Entity entity=(Entity)context.InputParameters["Target"];

    var contact = service.Retrieve("contact", entity.Id, new ColumnSet(true));
    tracingService.Trace("4 - entity");
    if (contact != null)
    {
    if (contact.Attributes.Contains("address1_name") == false)
    {
    Random rndgen = new Random();
    contact.Attributes.Add("address1_name", "first time value: " + rndgen.Next().ToString());
    }
    else
    {
    contact["address1_name"] = "i already exist";
    }
    service.Update(contact);
    }

    It seems you are trying to update some attribute based on the address1_name exists or not ?? if yes you can simply write a pre create plugin where you don't need to use update statement instead you can simply add your attribute to property bag like you are using here, no need to call update method in this case

    contact.Attributes.Add("address1_name", "first time value: " + rndgen.Next().ToString());

    Let me know if you need more information

  • Community Member Profile Picture
    on at

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Threading.Tasks;

    using Microsoft.Xrm.Sdk;

    using Microsoft.Xrm.Sdk.Query;

    namespace Test

    {

       public class Test : IPlugin

       {

           public void Execute(IServiceProvider serviceProvider)

           {

               throw new NotImplementedException();

               ITracingService tracingService =

                   (ITracingService)serviceProvider.GetService(typeof(ITracingService));

               // Obtain the execution context from the service provider.

               IPluginExecutionContext context = (IPluginExecutionContext)

                   serviceProvider.GetService(typeof(IPluginExecutionContext));

               if (context.Depth == 1)

               {

                   IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

                   IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                   // Obtain the target entity from the input parmameters.

                   Entity entity = (Entity)context.InputParameters["Target"];

                   //EntityReference entity = (EntityReference)context.OutputParameters["BusinessEntity"];

                   tracingService.Trace("3 - entity");

                   var contact = service.Retrieve("contact", entity.Id, new ColumnSet(true));

                   tracingService.Trace("4 - entity");

                   if (contact != null)

                   {

                       if (contact.Attributes.Contains("address1_name") == false)

                       {

                           Random rndgen = new Random();

                           contact.Attributes.Add("address1_name", "first time value: " + rndgen.Next().ToString());

                       }

                       else

                       {

                           contact["address1_name"] = "i already exist";

                       }

                       //service.Update(contact);

                   }

               }

           }

       }

    }

    Still not working base on your suggestion same error again,

    i've register my plugin for "preoperation"

  • Verified answer
    Arpit Shrivastava Profile Picture
    7,518 User Group Leader on at

    Hi Vinay,

    You have been written -

    throw new NotImplementedException in very first line inside the Execute method.

    And in your error file..it also gives the same error.

    Comment out this line and try.

    namespace Test

    {

       public class Test : IPlugin

       {

           public void Execute(IServiceProvider serviceProvider)

           {

               throw new NotImplementedException();

               ITracingService tracingService =

                   (ITracingService)serviceProvider.GetService(typeof(ITracingService));

    If my answer helped to resolve your issue, kindly verify it by clicking 'Yes'. It would be helpful to the other community members seeking to resolve a similar issue.


    Cheers
    Arpit
    https://arpitmscrmhunt.blogspot.in

     

  • Verified answer
    Abby Kong Profile Picture
    6 on at

    Hi K,

    The error says NotImplementedException.
    Try search in your plugin project source code to see if there is any throw new NotImplementedException()

    Regards,
    Abby

  • Verified answer
    Mahendar Pal Profile Picture
    45,095 on at

    You need to comment this line             //  throw new NotImplementedException();

  • Community Member Profile Picture
    on at

    Thank you everyone your suggestion helped me resolved my issue. while i'm trying to use "Entity" it does not work but it works with "EntityReference.

    Please let me know what is reason behind this

                 //Entity entity = (Entity)context.InputParameters["Target"];

                   EntityReference entity = (EntityReference)context.InputParameters["Target"];

                   tracingService.Trace("3 - entity");

                   var contact = service.Retrieve("contact", entity.Id, new ColumnSet(true));

                   tracingService.Trace("4 - entity");

                   if (contact != null)

                   {

                       if (contact.Attributes.Contains("address1_name") == false)

                       {

                           Random rndgen = new Random();

                           contact.Attributes.Add("address1_name", "first time value: " + rndgen.Next().ToString());

                       }

                       else

                       {

                           contact["address1_name"] = "i already exist";

                       }

                       service.Update(contact);

                   }

  • Suggested answer
    Temmy Wahyu Raharjo Profile Picture
    2,916 on at

    msdn.microsoft.com/.../gg309673.aspx;MSPPError=-2147217396

    Hi, you can check this one. If you using (EntityReference)context.InputParameters["Target"]   and it's works. It should be in  Delete message.

  • Community Member Profile Picture
    on at

    Getting 404 while opening your link.

  • Suggested answer
    Arpit Shrivastava Profile Picture
    7,518 User Group Leader on at

    Hi Vinay,

    I think you should have use below syntax, if you want to go with Entity as a Target.

    Because Target as an Entity does not have property entity.Id while Target as an EntityReference has.

    You need to get record guid from context instead of Entity object.If you use Target as an Entity.

    Entity entity = (Entity)context.InputParameters["Target"];

    tracingService.Trace("3 - entity");

    var contact = service.Retrieve("contact", context.PrimaryEntityId, new ColumnSet(true));

    Can also check below article for more info:

    [View:https://community.dynamics.com/crm/f/117/t/201583]

    Also, I would recommend to use Pre-Image instead of performing Retrieve query in order to get context entity unchanged field's data.

    It will avoid one additional DB call and improve plugin performace.

    Hope it helps

    Mark this as answer. If it helps.

    Cheers

    Arpit

  • Mahendar Pal Profile Picture
    45,095 on at

    Hi,

    What is available under Target, it depends on message on which plugin is registered, can you share your details on which event you are registring your plugin ??

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans