Error: Not able to retrieve after create in plugin

末鹿安然 提交于 2019-12-11 12:16:07

问题


I am executing a plugin synchronously postOperation. I create an entity using IOrganizationService.Create. This works, I get a GUID and I can see that the record exists in the CRM. Right after this, I am trying to retrieve the same record using same IOrganizationService and the GUID returned by the create call:

public void Execute(IServiceProvider serviceProvider)
    {
        ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
        IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
        IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
        IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
        tracer.Trace("context is " + context.InputParameters["Target"]);
        context.OutputParameters["Message"] = "in plugin";

        if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is EntityReference)
        {
/* some other processing here
.
.
*/
 Guid polId = service.Create(policy);
               Entity polEntity = service.Retrieve("new_customEntiry", polId, new ColumnSet(true));

But the polEntity.Id is null. polEntity["someattribute"] is also null .

Why is the retrieve call not working? What am I missing?

Related post: Error Account with Id = "xxxxxx" does not exist


回答1:


I am sure there are issues in code which you didn't share.

Possible rootcause:

The entity used for policy object in service.Create(policy) should be same as new_customEntiry which used in service.Retrieve, check it

Recommendations:

1.Add a trace log to log the polId in plugin trace or profile/debug it to see
2.Try asynchronous mode & test the same plugin step
3.Don't retrieve the whole entity object, instead do this

Guid polId = service.Create(policy);

Entity toUpdate = new Entity("new_entityToUpdate", entityToUpdateId);
toUpdate["new_customEntiryId"] = new EntityReference("new_customEntiry", polId);
service.Update(toUpdate);


来源:https://stackoverflow.com/questions/53108862/error-not-able-to-retrieve-after-create-in-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!