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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Service | Customer Service, Contact Center, Fie...
Unanswered

Post create plugin throws error: Salesorder id does not exist

(0) ShareShare
ReportReport
Posted on by 165

Hello,

I've made a plugin in order to automatically create an order detail when an order is created.

Here is my code:

            if (context.InputParameters.Contains("Target") &&
                context.InputParameters["Target"] is Entity)
            {

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


                IOrganizationServiceFactory serviceFactory =
                    (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                try
                {
                    Guid caseSerialNumber = Guid.Empty;
                    Guid orderId = Order.Id;

                    if (Order.GetAttributeValue("zst_case") != null)
                    {
                        caseSerialNumber = Order.GetAttributeValue("zst_case").Id;

                        QueryExpression query = new QueryExpression("zst_parttask");
                        query.ColumnSet = new ColumnSet("zst_product", "zst_quantity", "zst_unit");
                        query.Criteria.AddCondition("regardingobjectid", ConditionOperator.Equal, caseSerialNumber);

                        EntityCollection collection = service.RetrieveMultiple(query);

                        if (collection.Entities.Count > 0)
                        {

                            foreach (Entity product in collection.Entities)
                            {
                                Entity orderProduct = new Entity("salesorderdetail");

                                orderProduct["salesorderid"] = new EntityReference("salesorder", orderId);

                                if (product.Attributes.Contains("zst_quantity"))
                                {
                                    var inputQuantity = product.GetAttributeValue("zst_quantity");
                                    orderProduct.Attributes.Add("quantity", Convert.ToDecimal(inputQuantity));
                                }

                                if (product.Attributes.Contains("zst_product"))
                                {

                                    EntityReference taskProduct = (EntityReference)product.Attributes["zst_product"];

                                    var LookupId = taskProduct.Id;
                                    var logicalName = taskProduct.LogicalName;
                                    orderProduct["productid"] = new EntityReference(logicalName, LookupId);

                                }

                                if (product.Attributes.Contains("zst_unit"))
                                {
                                    EntityReference taskUnit = (EntityReference)product.Attributes["zst_unit"];
                                    var LookupIdUnit = taskUnit.Id;
                                    var logicalNameUnit = taskUnit.LogicalName;
                                    orderProduct["uomid"] = new EntityReference(logicalNameUnit, LookupIdUnit);
                                }

                                bool _price = false;
                                bool _product = false;

                                product["ispriceoverridden"] = _price;
                                product["isproductoverridden"] = _product;

                                service.Create(orderProduct);
                            }
                        }
                    }
                }

The plugin step is on Create of Order (Post operation, I've tried synchronously and asynchronously)

When I use the plugin profiler to debug it, I get the error "Salesorder id does not exist"

Anh help would be appreciated

I have the same question (0)
  • irenegr Profile Picture
    165 on at

    Still need help with this...

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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Service | Customer Service, Contact Center, Field Service, Guides

#1
Abhilash Warrier Profile Picture

Abhilash Warrier 51 Super User 2026 Season 1

#1
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 51 Most Valuable Professional

#3
Nagaraju_Matta Profile Picture

Nagaraju_Matta 50

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans