Skip to main content
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

  • irenegr Profile Picture
    165 on at
    RE: Post create plugin throws error: Salesorder id does not exist

    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

News and Announcements

Now Available: 2025 Release Wave 2

Quick Links

Ramesh Kumar – Community Spotlight

We are honored to recognize Ramesh Kumar as our July 2025 Community…

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

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

#1
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 51 Most Valuable Professional

#2
Ramesh Kumar Profile Picture

Ramesh Kumar 42

#3
David Shaw_UK Profile Picture

David Shaw_UK 27

Featured topics

Product updates

Dynamics 365 release plans