CRM 2011 Custom Workflow

坚强是说给别人听的谎言 提交于 2019-12-06 22:12:43

I've received different errors or no errors, but I know its not working

First, I suggest you to list errors in your question. It will be much easier to answer you and to avoid -1s. :) Second, use tracing service in workflow.

ITracingService tracingService = context.GetExtension<ITracingService>();
tracingService.Trace("I'm tracing something....");

What should happen if Contact with specified email is not found? You have to handle this case. Probably it fails here. Post error log in question, to check this.

If I'm to give a shot-from-the-hip answer, I'd guess this is the problem is that you in some cases don't get a match and filter out everything. Then, you get null somewhere (or try to refer to an empty guid, or try to select an attribute that hasn't been entered so that, although it's defined, it's not served).

What exact errors do you get? When do you get them and when don't you?

Also, some issues with the code that I've noticed (not the solution but still might be improved).

Typo in the name. I think you wanted ContactReference.

[Output("output")]
[ReferenceTarget("contact")]
public OutArgument<EntityReference> ContactRefernce { get; set; }

I'd design the query in a bit different way. Since you know what address you want to match, you might want to filter precisely that. Also, You seem only to be needing the guid for the match based on the email so you only need to fetch that.

private Guid MatchSenderWithExistingContact(
  IOrganizationService service, String fromAddress)
{
  QueryExpression query = new QueryExpression
  {
    EntityName = "contact",
    ColumnSet = new ColumnSet("emailaddress1"),
    Criteria = new FilterExpression
    {
      Filters =
      {
        new FilterExpression
        {
          Conditions =
          {
            new ConditionExpression(
              "emailaddress1", ConditionOperator.Equal, fromAddress)
          }
        }
      }
    }
  };

  EntityCollection retrieveMultipleRequest = service.RetrieveMultiple(query);
  IEnumerable<Entity> entities = retrieveMultipleRequest.Entities;
  return entities.FirstOrDefault().Id;
}

You might want to put the declaration of the query on step-by-step syntax. I prefer it this way because it's more convenient when creating advanced queries. However, the part when you iterated and used break is definitely better off as shown above.

Happy coding!

It might behoove you to use the tracing. Usually, I declare the tracing object as a member variable in the plugin class and then write to it at every operation. That way, at least I know where manure hits the AC and can log the value of the variables right before that.

private ITracingService trace;

public void Execute(IServiceProvider serviceProvider)
{
  trace = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
  _trace.Trace("Tracing successful!");
  throw new Exception("Intentional! Nice.");
}

Just keep it mind that the trace won't show unless an uncaught exception occurs. In fact, I've intentionally crashed the execution on occasion just to see what's in the variables. So remember that if you've got a global try-catch, you'll need to re-throw the exception manually.

Upside of this method is that you can tracing both on-premise and on-line. It works for all plugin registrations except the asynchronous. There are work-arounds for that too but that wasn't the topic of your question and I've digressed enough already.

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