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)

Plugin on Pre-Create of Order Close does not contain status

(0) ShareShare
ReportReport
Posted on by 1,540

I have a plugin that is triggered when an order is fulfilled or cancelled and is registered on synchronous pre-create of the orderclose entity.

I need the logic to behave differently if the order is being fulfilled or cancelled but I don't have access to this on the order close and retrieving the order entity only gives me the status the order is moving from.

Including this: tracer.Trace(targetEntity["statecode"].ToString());

Gives me an error "Given Key is not in Dictionary".

Running this:

foreach(KeyValuePair<string,object> a in targetEntity.Attributes)
{
    tracer.Trace(a.Key + ": " + a.Value.ToString());
}

Gives me this:

timezoneruleversionnumber: 0
activityid: 47615cb0-1317-e811-8117-5065f38a4a41
description: test
owningbusinessunit: Microsoft.Xrm.Sdk.EntityReference
modifiedon: 2/21/2018 2:30:04 PM


How can I detect whether the order is being cancelled or fulfilled?

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: Plugin on Pre-Create of Order Close does not contain status

    Hello,

    First idea - try to check ParentContext of your plugin - InputParameters of ParentContext should contain this information.

    Second idea - why do you handle create of "Order Close"? Try to handle correspond message like SetState/SetStateDynamicEntity of Sales Order?

  • ChrisJC Profile Picture
    1,540 on at
    RE: Plugin on Pre-Create of Order Close does not contain status

    Hi Andrew,

    Thanks for the reply. I'm struggling with the parent context. I'm getting an error "Object reference not set to an instance of an object" what trying to access context.ParentContext (infact checking whether it's null).

    I tried to run this on the SetState/SetStateDynamic messages but couldn't get other logic to work.

    Do you have any other pointers?

    Thanks,

    Chris

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: Plugin on Pre-Create of Order Close does not contain status

    Chris,

    Can you please provide your code and registration in Plugin Registration Tool? I'm smart enough but not that smart to help you without seeing the code.

  • ChrisJC Profile Picture
    1,540 on at
    RE: Plugin on Pre-Create of Order Close does not contain status

    Hi Andrew,

    Here are the relevant bits

    public class PreOrderClose : IPlugin
    {
    public void Execute(IServiceProvider serviceProvider)
    {
    IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
    IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
    IOrganizationService service = factory.CreateOrganizationService(context.UserId);
    ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
    if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
    {
    tracer.Trace("Enter Entity Check");
    targetEntity = (Entity)context.InputParameters["Target"];
    tracer.Trace("Check Parent");
    if (context.ParentContext != null) // Gives Error
    {
    IPluginExecutionContext parent = context.ParentContext;
    
    tracer.Trace("");
    tracer.Trace("Parent");
    
    Entity org_Order = (Entity)parent.InputParameters["Target"];
    //Do rest of logic. Includes editing order products hence why it's pre-create


    63760.Capture.JPG

  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: Plugin on Pre-Create of Order Close does not contain status

    Try following to detect parent message in your code:

    var orderCloseAction = context.ParentContext.MessageName;

    if (orderCloseAction == "Fulfill")

    {

    //Handle Fulfill message

    }

    else

    {

    //Handle Cancel

    }

  • ChrisJC Profile Picture
    1,540 on at
    RE: Plugin on Pre-Create of Order Close does not contain status

    That's it! Many thanks for your help

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

#2
ED-30091530-0 Profile Picture

ED-30091530-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans