Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested answer

How to get data from cancel salesorder step

(0) ShareShare
ReportReport
Posted on by 645

I'm using cancel salesorder step (for the first time) and I need to get the record's guid, my problem is that I get exeption of TargetImageEntity ('Target image entity doesn't exist in current context. ') how can I get the record guid from the pluginContext ?

  • Suggested answer
    Bipin D365 Profile Picture
    28,961 Moderator on at
    RE: How to get data from cancel salesorder step

    Hi,

    When we select calcel sales order from UI, New Order Close Entity record will be created by the system.

    To fix your issue. please register plugin step as shown below.

    CQ4.PNG

    Select Pre-operation/Post-Operation based on requirement in execution pipeline.

    Use below code to get record ID (Order ID)

    using Microsoft.Crm.Sdk.Messages;
    using Microsoft.Xrm.Sdk;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Microsoft.Crm.Sdk.Samples
    {
        public class CancleSalesOrder : IPlugin
        {
            public void Execute(IServiceProvider serviceProvider)
            {
                //Extract the tracing service for use in debugging sandboxed plug-ins.
                ITracingService tracingService =
                (ITracingService)serviceProvider.GetService(typeof(ITracingService));
    
                // Obtain the execution context from the service provider.
                IPluginExecutionContext context = (IPluginExecutionContext)
                serviceProvider.GetService(typeof(IPluginExecutionContext));
    
                // Obtain the organization service reference.
                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
    
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    //Order Close 
                    Entity orderClose = context.InputParameters["Target"] as Entity;
    
                    if (orderClose.Attributes.Contains("salesorderid") && orderClose["salesorderid"] != null)
                    {
                        EntityReference salesOrder = orderClose.GetAttributeValue("salesorderid");
                        //To Get Order ID
                        Guid orderId = salesOrder.Id;
                    }
                }
            }
        }
    }
    

    You could also get additional fields from context Target Entity

    CQ5.PNG

    If found helpful, Please mark my answer verified.

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,916 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,782 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans